diff --git a/build/target-repository/docs/rector_rules_overview.md b/build/target-repository/docs/rector_rules_overview.md index 15420bead9d4..468d05abeb09 100644 --- a/build/target-repository/docs/rector_rules_overview.md +++ b/build/target-repository/docs/rector_rules_overview.md @@ -5207,7 +5207,7 @@ change instanceof Object to is_resource public function run($obj) { - $obj instanceof \CurlHandle; -+ is_resource($obj); ++ is_resource($obj) || $obj instanceof \CurlHandle; } } ``` @@ -5447,7 +5447,7 @@ change instanceof Object to is_resource public function run($obj) { - $obj instanceof \finfo; -+ is_resource($obj); ++ is_resource($obj) || $obj instanceof \finfo; } } ``` diff --git a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/different_check.php.inc b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/different_check.php.inc index a84ab89dfdbe..5cd173d71ef1 100644 --- a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/different_check.php.inc +++ b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/different_check.php.inc @@ -20,7 +20,7 @@ class DifferentCheck { public function run($obj) { - is_resource($obj) && rand(0, 1) === 1; + (is_resource($obj) || $obj instanceof \CurlHandle) && rand(0, 1) === 1; } } diff --git a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/some_class.php.inc b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/some_class.php.inc index 8518f6940ba9..f30ea58b4fe8 100644 --- a/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/some_class.php.inc +++ b/rules-tests/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector/Fixture/some_class.php.inc @@ -36,23 +36,23 @@ class SomeClass { public function run($obj) { - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); + is_resource($obj) || $obj instanceof \CurlHandle; + is_resource($obj) || $obj instanceof \CurlMultiHandle; + is_resource($obj) || $obj instanceof \CurlShareHandle; + is_resource($obj) || $obj instanceof \Socket; + is_resource($obj) || $obj instanceof \GdImage; + is_resource($obj) || $obj instanceof \XMLWriter; + is_resource($obj) || $obj instanceof \XMLParser; + is_resource($obj) || $obj instanceof \EnchantBroker; + is_resource($obj) || $obj instanceof \EnchantDictionary; + is_resource($obj) || $obj instanceof \OpenSSLCertificate; + is_resource($obj) || $obj instanceof \OpenSSLCertificateSigningRequest; + is_resource($obj) || $obj instanceof \Shmop; + is_resource($obj) || $obj instanceof \SysvMessageQueue; + is_resource($obj) || $obj instanceof \SysvSemaphore; + is_resource($obj) || $obj instanceof \SysvSharedMemory; + is_resource($obj) || $obj instanceof \InflateContext; + is_resource($obj) || $obj instanceof \DeflateContext; } } diff --git a/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/some_class.php.inc b/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/some_class.php.inc index 201141227d09..5a54b3512af2 100644 --- a/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/some_class.php.inc +++ b/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/some_class.php.inc @@ -28,15 +28,15 @@ class SomeClass { public function run($obj) { - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); - is_resource($obj); + is_resource($obj) || $obj instanceof \finfo; + is_resource($obj) || $obj instanceof \FTP\Connection; + is_resource($obj) || $obj instanceof \IMAP\Connection; + is_resource($obj) || $obj instanceof \PSpell\Config; + is_resource($obj) || $obj instanceof \PSpell\Dictionary; + is_resource($obj) || $obj instanceof \LDAP\Result; + is_resource($obj) || $obj instanceof \LDAP\ResultEntry; + is_resource($obj) || $obj instanceof \PgSql\Result; + is_resource($obj) || $obj instanceof \PgSql\Lob; } } diff --git a/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/use_or_is_resource_different_expr.php.inc b/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/use_or_is_resource_different_expr.php.inc new file mode 100644 index 000000000000..d435cc45ae08 --- /dev/null +++ b/rules-tests/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector/Fixture/use_or_is_resource_different_expr.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector.php b/rules/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector.php index cbc0569b8fa7..dc4b47d69923 100644 --- a/rules/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector.php +++ b/rules/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector.php @@ -85,7 +85,7 @@ class SomeClass { public function run($obj) { - is_resource($obj); + is_resource($obj) || $obj instanceof \CurlHandle; } } CODE_SAMPLE diff --git a/rules/DowngradePhp81/NodeManipulator/ObjectToResourceReturn.php b/rules/DowngradePhp81/NodeManipulator/ObjectToResourceReturn.php index ff852745af13..7cf35c22414d 100644 --- a/rules/DowngradePhp81/NodeManipulator/ObjectToResourceReturn.php +++ b/rules/DowngradePhp81/NodeManipulator/ObjectToResourceReturn.php @@ -5,11 +5,15 @@ namespace Rector\DowngradePhp81\NodeManipulator; use PhpParser\Node; +use PhpParser\Node\Arg; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp; +use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Instanceof_; use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; +use Rector\Core\PhpParser\Comparing\NodeComparator; use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Node\NodeFactory; use Rector\NodeNameResolver\NodeNameResolver; @@ -19,6 +23,7 @@ final class ObjectToResourceReturn public function __construct( private BetterNodeFinder $betterNodeFinder, private NodeNameResolver $nodeNameResolver, + private NodeComparator $nodeComparator, private NodeFactory $nodeFactory ) { } @@ -26,7 +31,7 @@ public function __construct( /** * @param string[] $collectionObjectToResource */ - public function refactor(Instanceof_ $instanceof, array $collectionObjectToResource): ?FuncCall + public function refactor(Instanceof_ $instanceof, array $collectionObjectToResource): ?BooleanOr { if (! $instanceof->class instanceof FullyQualified) { return null; @@ -39,22 +44,25 @@ public function refactor(Instanceof_ $instanceof, array $collectionObjectToResou } $binaryOp = $this->betterNodeFinder->findParentType($instanceof, BinaryOp::class); - if ($this->hasIsResourceCheck($binaryOp)) { + if ($this->hasIsResourceCheck($instanceof->expr, $binaryOp)) { continue; } - return $this->nodeFactory->createFuncCall('is_resource', [$instanceof->expr]); + return new BooleanOr( + $this->nodeFactory->createFuncCall('is_resource', [$instanceof->expr]), + $instanceof + ); } return null; } - private function hasIsResourceCheck(?BinaryOp $binaryOp): bool + private function hasIsResourceCheck(Expr $expr, ?BinaryOp $binaryOp): bool { if ($binaryOp instanceof BinaryOp) { return (bool) $this->betterNodeFinder->findFirst( $binaryOp, - function (Node $subNode): bool { + function (Node $subNode) use ($expr): bool { if (! $subNode instanceof FuncCall) { return false; } @@ -63,7 +71,19 @@ function (Node $subNode): bool { return false; } - return $this->nodeNameResolver->isName($subNode->name, 'is_resource'); + if (! $this->nodeNameResolver->isName($subNode->name, 'is_resource')) { + return false; + } + + if (! isset($subNode->args[0])) { + return false; + } + + if (! $subNode->args[0] instanceof Arg) { + return false; + } + + return $this->nodeComparator->areNodesEqual($subNode->args[0], $expr); } ); } diff --git a/rules/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector.php b/rules/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector.php index 8eca4c436a5e..3eb4ca508751 100644 --- a/rules/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector.php +++ b/rules/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector.php @@ -72,7 +72,7 @@ class SomeClass { public function run($obj) { - is_resource($obj); + is_resource($obj) || $obj instanceof \finfo; } } CODE_SAMPLE