From 86d1e78b33acbae2614d0961885329a1beafaef1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 30 May 2023 15:14:05 +0200 Subject: [PATCH] Remove NewToMethodCallRector as unused core and no other extension, niche to use, better handle by PHPStorm --- .../docs/rector_rules_overview.md | 83 +---------- .../Fixture/fixture.php.inc | 34 ----- .../Fixture/fixture2.php.inc | 47 ------ ...erty_exists_via_property_promotion.php.inc | 37 ----- .../NewToMethodCallRectorTest.php | 28 ---- .../NewToMethodCallRector/Source/MyClass.php | 12 -- .../Source/MyClassFactory.php | 13 -- .../config/configured_rule.php | 17 --- .../Rector/New_/NewToMethodCallRector.php | 141 ------------------ .../Transform/ValueObject/NewToMethodCall.php | 36 ----- src/NodeManipulator/PropertyManipulator.php | 3 + 11 files changed, 9 insertions(+), 442 deletions(-) delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture2.php.inc delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/property_exists_via_property_promotion.php.inc delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/NewToMethodCallRectorTest.php delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/Source/MyClass.php delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/Source/MyClassFactory.php delete mode 100644 rules-tests/Transform/Rector/New_/NewToMethodCallRector/config/configured_rule.php delete mode 100644 rules/Transform/Rector/New_/NewToMethodCallRector.php delete mode 100644 rules/Transform/ValueObject/NewToMethodCall.php diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 2f32f572923..0c57e5f16f4 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -1,4 +1,4 @@ -# 393 Rules Overview +# 391 Rules Overview
@@ -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
-### 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; - } - } - } -``` - -
- ### FinalizeClassesWithoutChildrenRector Finalize every class that has no children @@ -7840,49 +7812,6 @@ return static function (RectorConfig $rectorConfig): void {
-### 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 -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); - } - } -``` - -
- ### NewToStaticCallRector Change new Object to static call diff --git a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture.php.inc b/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture.php.inc deleted file mode 100644 index 2ecd6cdff80..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,34 +0,0 @@ - ------ -myClassFactory->create('abcd'); - $class = $this->myClassFactory->create('abcd'); - } -} -?> diff --git a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture2.php.inc b/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture2.php.inc deleted file mode 100644 index ba4614cd794..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/fixture2.php.inc +++ /dev/null @@ -1,47 +0,0 @@ -mySomeFactory = $mySomeFactory; - } - public function default() - { - new MyClass('abcd'); - $class = new MyClass('abcd'); - } -} -?> ------ -mySomeFactory = $mySomeFactory; - } - public function default() - { - $this->mySomeFactory->create('abcd'); - $class = $this->mySomeFactory->create('abcd'); - } -} -?> diff --git a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/property_exists_via_property_promotion.php.inc b/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/property_exists_via_property_promotion.php.inc deleted file mode 100644 index dced16a1f28..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Fixture/property_exists_via_property_promotion.php.inc +++ /dev/null @@ -1,37 +0,0 @@ - ------ -mySomeFactory->create('abcd'); - $class = $this->mySomeFactory->create('abcd'); - } -} -?> diff --git a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/NewToMethodCallRectorTest.php b/rules-tests/Transform/Rector/New_/NewToMethodCallRector/NewToMethodCallRectorTest.php deleted file mode 100644 index b1de9b9e3ea..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/NewToMethodCallRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Source/MyClass.php b/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Source/MyClass.php deleted file mode 100644 index ca82a84ce56..00000000000 --- a/rules-tests/Transform/Rector/New_/NewToMethodCallRector/Source/MyClass.php +++ /dev/null @@ -1,12 +0,0 @@ -ruleWithConfiguration( - NewToMethodCallRector::class, - [new NewToMethodCall(MyClass::class, MyClassFactory::class, 'create')] - ); -}; diff --git a/rules/Transform/Rector/New_/NewToMethodCallRector.php b/rules/Transform/Rector/New_/NewToMethodCallRector.php deleted file mode 100644 index 03d51571996..00000000000 --- a/rules/Transform/Rector/New_/NewToMethodCallRector.php +++ /dev/null @@ -1,141 +0,0 @@ -myClassFactory->create($argument); - } -} -CODE_SAMPLE - , - [new NewToMethodCall('MyClass', 'MyClassFactory', 'create')] - ), - ]); - } - - /** - * @return array> - */ - 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; - } -} diff --git a/rules/Transform/ValueObject/NewToMethodCall.php b/rules/Transform/ValueObject/NewToMethodCall.php deleted file mode 100644 index 8e923edb8bd..00000000000 --- a/rules/Transform/ValueObject/NewToMethodCall.php +++ /dev/null @@ -1,36 +0,0 @@ -newType); - } - - public function getServiceObjectType(): ObjectType - { - return new ObjectType($this->serviceType); - } - - public function getServiceMethod(): string - { - return $this->serviceMethod; - } -} diff --git a/src/NodeManipulator/PropertyManipulator.php b/src/NodeManipulator/PropertyManipulator.php index 91909283dc9..b5564e9f604 100644 --- a/src/NodeManipulator/PropertyManipulator.php +++ b/src/NodeManipulator/PropertyManipulator.php @@ -174,6 +174,9 @@ public function isPropertyChangeableExceptConstructor( return false; } + /** + * @api Used in rector-symfony + */ public function resolveExistingClassPropertyNameByType(Class_ $class, ObjectType $objectType): ?string { foreach ($class->getProperties() as $property) {