From 2bc23a1bfadc576311d15c856ed0fe6ae0b1bd7d Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 9 Sep 2025 15:01:27 +0200 Subject: [PATCH] add support for multiple items --- config/sets/typed-collections-docblocks.php | 2 +- .../Fixture/multiple_items_added.php.inc | 52 +++++++++++++++++++ .../CollectionDocblockGenericTypeRector.php | 36 ++++++++----- 3 files changed, 76 insertions(+), 14 deletions(-) create mode 100644 rules-tests/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector/Fixture/multiple_items_added.php.inc diff --git a/config/sets/typed-collections-docblocks.php b/config/sets/typed-collections-docblocks.php index 013cc006..74bb9fc8 100644 --- a/config/sets/typed-collections-docblocks.php +++ b/config/sets/typed-collections-docblocks.php @@ -2,8 +2,8 @@ declare(strict_types=1); -use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector; use Rector\Config\RectorConfig; +use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ diff --git a/rules-tests/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector/Fixture/multiple_items_added.php.inc b/rules-tests/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector/Fixture/multiple_items_added.php.inc new file mode 100644 index 00000000..b205f3d1 --- /dev/null +++ b/rules-tests/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector/Fixture/multiple_items_added.php.inc @@ -0,0 +1,52 @@ +add(new RandomHouse()); + + $collection->add(new RandomHouse()); + + return $collection; + } +} + +?> +----- + + */ + public function getItems(): Collection + { + $collection = new ArrayCollection(); + $collection->add(new RandomHouse()); + + $collection->add(new RandomHouse()); + + return $collection; + } +} + +?> diff --git a/rules/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector.php b/rules/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector.php index d17faaaa..4bdc215d 100644 --- a/rules/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector.php +++ b/rules/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector.php @@ -137,19 +137,7 @@ public function refactor(Node $node): ?ClassMethod return null; } - $setTypeClasses = []; - - foreach ($collectionAddMethodCalls as $collectionAddMethodCall) { - $setArg = $collectionAddMethodCall->getArgs()[0]; - $setType = $this->getType($setArg->value); - - if (! isset($setType->getObjectClassNames()[0])) { - continue; - } - - $setTypeClasses[] = $setType->getObjectClassNames()[0]; - } - + $setTypeClasses = $this->resolveSetTypeClasses($collectionAddMethodCalls); if (count($setTypeClasses) !== 1) { return null; } @@ -175,4 +163,26 @@ public function refactor(Node $node): ?ClassMethod return null; } + + /** + * @param MethodCall[] $collectionAddMethodCalls + * @return string[] + */ + private function resolveSetTypeClasses(array $collectionAddMethodCalls): array + { + $setTypeClasses = []; + + foreach ($collectionAddMethodCalls as $collectionAddMethodCall) { + $setArg = $collectionAddMethodCall->getArgs()[0]; + $setType = $this->getType($setArg->value); + + if (! isset($setType->getObjectClassNames()[0])) { + continue; + } + + $setTypeClasses[] = $setType->getObjectClassNames()[0]; + } + + return array_unique($setTypeClasses); + } }