[DowngradePhp81/80] Skip combine check with is_resource and instanceof Object (#1192)

* [DowngradePhp81/80] Skip combine check with is_resource and instanceof Object

* regenerate docs

* only check after classname comparison

* test for different check
This commit is contained in:
Abdul Malik Ikhsan 2021-11-09 02:25:53 +07:00 committed by GitHub
parent 4ac7fa44bd
commit 4815cd9643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 148 additions and 5 deletions

View File

@ -1,4 +1,4 @@
# 476 Rules Overview
# 480 Rules Overview
<br>
@ -24,6 +24,8 @@
- [DowngradePhp54](#downgradephp54) (1)
- [DowngradePhp56](#downgradephp56) (2)
- [DowngradePhp70](#downgradephp70) (11)
- [DowngradePhp71](#downgradephp71) (10)
@ -34,9 +36,9 @@
- [DowngradePhp74](#downgradephp74) (11)
- [DowngradePhp80](#downgradephp80) (18)
- [DowngradePhp80](#downgradephp80) (19)
- [DowngradePhp81](#downgradephp81) (1)
- [DowngradePhp81](#downgradephp81) (2)
- [EarlyReturn](#earlyreturn) (11)
@ -4050,6 +4052,34 @@ Remove static from closure
<br>
## DowngradePhp56
### DowngradeExponentialAssignmentOperatorRector
Remove exponential assignment operator **=
- class: [`Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialAssignmentOperatorRector`](../rules/DowngradePhp56/Rector/Pow/DowngradeExponentialAssignmentOperatorRector.php)
```diff
-$a **= 3;
+$a = pow($a, 3);
```
<br>
### DowngradeExponentialOperatorRector
Changes ** (exp) operator to pow(val, val2)
- class: [`Rector\DowngradePhp56\Rector\Pow\DowngradeExponentialOperatorRector`](../rules/DowngradePhp56/Rector/Pow/DowngradeExponentialOperatorRector.php)
```diff
-1**2;
+pow(1, 2);
```
<br>
## DowngradePhp70
### DowngradeAnonymousClassRector
@ -5165,6 +5195,25 @@ Change nullsafe operator to ternary operator rector
<br>
### DowngradePhp80ResourceReturnToObjectRector
change instanceof Object to is_resource
- class: [`Rector\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector`](../rules/DowngradePhp80/Rector/Instanceof_/DowngradePhp80ResourceReturnToObjectRector.php)
```diff
class SomeClass
{
public function run($obj)
{
- $obj instanceof \CurlHandle;
+ is_resource($obj);
}
}
```
<br>
### DowngradePhpTokenRector
`"something()"` will be renamed to `"somethingElse()"`
@ -5386,6 +5435,25 @@ Remove final from class constants
<br>
### DowngradePhp81ResourceReturnToObjectRector
change instanceof Object to is_resource
- class: [`Rector\DowngradePhp81\Rector\Instanceof_\DowngradePhp81ResourceReturnToObjectRector`](../rules/DowngradePhp81/Rector/Instanceof_/DowngradePhp81ResourceReturnToObjectRector.php)
```diff
class SomeClass
{
public function run($obj)
{
- $obj instanceof \finfo;
+ is_resource($obj);
}
}
```
<br>
## EarlyReturn
### ChangeAndIfToEarlyReturnRector

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector\Fixture;
class DifferentCheck
{
public function run($obj)
{
$obj instanceof \CurlHandle && rand(0, 1) === 1;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector\Fixture;
class DifferentCheck
{
public function run($obj)
{
is_resource($obj) && rand(0, 1) === 1;
}
}
?>

View File

@ -0,0 +1,12 @@
<?php
namespace Rector\Tests\DowngradePhp80\Rector\Instanceof_\DowngradePhp80ResourceReturnToObjectRector\Fixture;
class SkipCombineCheckWithIsResource
{
public function run($image)
{
if (!$image instanceof \GdImage && !(\is_resource($image) && \get_resource_type($image) === 'gd')) {
}
}
}

View File

@ -4,14 +4,21 @@ declare(strict_types=1);
namespace Rector\DowngradePhp81\NodeManipulator;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\NodeFactory;
use Rector\NodeNameResolver\NodeNameResolver;
final class ObjectToResourceReturn
{
public function __construct(
private BetterNodeFinder $betterNodeFinder,
private NodeNameResolver $nodeNameResolver,
private NodeFactory $nodeFactory
) {
}
@ -27,11 +34,40 @@ final class ObjectToResourceReturn
$className = $instanceof->class->toString();
foreach ($collectionObjectToResource as $singleCollectionObjectToResource) {
if ($singleCollectionObjectToResource === $className) {
return $this->nodeFactory->createFuncCall('is_resource', [$instanceof->expr]);
if ($singleCollectionObjectToResource !== $className) {
continue;
}
$binaryOp = $this->betterNodeFinder->findParentType($instanceof, BinaryOp::class);
if ($this->hasIsResourceCheck($binaryOp)) {
continue;
}
return $this->nodeFactory->createFuncCall('is_resource', [$instanceof->expr]);
}
return null;
}
private function hasIsResourceCheck(?BinaryOp $binaryOp): bool
{
if ($binaryOp instanceof BinaryOp) {
return (bool) $this->betterNodeFinder->findFirst(
$binaryOp,
function (Node $subNode): bool {
if (! $subNode instanceof FuncCall) {
return false;
}
if (! $subNode->name instanceof Name) {
return false;
}
return $this->nodeNameResolver->isName($subNode->name, 'is_resource');
}
);
}
return false;
}
}