-
-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add RemoveUselessIsObjectCheckRector (#5316)
Co-authored-by: GitHub Action <actions@github.com>
- Loading branch information
1 parent
be924be
commit ea5b331
Showing
9 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...oleanAnd/RemoveUselessIsObjectCheckRector/Fixture/left_instanceof_right_is_object.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class LeftInstanceofRightIsObject | ||
{ | ||
public function run($obj) | ||
{ | ||
return $obj instanceof \DateTime && is_object($obj); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class LeftInstanceofRightIsObject | ||
{ | ||
public function run($obj) | ||
{ | ||
return $obj instanceof \DateTime; | ||
} | ||
} | ||
|
||
?> |
27 changes: 27 additions & 0 deletions
27
...oleanAnd/RemoveUselessIsObjectCheckRector/Fixture/left_is_object_right_instanceof.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class LeftIsObjectRightInstanceof | ||
{ | ||
public function run($obj) | ||
{ | ||
return is_object($obj) && $obj instanceof \DateTime; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class LeftIsObjectRightInstanceof | ||
{ | ||
public function run($obj) | ||
{ | ||
return $obj instanceof \DateTime; | ||
} | ||
} | ||
|
||
?> |
11 changes: 11 additions & 0 deletions
11
...ty/Rector/BooleanAnd/RemoveUselessIsObjectCheckRector/Fixture/skip_different_expr.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class SkipDifferentExpr | ||
{ | ||
public function run($obj, $other) | ||
{ | ||
return is_object($obj) && $other instanceof \DateTime; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...y/Rector/BooleanAnd/RemoveUselessIsObjectCheckRector/Fixture/skip_different_expr2.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class SkipDifferentExpr2 | ||
{ | ||
public function run($obj, $other) | ||
{ | ||
return $other instanceof \DateTime && is_object($obj); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...y/Rector/BooleanAnd/RemoveUselessIsObjectCheckRector/Fixture/skip_different_expr3.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\Fixture; | ||
|
||
final class SkipDifferentExpr3 | ||
{ | ||
public function run($obj) | ||
{ | ||
return is_object($obj) && count($obj->data) === 1; | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ctor/BooleanAnd/RemoveUselessIsObjectCheckRector/RemoveUselessIsObjectCheckRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class RemoveUselessIsObjectCheckRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...CodeQuality/Rector/BooleanAnd/RemoveUselessIsObjectCheckRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(RemoveUselessIsObjectCheckRector::class); | ||
}; |
79 changes: 79 additions & 0 deletions
79
rules/CodeQuality/Rector/BooleanAnd/RemoveUselessIsObjectCheckRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\BooleanAnd; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanAnd; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Expr\Instanceof_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\BooleanAnd\RemoveUselessIsObjectCheckRector\RemoveUselessIsObjectCheckRectorTest | ||
*/ | ||
final class RemoveUselessIsObjectCheckRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Remove useless is_object() check on combine with instanceof check', | ||
[new CodeSample('is_object($obj) && $obj instanceof DateTime', '$obj instanceof DateTime')] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [BooleanAnd::class]; | ||
} | ||
|
||
/** | ||
* @param BooleanAnd $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node->left instanceof FuncCall | ||
&& $this->isName($node->left, 'is_object') | ||
&& $node->right instanceof Instanceof_) { | ||
return $this->processRemoveUselessIsObject($node->left, $node->right); | ||
} | ||
|
||
if (! $node->left instanceof Instanceof_) { | ||
return null; | ||
} | ||
|
||
if (! $node->right instanceof FuncCall) { | ||
return null; | ||
} | ||
|
||
if (! $this->isName($node->right, 'is_object')) { | ||
return null; | ||
} | ||
|
||
return $this->processRemoveUselessIsObject($node->right, $node->left); | ||
} | ||
|
||
private function processRemoveUselessIsObject(FuncCall $funcCall, Instanceof_ $instanceof): ?Instanceof_ | ||
{ | ||
if ($funcCall->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
$args = $funcCall->getArgs(); | ||
if (! isset($args[0])) { | ||
return null; | ||
} | ||
|
||
if (! $this->nodeComparator->areNodesEqual($args[0]->value, $instanceof->expr)) { | ||
return null; | ||
} | ||
|
||
return $instanceof; | ||
} | ||
} |