Skip to content

Commit 7207a72

Browse files
authored
skip is_object() on anything (#421)
1 parent aeb1421 commit 7207a72

3 files changed

Lines changed: 35 additions & 7 deletions

File tree

rules-tests/TypedCollections/Rector/If_/RemoveIfInstanceofCollectionRector/Fixture/Ternary_/is_object_to_direct.php.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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

55
use 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

2323
use Doctrine\Common\Collections\Collection;
2424

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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+
}

rules/TypedCollections/Rector/If_/RemoveIfInstanceofCollectionRector.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff 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
}

0 commit comments

Comments
 (0)