Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DowngradePhp80/81] Refactor Downgrade Resource Return to Object to use BooleanOr check #1193

Merged
merged 6 commits into from
Nov 8, 2021
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
4 changes: 2 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
```
Expand Down Expand Up @@ -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;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

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

namespace Rector\Tests\DowngradePhp81\Rector\Instanceof_\DowngradePhp81ResourceReturnToObjectRector\Fixture;

class UseOrIsResourceDifferentExpr
{
public function run($obj, $expr)
{
$obj instanceof \finfo && is_resource($expr);
}
}

?>
-----
<?php

namespace Rector\Tests\DowngradePhp81\Rector\Instanceof_\DowngradePhp81ResourceReturnToObjectRector\Fixture;

class UseOrIsResourceDifferentExpr
{
public function run($obj, $expr)
{
(is_resource($obj) || $obj instanceof \finfo) && is_resource($expr);
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class SomeClass
{
public function run($obj)
{
is_resource($obj);
is_resource($obj) || $obj instanceof \CurlHandle;
}
}
CODE_SAMPLE
Expand Down
32 changes: 26 additions & 6 deletions rules/DowngradePhp81/NodeManipulator/ObjectToResourceReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -19,14 +23,15 @@ final class ObjectToResourceReturn
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver,
private NodeComparator $nodeComparator,
private NodeFactory $nodeFactory
) {
}

/**
* @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;
Expand All @@ -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;
}
Expand All @@ -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);
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class SomeClass
{
public function run($obj)
{
is_resource($obj);
is_resource($obj) || $obj instanceof \finfo;
}
}
CODE_SAMPLE
Expand Down