Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture\Ternary;
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture\Ternary_;

use Doctrine\Common\Collections\Collection;

Expand All @@ -18,7 +18,7 @@ final class IsObjectToDirect
-----
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture\Ternary;
namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture\Ternary_;

use Doctrine\Common\Collections\Collection;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Doctrine\Tests\TypedCollections\Rector\If_\RemoveIfInstanceofCollectionRector\Fixture\Ternary_;

final class SkipIsObjectOnAnything
{
public $items;

public function someMethod()
{
return is_object($this->items) ? 1 : 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,7 @@ private function refactorTernary(Ternary $ternary): ?Expr
$condition = $ternary->cond;
}

if (! $condition instanceof FuncCall) {
return null;
}

if ($this->isName($condition, 'is_object')) {
if ($this->isIsObjectFuncCallOnCollection($condition)) {
return $ternary->if;
}

Expand All @@ -201,4 +197,23 @@ private function isInstanceofCollectionType(Expr $expr): bool

return $this->collectionTypeDetector->isCollectionType($expr->expr);
}

private function isIsObjectFuncCallOnCollection(Expr $expr): bool
{
if (! $expr instanceof FuncCall) {
return false;
}

if ($expr->isFirstClassCallable()) {
return false;
}

if (! $this->isName($expr->name, 'is_object')) {
return false;
}

$firstArg = $expr->getArgs()[0];

return $this->collectionTypeDetector->isCollectionType($firstArg->value);
}
}