diff --git a/config/set/cakephp/cakephp40.yaml b/config/set/cakephp/cakephp40.yaml index 1200a8e1d822..e880eb7238ed 100644 --- a/config/set/cakephp/cakephp40.yaml +++ b/config/set/cakephp/cakephp40.yaml @@ -69,67 +69,66 @@ services: Cake\View\Helper: initialize: 'void' - Rector\Rector\Typehint\ParentTypehintedArgumentRector: - $typehintForArgumentByMethodAndClass: + Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector: + $typehintForParameterByMethodByClass: Cake\ORM\Behavior: beforeFind: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' buildValidator: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' buildRules: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeRules: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterRules: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeSave: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterSave: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeDelete: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterDelete: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' Cake\ORM\Table: beforeFind: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' buildValidator: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' buildRules: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeRules: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterRules: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeSave: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterSave: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeDelete: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterDelete: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' Cake\Controller\Controller: beforeFilter: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' afterFilter: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeRender: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeRedirect: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' Cake\Controller\Component: shutdown: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' startup: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeFilter: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeRender: - $event: 'Cake\Event\EventInterface' + 0: 'Cake\Event\EventInterface' beforeRedirect: - $event: 'Cake\Event\EventInterface' - + 0: 'Cake\Event\EventInterface' Rector\CakePHP\Rector\MethodCall\RenameMethodCallBasedOnParameterRector: $methodNamesByTypes: diff --git a/config/set/doctrine/doctrine25.yaml b/config/set/doctrine/doctrine25.yaml index a8987829be04..ac41dc25a505 100644 --- a/config/set/doctrine/doctrine25.yaml +++ b/config/set/doctrine/doctrine25.yaml @@ -1,12 +1,12 @@ services: - Rector\Rector\Typehint\ParentTypehintedArgumentRector: - $typehintForArgumentByMethodAndClass: + Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector: + $typehintForParameterByMethodByClass: Doctrine\ORM\Mapping\ClassMetadataFactory: setEntityManager: - em: 'Doctrine\ORM\EntityManagerInterface' + 0: 'Doctrine\ORM\EntityManagerInterface' Doctrine\ORM\Tools\DebugUnitOfWorkListener: dumpIdentityMap: - em: 'Doctrine\ORM\EntityManagerInterface' + 0: 'Doctrine\ORM\EntityManagerInterface' Rector\Rector\Argument\ArgumentRemoverRector: $positionsByMethodNameByClassType: diff --git a/config/set/phpunit/phpunit80.yaml b/config/set/phpunit/phpunit80.yaml index 6201654334a5..9b1df5af1ff8 100644 --- a/config/set/phpunit/phpunit80.yaml +++ b/config/set/phpunit/phpunit80.yaml @@ -3,11 +3,11 @@ imports: services: # https://github.com/rectorphp/rector/issues/1024 - Rector\Rector\Typehint\ParentTypehintedArgumentRector: - $typehintForArgumentByMethodAndClass: + Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector: + $typehintForParameterByMethodByClass: PHPUnit\Framework\TestCase: __construct: - dataName: '' # no type + 2: '' # no type, $dataName Rector\PHPUnit\Rector\MethodCall\SpecificAssertContainsRector: ~ Rector\PHPUnit\Rector\MethodCall\SpecificAssertInternalTypeRector: ~ diff --git a/config/set/sylius/sylius10.yaml b/config/set/sylius/sylius10.yaml index 8d0fa26d7cad..d9a34631ac95 100644 --- a/config/set/sylius/sylius10.yaml +++ b/config/set/sylius/sylius10.yaml @@ -18,11 +18,11 @@ services: 'Sylius\Component\Taxonomy\Model\TaxonInterface': 'getParents ': 'getAncestors' - Rector\Rector\Typehint\ParentTypehintedArgumentRector: - $typehintForArgumentByMethodAndClass: + Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector: + $typehintForParameterByMethodByClass: Sylius\Bundle\CoreBundle\Context\SessionAndChannelBasedCartContext: __construct: - $cartStorage: 'Sylius\Component\Core\Storage\CartStorageInterface' + 0: 'Sylius\Component\Core\Storage\CartStorageInterface' Rector\Rector\Argument\ArgumentAdderRector: $positionWithDefaultValueByMethodNamesByClassTypes: diff --git a/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php b/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php index d54b01b82685..44d5faa96293 100644 --- a/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php +++ b/packages/TypeDeclaration/src/Rector/ClassMethod/AddParamTypeDeclarationRector.php @@ -55,7 +55,6 @@ public function process(string $name) } } PHP - , [ '$typehintForParameterByMethodByClass' => [ 'SomeClass' => [ @@ -141,6 +140,18 @@ private function shouldSkip(ClassMethod $classMethod): bool return ! (bool) $class->extends; } + private function refactorClassMethodWithTypehintByParameterPosition(Node $node, $typehintByParameterPosition): void + { + foreach ($typehintByParameterPosition as $parameterPosition => $type) { + if (! isset($node->params[$parameterPosition])) { + continue; + } + + $parameter = $node->params[$parameterPosition]; + $this->refactorParameter($parameter, $type); + } + } + private function refactorParameter(Param $param, string $newType): void { // already set → no change @@ -161,16 +172,4 @@ private function refactorParameter(Param $param, string $newType): void $param->type = $returnTypeNode; } - - private function refactorClassMethodWithTypehintByParameterPosition(Node $node, $typehintByParameterPosition): void - { - foreach ($typehintByParameterPosition as $parameterPosition => $type) { - if (! isset($node->params[$parameterPosition])) { - continue; - } - - $parameter = $node->params[$parameterPosition]; - $this->refactorParameter($parameter, $type); - } - } } diff --git a/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/AddParamTypeDeclarationRectorTest.php b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/AddParamTypeDeclarationRectorTest.php index b0737c4033dc..638fab772eb3 100644 --- a/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/AddParamTypeDeclarationRectorTest.php +++ b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/AddParamTypeDeclarationRectorTest.php @@ -7,6 +7,8 @@ use Iterator; use Rector\Testing\PHPUnit\AbstractRectorTestCase; use Rector\TypeDeclaration\Rector\ClassMethod\AddParamTypeDeclarationRector; +use Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddParamTypeDeclarationRector\Source\ClassMetadataFactory; +use Rector\TypeDeclaration\Tests\Rector\ClassMethod\AddParamTypeDeclarationRector\Source\ParserInterface; final class AddParamTypeDeclarationRectorTest extends AbstractRectorTestCase { @@ -33,6 +35,16 @@ protected function getRectorsWithConfiguration(): array 0 => 'string', ], ], + ParserInterface::class => [ + 'parse' => [ + 0 => 'string', + ], + ], + ClassMetadataFactory::class => [ + 'setEntityManager' => [ + 0 => 'Doctrine\ORM\EntityManagerInterface', + ], + ], ], ], ]; diff --git a/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture/MyMetadataFactory.php.inc b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture/MyMetadataFactory.php.inc new file mode 100644 index 000000000000..34563b1937f5 --- /dev/null +++ b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture/MyMetadataFactory.php.inc @@ -0,0 +1,33 @@ +em = $emanager; + } +} + +?> +----- +em = $emanager; + } +} + +?> diff --git a/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture/SomeClassImplementingParserInterface.php.inc b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture/SomeClassImplementingParserInterface.php.inc new file mode 100644 index 000000000000..8dcae77a952e --- /dev/null +++ b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Fixture/SomeClassImplementingParserInterface.php.inc @@ -0,0 +1,29 @@ + +----- + diff --git a/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Source/ClassMetadataFactory.php b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Source/ClassMetadataFactory.php new file mode 100644 index 000000000000..a33a08b5eadd --- /dev/null +++ b/packages/TypeDeclaration/tests/Rector/ClassMethod/AddParamTypeDeclarationRector/Source/ClassMetadataFactory.php @@ -0,0 +1,10 @@ + [ - * method => [ - * argument => typehting - * ] - * ] - * - * @var string[][][] - */ - private $typehintForArgumentByMethodAndClass = []; - - /** - * @param mixed[] $typehintForArgumentByMethodAndClass - */ - public function __construct(array $typehintForArgumentByMethodAndClass = []) - { - $this->typehintForArgumentByMethodAndClass = $typehintForArgumentByMethodAndClass; - } - - public function getDefinition(): RectorDefinition - { - return new RectorDefinition('Changes defined parent class typehints.', [ - new ConfiguredCodeSample( - <<<'PHP' -interface SomeInterface -{ - public read(string $content); -} - -class SomeClass implements SomeInterface -{ - public read($content); -} -PHP - , - <<<'PHP' -interface SomeInterface -{ - public read(string $content); -} - -class SomeClass implements SomeInterface -{ - public read(string $content); -} -PHP - , - [ - '$typehintForArgumentByMethodAndClass' => [ - 'SomeInterface' => [ - 'read' => [ - '$content' => 'string', - ], - ], - ], - ] - ), - ]); - } - - /** - * @return string[] - */ - public function getNodeTypes(): array - { - return [ClassMethod::class]; - } - - /** - * @param ClassMethod $node - */ - public function refactor(Node $node): ?Node - { - foreach ($this->typehintForArgumentByMethodAndClass as $type => $methodToArgumentToTypes) { - $classNode = $node->getAttribute(AttributeKey::CLASS_NODE); - if ($classNode === null) { - // anonymous class - continue; - } - - if (! $this->isObjectType($classNode, $type)) { - continue; - } - - $this->processArgumentToTypes($node, $methodToArgumentToTypes); - } - - return null; - } - - /** - * @param string[][] $methodToArgumentToTypes - */ - private function processArgumentToTypes(ClassMethod $classMethod, array $methodToArgumentToTypes): void - { - foreach ($methodToArgumentToTypes as $method => $argumentToTypes) { - if (! $this->isName($classMethod, $method)) { - continue; - } - - $this->processClassMethodNodeWithTypehints($classMethod, $argumentToTypes); - return; - } - } - - /** - * @param string[] $parametersToTypes - */ - private function processClassMethodNodeWithTypehints(ClassMethod $classMethod, array $parametersToTypes): void - { - /** @var Param $param */ - foreach ($classMethod->params as $param) { - foreach ($parametersToTypes as $parameter => $type) { - $parameter = ltrim($parameter, '$'); - - if (! $this->isName($param, $parameter)) { - continue; - } - - $param->type = $type === '' ? null : $this->staticTypeMapper->mapStringToPhpParserNode($type); - } - } - } -} diff --git a/tests/Rector/Typehint/ParentTypehintedArgumentRector/Fixture/MyMetadataFactory.php.inc b/tests/Rector/Typehint/ParentTypehintedArgumentRector/Fixture/MyMetadataFactory.php.inc deleted file mode 100644 index 6d6e5f64680c..000000000000 --- a/tests/Rector/Typehint/ParentTypehintedArgumentRector/Fixture/MyMetadataFactory.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -em = $em; - } -} - -?> ------ -em = $em; - } -} - -?> diff --git a/tests/Rector/Typehint/ParentTypehintedArgumentRector/Fixture/SomeClassImplementingParserInterface.php.inc b/tests/Rector/Typehint/ParentTypehintedArgumentRector/Fixture/SomeClassImplementingParserInterface.php.inc deleted file mode 100644 index 45cf96742f1e..000000000000 --- a/tests/Rector/Typehint/ParentTypehintedArgumentRector/Fixture/SomeClassImplementingParserInterface.php.inc +++ /dev/null @@ -1,29 +0,0 @@ - ------ - diff --git a/tests/Rector/Typehint/ParentTypehintedArgumentRector/ParentTypehintedArgumentRectorTest.php b/tests/Rector/Typehint/ParentTypehintedArgumentRector/ParentTypehintedArgumentRectorTest.php deleted file mode 100644 index 3878be5daab4..000000000000 --- a/tests/Rector/Typehint/ParentTypehintedArgumentRector/ParentTypehintedArgumentRectorTest.php +++ /dev/null @@ -1,50 +0,0 @@ -doTestFile($file); - } - - public function provideDataForTest(): Iterator - { - return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - /** - * @return mixed[] - */ - protected function getRectorsWithConfiguration(): array - { - return [ - ParentTypehintedArgumentRector::class => [ - '$typehintForArgumentByMethodAndClass' => [ - ParserInterface::class => [ - 'parse' => [ - 'code' => 'string', - ], - ], - ClassMetadataFactory::class => [ - 'setEntityManager' => [ - '$em' => 'Doctrine\ORM\EntityManagerInterface', - ], - ], - ], - ], - ]; - } -} diff --git a/tests/Rector/Typehint/ParentTypehintedArgumentRector/Source/ClassMetadataFactory.php b/tests/Rector/Typehint/ParentTypehintedArgumentRector/Source/ClassMetadataFactory.php deleted file mode 100644 index 822c27aeb47d..000000000000 --- a/tests/Rector/Typehint/ParentTypehintedArgumentRector/Source/ClassMetadataFactory.php +++ /dev/null @@ -1,10 +0,0 @@ -