File tree Expand file tree Collapse file tree
rules-tests/TypedCollections/Rector/If_/RemoveIfInstanceofCollectionRector/Fixture/Ternary_
rules/TypedCollections/Rector/If_ Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11<?php
22
3- namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Fixture \Ternary ;
3+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Fixture \Ternary_ ;
44
55use Doctrine \Common \Collections \Collection ;
66
@@ -18,7 +18,7 @@ final class IsObjectToDirect
1818-----
1919<?php
2020
21- namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Fixture \Ternary ;
21+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Fixture \Ternary_ ;
2222
2323use Doctrine \Common \Collections \Collection ;
2424
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Doctrine \Tests \TypedCollections \Rector \If_ \RemoveIfInstanceofCollectionRector \Fixture \Ternary_ ;
4+
5+ final class SkipIsObjectOnAnything
6+ {
7+ public $ items ;
8+
9+ public function someMethod ()
10+ {
11+ return is_object ($ this ->items ) ? 1 : 2 ;
12+ }
13+ }
Original file line number Diff line number Diff line change @@ -182,11 +182,7 @@ private function refactorTernary(Ternary $ternary): ?Expr
182182 $ condition = $ ternary ->cond ;
183183 }
184184
185- if (! $ condition instanceof FuncCall) {
186- return null ;
187- }
188-
189- if ($ this ->isName ($ condition , 'is_object ' )) {
185+ if ($ this ->isIsObjectFuncCallOnCollection ($ condition )) {
190186 return $ ternary ->if ;
191187 }
192188
@@ -201,4 +197,23 @@ private function isInstanceofCollectionType(Expr $expr): bool
201197
202198 return $ this ->collectionTypeDetector ->isCollectionType ($ expr ->expr );
203199 }
200+
201+ private function isIsObjectFuncCallOnCollection (Expr $ expr ): bool
202+ {
203+ if (! $ expr instanceof FuncCall) {
204+ return false ;
205+ }
206+
207+ if ($ expr ->isFirstClassCallable ()) {
208+ return false ;
209+ }
210+
211+ if (! $ this ->isName ($ expr ->name , 'is_object ' )) {
212+ return false ;
213+ }
214+
215+ $ firstArg = $ expr ->getArgs ()[0 ];
216+
217+ return $ this ->collectionTypeDetector ->isCollectionType ($ firstArg ->value );
218+ }
204219}
You can’t perform that action at this time.
0 commit comments