Skip to content

Commit 7c59c5c

Browse files
authored
add support for multiple items (#464)
1 parent 8cde557 commit 7c59c5c

3 files changed

Lines changed: 76 additions & 14 deletions

File tree

config/sets/typed-collections-docblocks.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
declare(strict_types=1);
44

5-
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector;
65
use Rector\Config\RectorConfig;
6+
use Rector\Doctrine\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector;
77

88
return static function (RectorConfig $rectorConfig): void {
99
$rectorConfig->rules([
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Fixture;
6+
7+
use Doctrine\Common\Collections\Collection;
8+
use Doctrine\Common\Collections\ArrayCollection;
9+
use Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Source\RandomHouse;
10+
11+
final class MultipleItemsAdded
12+
{
13+
public function getItems(): Collection
14+
{
15+
$collection = new ArrayCollection();
16+
$collection->add(new RandomHouse());
17+
18+
$collection->add(new RandomHouse());
19+
20+
return $collection;
21+
}
22+
}
23+
24+
?>
25+
-----
26+
<?php
27+
28+
declare(strict_types=1);
29+
30+
namespace Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Fixture;
31+
32+
use Doctrine\Common\Collections\Collection;
33+
use Doctrine\Common\Collections\ArrayCollection;
34+
use Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Source\RandomHouse;
35+
36+
final class MultipleItemsAdded
37+
{
38+
/**
39+
* @return \Doctrine\Common\Collections\Collection<int, \Rector\Doctrine\Tests\TypedCollections\Rector\ClassMethod\CollectionDocblockGenericTypeRector\Source\RandomHouse>
40+
*/
41+
public function getItems(): Collection
42+
{
43+
$collection = new ArrayCollection();
44+
$collection->add(new RandomHouse());
45+
46+
$collection->add(new RandomHouse());
47+
48+
return $collection;
49+
}
50+
}
51+
52+
?>

rules/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,7 @@ public function refactor(Node $node): ?ClassMethod
137137
return null;
138138
}
139139

140-
$setTypeClasses = [];
141-
142-
foreach ($collectionAddMethodCalls as $collectionAddMethodCall) {
143-
$setArg = $collectionAddMethodCall->getArgs()[0];
144-
$setType = $this->getType($setArg->value);
145-
146-
if (! isset($setType->getObjectClassNames()[0])) {
147-
continue;
148-
}
149-
150-
$setTypeClasses[] = $setType->getObjectClassNames()[0];
151-
}
152-
140+
$setTypeClasses = $this->resolveSetTypeClasses($collectionAddMethodCalls);
153141
if (count($setTypeClasses) !== 1) {
154142
return null;
155143
}
@@ -175,4 +163,26 @@ public function refactor(Node $node): ?ClassMethod
175163

176164
return null;
177165
}
166+
167+
/**
168+
* @param MethodCall[] $collectionAddMethodCalls
169+
* @return string[]
170+
*/
171+
private function resolveSetTypeClasses(array $collectionAddMethodCalls): array
172+
{
173+
$setTypeClasses = [];
174+
175+
foreach ($collectionAddMethodCalls as $collectionAddMethodCall) {
176+
$setArg = $collectionAddMethodCall->getArgs()[0];
177+
$setType = $this->getType($setArg->value);
178+
179+
if (! isset($setType->getObjectClassNames()[0])) {
180+
continue;
181+
}
182+
183+
$setTypeClasses[] = $setType->getObjectClassNames()[0];
184+
}
185+
186+
return array_unique($setTypeClasses);
187+
}
178188
}

0 commit comments

Comments
 (0)