File tree Expand file tree Collapse file tree
rules-tests/TypedCollections/Rector
ClassMethod/CollectionParamTypeSetterToCollectionPropertyRector/Fixture
If_/RemoveIfInstanceofCollectionRector
MethodCall/SetArrayToNewCollectionRector/Fixture
rules/TypedCollections/Rector/ClassMethod Expand file tree Collapse file tree 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 \CollectionParamTypeSetterToCollectionPropertyRector \Fixture ;
6+
7+ use Doctrine \Common \Collections \ArrayCollection ;
8+ use Doctrine \Common \Collections \Collection ;
9+
10+ final class SkipConstructorSetter
11+ {
12+ /**
13+ * @var Collection<int, string>
14+ */
15+ public $ items ;
16+
17+ public function __construct ($ item )
18+ {
19+ $ this ->items = new ArrayCollection ([]);
20+ }
21+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Fixture ;
4+
5+ use Doctrine \Common \Collections \ArrayCollection ;
6+ use Doctrine \Common \Collections \Collection ;
7+ use Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Source \SomeInstance ;
8+
9+ final class SkipNonCollection
10+ {
11+ public Collection $ items ;
12+
13+ public function __construct ($ item )
14+ {
15+ $ this ->items = new ArrayCollection ();
16+ if ($ item instanceof SomeInstance) {
17+ echo 'check ' ;
18+ }
19+ }
20+ }
21+
22+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Source ;
4+
5+ class SomeInstance
6+ {
7+
8+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Fixture ;
4+
5+ use Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Source \ItemType ;
6+ use Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Source \SomeClassWithConstructorCollection ;
7+ use Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Source \SomeClassWithSetter ;
8+
9+ final class NewInstance
10+ {
11+ public function run ()
12+ {
13+ $ someVariable = [new ItemType (), new ItemType ()];
14+
15+ $ someClassWithSetter = new SomeClassWithConstructorCollection ($ someVariable );
16+ }
17+ }
18+
19+ ?>
20+ -----
21+ <?php
22+
23+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Fixture ;
24+
25+ use Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Source \ItemType ;
26+ use Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Source \SomeClassWithConstructorCollection ;
27+ use Rector \Doctrine \Tests \TypedCollections \Rector \MethodCall \SetArrayToNewCollectionRector \Source \SomeClassWithSetter ;
28+
29+ final class NewInstance
30+ {
31+ public function run ()
32+ {
33+ $ someVariable = [new ItemType (), new ItemType ()];
34+
35+ $ someClassWithSetter = new SomeClassWithConstructorCollection (new \Doctrine \Common \Collections \ArrayCollection ($ someVariable ));
36+ }
37+ }
38+
39+ ?>
Original file line number Diff line number Diff line change 77use PhpParser \Node ;
88use PhpParser \Node \Expr ;
99use PhpParser \Node \Expr \Assign ;
10+ use PhpParser \Node \Expr \New_ ;
1011use PhpParser \Node \Expr \PropertyFetch ;
1112use PhpParser \Node \Name ;
1213use PhpParser \Node \Name \FullyQualified ;
@@ -131,6 +132,10 @@ private function isAssignToPropertyFetchCollection(Assign $assign): bool
131132 return false ;
132133 }
133134
135+ if ($ assign ->expr instanceof New_) {
136+ return false ;
137+ }
138+
134139 $ propertyFetchType = $ this ->getType ($ assign ->var );
135140 if (! $ propertyFetchType instanceof ObjectType) {
136141 return false ;
You can’t perform that action at this time.
0 commit comments