File tree Expand file tree Collapse file tree 5 files changed +95
-0
lines changed
rules-tests/TypedCollections/Rector
ClassMethod/CollectionParamTypeSetterToCollectionPropertyRector/Fixture
If_/RemoveIfInstanceofCollectionRector
MethodCall/SetArrayToNewCollectionRector/Fixture
rules/TypedCollections/Rector/ClassMethod Expand file tree Collapse file tree 5 files changed +95
-0
lines changed 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 7
7
use PhpParser \Node ;
8
8
use PhpParser \Node \Expr ;
9
9
use PhpParser \Node \Expr \Assign ;
10
+ use PhpParser \Node \Expr \New_ ;
10
11
use PhpParser \Node \Expr \PropertyFetch ;
11
12
use PhpParser \Node \Name ;
12
13
use PhpParser \Node \Name \FullyQualified ;
@@ -131,6 +132,10 @@ private function isAssignToPropertyFetchCollection(Assign $assign): bool
131
132
return false ;
132
133
}
133
134
135
+ if ($ assign ->expr instanceof New_) {
136
+ return false ;
137
+ }
138
+
134
139
$ propertyFetchType = $ this ->getType ($ assign ->var );
135
140
if (! $ propertyFetchType instanceof ObjectType) {
136
141
return false ;
You can’t perform that action at this time.
0 commit comments