|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr; |
| 10 | +use PhpParser\Node\Expr\Closure; |
| 11 | +use PhpParser\Node\Expr\Instanceof_; |
| 12 | +use PhpParser\Node\Expr\MethodCall; |
| 13 | +use PhpParser\Node\Name; |
| 14 | +use PhpParser\Node\Stmt\Expression; |
| 15 | +use PhpParser\Node\Stmt\Return_; |
| 16 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 17 | +use Rector\Rector\AbstractRector; |
| 18 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 20 | + |
| 21 | +/** |
| 22 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\SimplerWithIsInstanceOfRector\SimplerWithIsInstanceOfRectorTest |
| 23 | + */ |
| 24 | +final class SimplerWithIsInstanceOfRector extends AbstractRector |
| 25 | +{ |
| 26 | + public function __construct( |
| 27 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
| 28 | + ) { |
| 29 | + } |
| 30 | + |
| 31 | + public function getRuleDefinition(): RuleDefinition |
| 32 | + { |
| 33 | + return new RuleDefinition( |
| 34 | + 'Replaces use of with, callable and instance assert to simple isInstanceOf() method', |
| 35 | + [ |
| 36 | + new CodeSample( |
| 37 | + <<<'CODE_SAMPLE' |
| 38 | +use PHPUnit\Framework\TestCase |
| 39 | +
|
| 40 | +final class SomeClass extends TestCase |
| 41 | +{ |
| 42 | + public function test() |
| 43 | + { |
| 44 | + $someMock = $this->createMock(SomeClass::class) |
| 45 | + ->method('someMethod') |
| 46 | + ->with($this->callable(function ($arg): bool { |
| 47 | + return $arg instanceof SomeType; |
| 48 | + })); |
| 49 | + } |
| 50 | +} |
| 51 | +CODE_SAMPLE |
| 52 | + , |
| 53 | + <<<'CODE_SAMPLE' |
| 54 | +use PHPUnit\Framework\TestCase; |
| 55 | +
|
| 56 | +final class SomeClass extends TestCase |
| 57 | +{ |
| 58 | + public function test() |
| 59 | + { |
| 60 | + $someMock = $this->createMock(SomeClass::class) |
| 61 | + ->method('someMethod') |
| 62 | + ->with($this->isInstanceOf(SomeType::class)); |
| 63 | + } |
| 64 | +} |
| 65 | +CODE_SAMPLE |
| 66 | + ), |
| 67 | + ] |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function getNodeTypes(): array |
| 72 | + { |
| 73 | + return [MethodCall::class]; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @param MethodCall $node |
| 78 | + */ |
| 79 | + public function refactor(Node $node): MethodCall|null |
| 80 | + { |
| 81 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 82 | + return null; |
| 83 | + } |
| 84 | + |
| 85 | + if ($node->isFirstClassCallable()) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + if (! $this->isName($node->name, 'with')) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + |
| 93 | + $withFirstArgValue = $node->getArgs()[0] |
| 94 | + ->value; |
| 95 | + if (! $withFirstArgValue instanceof MethodCall || ! $this->isName($withFirstArgValue->name, 'callback')) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + $callableMethodCall = $withFirstArgValue; |
| 100 | + $callableFirstArgValue = $callableMethodCall->getArgs()[0] |
| 101 | + ->value; |
| 102 | + |
| 103 | + $innerClosure = $callableFirstArgValue; |
| 104 | + if (! $innerClosure instanceof Closure) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $instanceCheckedClassName = $this->matchSoleInstanceofCheckClassName($innerClosure); |
| 109 | + |
| 110 | + if (! $instanceCheckedClassName instanceof Node) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + // convert name to expr |
| 115 | + if ($instanceCheckedClassName instanceof Name) { |
| 116 | + $instanceCheckedClassName = $this->nodeFactory->createClassConstFetch( |
| 117 | + $instanceCheckedClassName->toString(), |
| 118 | + 'class' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + $node->args = [ |
| 123 | + new Arg($this->nodeFactory->createMethodCall('this', 'isInstanceOf', [$instanceCheckedClassName])), |
| 124 | + ]; |
| 125 | + |
| 126 | + return $node; |
| 127 | + } |
| 128 | + |
| 129 | + private function matchSoleInstanceofCheckClassName(Closure $innerClosure): Node|null|Expr|Name |
| 130 | + { |
| 131 | + // return + instancecheck only |
| 132 | + $innerClosureStmts = $innerClosure->getStmts(); |
| 133 | + if (count($innerClosureStmts) === 2) { |
| 134 | + if (! $innerClosureStmts[1] instanceof Return_) { |
| 135 | + return null; |
| 136 | + } |
| 137 | + |
| 138 | + $firstStmt = $innerClosureStmts[0]; |
| 139 | + if (! $firstStmt instanceof Expression) { |
| 140 | + return null; |
| 141 | + } |
| 142 | + |
| 143 | + $firstStmtExpr = $firstStmt->expr; |
| 144 | + if (! $firstStmtExpr instanceof MethodCall) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + if (! $this->isName($firstStmtExpr->name, 'assertInstanceOf')) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + return $firstStmtExpr->getArgs()[0] |
| 153 | + ->value; |
| 154 | + } |
| 155 | + |
| 156 | + if (count($innerClosureStmts) === 1) { |
| 157 | + $onlyStmt = $innerClosureStmts[0]; |
| 158 | + if (! $onlyStmt instanceof Return_) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + |
| 162 | + $returnExpr = $onlyStmt->expr; |
| 163 | + if (! $returnExpr instanceof Instanceof_) { |
| 164 | + return null; |
| 165 | + } |
| 166 | + |
| 167 | + $instanceofExpr = $returnExpr; |
| 168 | + if (! $instanceofExpr->class instanceof Name) { |
| 169 | + return null; |
| 170 | + } |
| 171 | + |
| 172 | + return $instanceofExpr->class; |
| 173 | + } |
| 174 | + |
| 175 | + return null; |
| 176 | + |
| 177 | + } |
| 178 | +} |
0 commit comments