File tree Expand file tree Collapse file tree
rules-tests/TypedCollections/Rector/ClassMethod/CollectionDocblockGenericTypeRector/Fixture
rules/TypedCollections/Rector/ClassMethod Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33declare (strict_types=1 );
44
5- use Rector \Doctrine \TypedCollections \Rector \ClassMethod \CollectionDocblockGenericTypeRector ;
65use Rector \Config \RectorConfig ;
6+ use Rector \Doctrine \TypedCollections \Rector \ClassMethod \CollectionDocblockGenericTypeRector ;
77
88return static function (RectorConfig $ rectorConfig ): void {
99 $ rectorConfig ->rules ([
Original file line number Diff line number Diff line change 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+ ?>
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments