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,14 +182,18 @@ 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
189+ // if (! $condition instanceof FuncCall) {
190+ // return null;
191+ // }
192+
193+ // if ($this->isName($condition, 'is_object') && $this->isInstanceofCollectionType($condition->args[0]->value)) {
194+ // return $ternary->if;
195+ // }
196+
193197 return null ;
194198 }
195199
@@ -201,4 +205,23 @@ private function isInstanceofCollectionType(Expr $expr): bool
201205
202206 return $ this ->collectionTypeDetector ->isCollectionType ($ expr ->expr );
203207 }
208+
209+ private function isIsObjectFuncCallOnCollection (Expr $ expr ): bool
210+ {
211+ if (! $ expr instanceof FuncCall) {
212+ return false ;
213+ }
214+
215+ if ($ expr ->isFirstClassCallable ()) {
216+ return false ;
217+ }
218+
219+ if (! $ this ->isName ($ expr ->name , 'is_object ' )) {
220+ return false ;
221+ }
222+
223+ $ firstArg = $ expr ->getArgs ()[0 ];
224+
225+ return $ this ->collectionTypeDetector ->isCollectionType ($ firstArg ->value );
226+ }
204227}
You can’t perform that action at this time.
0 commit comments