|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\DeadCode\Rector\StmtsAwareInterface; |
| 6 | + |
| 7 | +use PhpParser\Comment\Doc; |
| 8 | +use PhpParser\Node; |
| 9 | +use PhpParser\Node\Expr; |
| 10 | +use PhpParser\Node\Expr\FuncCall; |
| 11 | +use PhpParser\Node\Expr\Instanceof_; |
| 12 | +use PhpParser\Node\Expr\PropertyFetch; |
| 13 | +use PhpParser\Node\Expr\Variable; |
| 14 | +use PhpParser\Node\Name; |
| 15 | +use PhpParser\Node\Stmt\Expression; |
| 16 | +use PhpParser\Node\Stmt\Nop; |
| 17 | +use Rector\PhpParser\Enum\NodeGroup; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 21 | + |
| 22 | +/** |
| 23 | + * @see \Rector\Tests\DeadCode\Rector\StmtsAwareInterface\RemoveDeadInstanceOfAssertRector\RemoveDeadInstanceOfAssertRectorTest |
| 24 | + */ |
| 25 | +final class RemoveDeadInstanceOfAssertRector extends AbstractRector |
| 26 | +{ |
| 27 | + public function getRuleDefinition(): RuleDefinition |
| 28 | + { |
| 29 | + return new RuleDefinition( |
| 30 | + 'Remove assert() with instanceof check on a value whose type is already known', |
| 31 | + [ |
| 32 | + new CodeSample( |
| 33 | + <<<'CODE_SAMPLE' |
| 34 | +final class SomeClass |
| 35 | +{ |
| 36 | + public function __construct( |
| 37 | + private UserRepository $userRepository |
| 38 | + ) { |
| 39 | + } |
| 40 | +
|
| 41 | + public function run(): void |
| 42 | + { |
| 43 | + assert($this->userRepository instanceof UserRepository); |
| 44 | + } |
| 45 | +} |
| 46 | +CODE_SAMPLE |
| 47 | + , |
| 48 | + <<<'CODE_SAMPLE' |
| 49 | +final class SomeClass |
| 50 | +{ |
| 51 | + public function __construct( |
| 52 | + private UserRepository $userRepository |
| 53 | + ) { |
| 54 | + } |
| 55 | +
|
| 56 | + public function run(): void |
| 57 | + { |
| 58 | + } |
| 59 | +} |
| 60 | +CODE_SAMPLE |
| 61 | + ), |
| 62 | + ] |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public function getNodeTypes(): array |
| 67 | + { |
| 68 | + return NodeGroup::STMTS_AWARE; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param StmtsAware $node |
| 73 | + */ |
| 74 | + public function refactor(Node $node): ?Node |
| 75 | + { |
| 76 | + if ($node->stmts === null) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + |
| 80 | + $hasChanged = false; |
| 81 | + |
| 82 | + foreach ($node->stmts as $key => $stmt) { |
| 83 | + if (! $stmt instanceof Expression) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + |
| 87 | + if (! $this->isDeadInstanceOfAssert($stmt->expr)) { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + $docComment = $stmt->getDocComment(); |
| 92 | + if ($docComment instanceof Doc) { |
| 93 | + $nop = new Nop(); |
| 94 | + $nop->setDocComment($docComment); |
| 95 | + $node->stmts[$key] = $nop; |
| 96 | + } else { |
| 97 | + unset($node->stmts[$key]); |
| 98 | + } |
| 99 | + |
| 100 | + $hasChanged = true; |
| 101 | + } |
| 102 | + |
| 103 | + if (! $hasChanged) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + $node->stmts = array_values($node->stmts); |
| 108 | + |
| 109 | + return $node; |
| 110 | + } |
| 111 | + |
| 112 | + private function isDeadInstanceOfAssert(Expr $expr): bool |
| 113 | + { |
| 114 | + if (! $expr instanceof FuncCall) { |
| 115 | + return false; |
| 116 | + } |
| 117 | + |
| 118 | + if ($expr->isFirstClassCallable()) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + |
| 122 | + if (! $this->isName($expr, 'assert')) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + // single-arg assert only; keep asserts that carry a description message |
| 127 | + if (count($expr->getArgs()) !== 1) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + $instanceof = $expr->getArgs()[0] |
| 132 | + ->value; |
| 133 | + if (! $instanceof instanceof Instanceof_) { |
| 134 | + return false; |
| 135 | + } |
| 136 | + |
| 137 | + // only property fetch or variable value |
| 138 | + if (! $instanceof->expr instanceof PropertyFetch && ! $instanceof->expr instanceof Variable) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + if (! $instanceof->class instanceof Name) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + |
| 146 | + $classType = $this->nodeTypeResolver->getType($instanceof->class); |
| 147 | + $exprType = $this->nodeTypeResolver->getType($instanceof->expr); |
| 148 | + if ($classType->equals($exprType)) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + |
| 152 | + return $classType->isSuperTypeOf($exprType) |
| 153 | + ->yes(); |
| 154 | + } |
| 155 | +} |
0 commit comments