-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
RemoveUselessIsObjectCheckRector.php
62 lines (60 loc) · 2.08 KB
/
RemoveUselessIsObjectCheckRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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\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;
}
}