mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[DowngradePhp80/81] Refactor Downgrade Resource Return to Object to use BooleanOr check (#1193)
* [DowngradePhp80/81] Refactor Downgrade Resource Return to Object to use BooleanOr check * update example * updaet docs * handle the is_resource pass different expr * handle check along with is_resource but different expr * clean up
This commit is contained in:
parent
4815cd9643
commit
85c3848e8b
|
@ -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;
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -85,7 +85,7 @@ class SomeClass
|
|||
{
|
||||
public function run($obj)
|
||||
{
|
||||
is_resource($obj);
|
||||
is_resource($obj) || $obj instanceof \CurlHandle;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
|
|
@ -5,11 +5,15 @@ declare(strict_types=1);
|
|||
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 @@ final class ObjectToResourceReturn
|
|||
/**
|
||||
* @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 @@ final class ObjectToResourceReturn
|
|||
}
|
||||
|
||||
$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 @@ final class ObjectToResourceReturn
|
|||
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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ class SomeClass
|
|||
{
|
||||
public function run($obj)
|
||||
{
|
||||
is_resource($obj);
|
||||
is_resource($obj) || $obj instanceof \finfo;
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
|
Loading…
Reference in New Issue
Block a user