Skip to content

Commit

Permalink
[DowngradePhp81/80] Skip combine check with is_resource and instanceo…
Browse files Browse the repository at this point in the history
…f Object (#1192)

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

* regenerate docs

* only check after classname comparison

* test for different check
  • Loading branch information
samsonasik authored Nov 8, 2021
1 parent 4ac7fa4 commit 4815cd9
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 5 deletions.
74 changes: 71 additions & 3 deletions build/target-repository/docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 476 Rules Overview
# 480 Rules Overview

<br>

Expand All @@ -24,6 +24,8 @@

- [DowngradePhp54](#downgradephp54) (1)

- [DowngradePhp56](#downgradephp56) (2)

- [DowngradePhp70](#downgradephp70) (11)

- [DowngradePhp71](#downgradephp71) (10)
Expand All @@ -34,9 +36,9 @@

- [DowngradePhp74](#downgradephp74) (11)

- [DowngradePhp80](#downgradephp80) (18)
- [DowngradePhp80](#downgradephp80) (19)

- [DowngradePhp81](#downgradephp81) (1)
- [DowngradePhp81](#downgradephp81) (2)

- [EarlyReturn](#earlyreturn) (11)

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

?>
Original file line number Diff line number Diff line change
@@ -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')) {
}
}
}
40 changes: 38 additions & 2 deletions rules/DowngradePhp81/NodeManipulator/ObjectToResourceReturn.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@

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
) {
}
Expand All @@ -27,11 +34,40 @@ public function refactor(Instanceof_ $instanceof, array $collectionObjectToResou

$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;
}
}

0 comments on commit 4815cd9

Please sign in to comment.