Updated Rector to commit 105df5f485c8e3b2e9b75320555a32b5325186b5

105df5f485 Remove NewToMethodCallRector as unused core and no other extension, niche to use, better handle by PHPStorm (#4029)
This commit is contained in:
Tomas Votruba 2023-05-30 13:20:44 +00:00
parent df2ad70dd3
commit 36bff0a577
9 changed files with 21 additions and 265 deletions

View File

@ -1,4 +1,4 @@
# 393 Rules Overview
# 391 Rules Overview
<br>
@ -50,7 +50,7 @@
- [Php82](#php82) (3)
- [Privatization](#privatization) (6)
- [Privatization](#privatization) (5)
- [Removing](#removing) (6)
@ -60,7 +60,7 @@
- [Strict](#strict) (6)
- [Transform](#transform) (28)
- [Transform](#transform) (27)
- [TypeDeclaration](#typedeclaration) (40)
@ -2219,7 +2219,9 @@ return static function (RectorConfig $rectorConfig): void {
{
public static function provideData()
{
- return [['some text']];
- return [
- ['some text']
- ];
+ yield ['some text'];
}
}
@ -6334,36 +6336,6 @@ Change global `$variables` to private properties
<br>
### ChangeReadOnlyPropertyWithDefaultValueToConstantRector
Change property with read only status with default value to constant
- class: [`Rector\Privatization\Rector\Property\ChangeReadOnlyPropertyWithDefaultValueToConstantRector`](../rules/Privatization/Rector/Property/ChangeReadOnlyPropertyWithDefaultValueToConstantRector.php)
```diff
class SomeClass
{
/**
* @var string[]
*/
- private $magicMethods = [
+ private const MAGIC_METHODS = [
'__toString',
'__wakeup',
];
public function run()
{
- foreach ($this->magicMethods as $magicMethod) {
+ foreach (self::MAGIC_METHODS as $magicMethod) {
echo $magicMethod;
}
}
}
```
<br>
### FinalizeClassesWithoutChildrenRector
Finalize every class that has no children
@ -7840,49 +7812,6 @@ return static function (RectorConfig $rectorConfig): void {
<br>
### NewToMethodCallRector
Replaces creating object instances with "new" keyword with factory method.
:wrench: **configure it!**
- class: [`Rector\Transform\Rector\New_\NewToMethodCallRector`](../rules/Transform/Rector/New_/NewToMethodCallRector.php)
```php
<?php
declare(strict_types=1);
use Rector\Config\RectorConfig;
use Rector\Transform\Rector\New_\NewToMethodCallRector;
use Rector\Transform\ValueObject\NewToMethodCall;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(NewToMethodCallRector::class, [
new NewToMethodCall('MyClass', 'MyClassFactory', 'create'),
]);
};
```
```diff
class SomeClass
{
+ /**
+ * @var \MyClassFactory
+ */
+ private $myClassFactory;
+
public function example() {
- new MyClass($argument);
+ $this->myClassFactory->create($argument);
}
}
```
<br>
### NewToStaticCallRector
Change new Object to static call

View File

@ -1,126 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\Rector\New_;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Class_;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Core\Contract\Rector\ConfigurableRectorInterface;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\PropertyToAddCollector;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Rector\Transform\ValueObject\NewToMethodCall;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use RectorPrefix202305\Webmozart\Assert\Assert;
/**
* @see \Rector\Tests\Transform\Rector\New_\NewToMethodCallRector\NewToMethodCallRectorTest
*/
final class NewToMethodCallRector extends AbstractRector implements ConfigurableRectorInterface
{
/**
* @var NewToMethodCall[]
*/
private $newsToMethodCalls = [];
/**
* @readonly
* @var \Rector\CodingStyle\Naming\ClassNaming
*/
private $classNaming;
/**
* @readonly
* @var \Rector\Core\NodeManipulator\PropertyManipulator
*/
private $propertyManipulator;
/**
* @readonly
* @var \Rector\PostRector\Collector\PropertyToAddCollector
*/
private $propertyToAddCollector;
public function __construct(ClassNaming $classNaming, PropertyManipulator $propertyManipulator, PropertyToAddCollector $propertyToAddCollector)
{
$this->classNaming = $classNaming;
$this->propertyManipulator = $propertyManipulator;
$this->propertyToAddCollector = $propertyToAddCollector;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Replaces creating object instances with "new" keyword with factory method.', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function example() {
new MyClass($argument);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
/**
* @var \MyClassFactory
*/
private $myClassFactory;
public function example() {
$this->myClassFactory->create($argument);
}
}
CODE_SAMPLE
, [new NewToMethodCall('MyClass', 'MyClassFactory', 'create')])]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [New_::class];
}
/**
* @param New_ $node
*/
public function refactor(Node $node) : ?Node
{
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (!$class instanceof Class_) {
return null;
}
$className = $this->getName($class);
if (!\is_string($className)) {
return null;
}
foreach ($this->newsToMethodCalls as $newToMethodCall) {
if (!$this->isObjectType($node, $newToMethodCall->getNewObjectType())) {
continue;
}
$serviceObjectType = $newToMethodCall->getServiceObjectType();
if ($className === $serviceObjectType->getClassName()) {
continue;
}
$propertyName = $this->propertyManipulator->resolveExistingClassPropertyNameByType($class, $newToMethodCall->getServiceObjectType());
if ($propertyName === null) {
$serviceObjectType = $newToMethodCall->getServiceObjectType();
$propertyName = $this->classNaming->getShortName($serviceObjectType->getClassName());
$propertyName = \lcfirst($propertyName);
$propertyMetadata = new PropertyMetadata($propertyName, $newToMethodCall->getServiceObjectType(), Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);
}
$propertyFetch = new PropertyFetch(new Variable('this'), $propertyName);
return new MethodCall($propertyFetch, $newToMethodCall->getServiceMethod(), $node->args);
}
return $node;
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration) : void
{
Assert::allIsAOf($configuration, NewToMethodCall::class);
$this->newsToMethodCalls = $configuration;
}
}

View File

@ -1,46 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\Transform\ValueObject;
use PHPStan\Type\ObjectType;
use Rector\Core\Validation\RectorAssert;
final class NewToMethodCall
{
/**
* @readonly
* @var string
*/
private $newType;
/**
* @readonly
* @var string
*/
private $serviceType;
/**
* @readonly
* @var string
*/
private $serviceMethod;
public function __construct(string $newType, string $serviceType, string $serviceMethod)
{
$this->newType = $newType;
$this->serviceType = $serviceType;
$this->serviceMethod = $serviceMethod;
RectorAssert::className($newType);
RectorAssert::className($serviceType);
RectorAssert::methodName($serviceMethod);
}
public function getNewObjectType() : ObjectType
{
return new ObjectType($this->newType);
}
public function getServiceObjectType() : ObjectType
{
return new ObjectType($this->serviceType);
}
public function getServiceMethod() : string
{
return $this->serviceMethod;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '4c782243fdb6b03474faa0076800048cb3ef0de9';
public const PACKAGE_VERSION = '105df5f485c8e3b2e9b75320555a32b5325186b5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-30 13:04:28';
public const RELEASE_DATE = '2023-05-30 13:15:49';
/**
* @var int
*/

View File

@ -212,6 +212,9 @@ final class PropertyManipulator
}
return \false;
}
/**
* @api Used in rector-symfony
*/
public function resolveExistingClassPropertyNameByType(Class_ $class, ObjectType $objectType) : ?string
{
foreach ($class->getProperties() as $property) {

2
vendor/autoload.php vendored
View File

@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93::getLoader();
return ComposerAutoloaderInit2a9d14bbb654e04e95c209b6e7bde4fd::getLoader();

View File

@ -2689,7 +2689,6 @@ return array(
'Rector\\Transform\\Rector\\MethodCall\\ReplaceParentCallByPropertyCallRector' => $baseDir . '/rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewArgToMethodCallRector' => $baseDir . '/rules/Transform/Rector/New_/NewArgToMethodCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewToConstructorInjectionRector' => $baseDir . '/rules/Transform/Rector/New_/NewToConstructorInjectionRector.php',
'Rector\\Transform\\Rector\\New_\\NewToMethodCallRector' => $baseDir . '/rules/Transform/Rector/New_/NewToMethodCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewToStaticCallRector' => $baseDir . '/rules/Transform/Rector/New_/NewToStaticCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToFuncCallRector' => $baseDir . '/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToMethodCallRector' => $baseDir . '/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php',
@ -2704,7 +2703,6 @@ return array(
'Rector\\Transform\\ValueObject\\MethodCallToPropertyFetch' => $baseDir . '/rules/Transform/ValueObject/MethodCallToPropertyFetch.php',
'Rector\\Transform\\ValueObject\\MethodCallToStaticCall' => $baseDir . '/rules/Transform/ValueObject/MethodCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\NewArgToMethodCall' => $baseDir . '/rules/Transform/ValueObject/NewArgToMethodCall.php',
'Rector\\Transform\\ValueObject\\NewToMethodCall' => $baseDir . '/rules/Transform/ValueObject/NewToMethodCall.php',
'Rector\\Transform\\ValueObject\\NewToStaticCall' => $baseDir . '/rules/Transform/ValueObject/NewToStaticCall.php',
'Rector\\Transform\\ValueObject\\ParentClassToTraits' => $baseDir . '/rules/Transform/ValueObject/ParentClassToTraits.php',
'Rector\\Transform\\ValueObject\\PropertyAssignToMethodCall' => $baseDir . '/rules/Transform/ValueObject/PropertyAssignToMethodCall.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93
class ComposerAutoloaderInit2a9d14bbb654e04e95c209b6e7bde4fd
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit2a9d14bbb654e04e95c209b6e7bde4fd', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit034991f4b22807fc1159cd21567fcc93', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit2a9d14bbb654e04e95c209b6e7bde4fd', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit034991f4b22807fc1159cd21567fcc93::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit2a9d14bbb654e04e95c209b6e7bde4fd::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit2a9d14bbb654e04e95c209b6e7bde4fd::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@ -4,7 +4,7 @@
namespace Composer\Autoload;
class ComposerStaticInit034991f4b22807fc1159cd21567fcc93
class ComposerStaticInit2a9d14bbb654e04e95c209b6e7bde4fd
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2931,7 +2931,6 @@ class ComposerStaticInit034991f4b22807fc1159cd21567fcc93
'Rector\\Transform\\Rector\\MethodCall\\ReplaceParentCallByPropertyCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/MethodCall/ReplaceParentCallByPropertyCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewArgToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewArgToMethodCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewToConstructorInjectionRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewToConstructorInjectionRector.php',
'Rector\\Transform\\Rector\\New_\\NewToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewToMethodCallRector.php',
'Rector\\Transform\\Rector\\New_\\NewToStaticCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/New_/NewToStaticCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToFuncCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/StaticCall/StaticCallToFuncCallRector.php',
'Rector\\Transform\\Rector\\StaticCall\\StaticCallToMethodCallRector' => __DIR__ . '/../..' . '/rules/Transform/Rector/StaticCall/StaticCallToMethodCallRector.php',
@ -2946,7 +2945,6 @@ class ComposerStaticInit034991f4b22807fc1159cd21567fcc93
'Rector\\Transform\\ValueObject\\MethodCallToPropertyFetch' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToPropertyFetch.php',
'Rector\\Transform\\ValueObject\\MethodCallToStaticCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/MethodCallToStaticCall.php',
'Rector\\Transform\\ValueObject\\NewArgToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/NewArgToMethodCall.php',
'Rector\\Transform\\ValueObject\\NewToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/NewToMethodCall.php',
'Rector\\Transform\\ValueObject\\NewToStaticCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/NewToStaticCall.php',
'Rector\\Transform\\ValueObject\\ParentClassToTraits' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/ParentClassToTraits.php',
'Rector\\Transform\\ValueObject\\PropertyAssignToMethodCall' => __DIR__ . '/../..' . '/rules/Transform/ValueObject/PropertyAssignToMethodCall.php',
@ -3081,9 +3079,9 @@ class ComposerStaticInit034991f4b22807fc1159cd21567fcc93
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit034991f4b22807fc1159cd21567fcc93::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit2a9d14bbb654e04e95c209b6e7bde4fd::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit2a9d14bbb654e04e95c209b6e7bde4fd::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit2a9d14bbb654e04e95c209b6e7bde4fd::$classMap;
}, null, ClassLoader::class);
}