|
| 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\Expr; |
| 9 | +use PhpParser\Node\Expr\Closure; |
| 10 | +use PhpParser\Node\Expr\ConstFetch; |
| 11 | +use PhpParser\Node\Expr\MethodCall; |
| 12 | +use PhpParser\Node\Stmt\Expression; |
| 13 | +use PhpParser\Node\Stmt\Return_; |
| 14 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 15 | +use Rector\Rector\AbstractRector; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 17 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 18 | + |
| 19 | +/** |
| 20 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\CallbackSingleAssertToSimplerRector\CallbackSingleAssertToSimplerRectorTest |
| 21 | + */ |
| 22 | +final class CallbackSingleAssertToSimplerRector extends AbstractRector |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + public function getRuleDefinition(): RuleDefinition |
| 30 | + { |
| 31 | + return new RuleDefinition( |
| 32 | + 'Replaces use of with, callback and sole assertSame() to simple equalTo() matcher', |
| 33 | + [ |
| 34 | + new CodeSample( |
| 35 | + <<<'CODE_SAMPLE' |
| 36 | +use PHPUnit\Framework\TestCase; |
| 37 | +
|
| 38 | +final class SomeClass extends TestCase |
| 39 | +{ |
| 40 | + public function test() |
| 41 | + { |
| 42 | + $builder->expects($this->exactly(2)) |
| 43 | + ->method('add') |
| 44 | + ->with($this->callback(function ($type): bool { |
| 45 | + $this->assertSame(TextType::class, $type); |
| 46 | +
|
| 47 | + return true; |
| 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 | + $builder->expects($this->exactly(2)) |
| 61 | + ->method('add') |
| 62 | + ->with($this->equalTo(TextType::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 | + $hasChanged = false; |
| 94 | + |
| 95 | + foreach ($node->getArgs() as $arg) { |
| 96 | + $expectedExpr = $this->matchCallbackSoleAssertSameExpected($arg->value); |
| 97 | + if (! $expectedExpr instanceof Expr) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $arg->value = $this->nodeFactory->createMethodCall('this', 'equalTo', [$expectedExpr]); |
| 102 | + $hasChanged = true; |
| 103 | + } |
| 104 | + |
| 105 | + if (! $hasChanged) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + return $node; |
| 110 | + } |
| 111 | + |
| 112 | + private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr |
| 113 | + { |
| 114 | + if (! $expr instanceof MethodCall || ! $this->isName($expr->name, 'callback')) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + $callbackArgs = $expr->getArgs(); |
| 119 | + if ($callbackArgs === []) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + |
| 123 | + $innerClosure = $callbackArgs[0]->value; |
| 124 | + if (! $innerClosure instanceof Closure) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + // exactly assertSame() expression + "return true;" |
| 129 | + $closureStmts = $innerClosure->getStmts(); |
| 130 | + if (count($closureStmts) !== 2) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + [$firstStmt, $secondStmt] = $closureStmts; |
| 135 | + |
| 136 | + if (! $firstStmt instanceof Expression) { |
| 137 | + return null; |
| 138 | + } |
| 139 | + |
| 140 | + $firstStmtExpr = $firstStmt->expr; |
| 141 | + if (! $firstStmtExpr instanceof MethodCall || ! $this->isName($firstStmtExpr->name, 'assertSame')) { |
| 142 | + return null; |
| 143 | + } |
| 144 | + |
| 145 | + if (! $secondStmt instanceof Return_ || ! $this->isTrueReturn($secondStmt)) { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + return $firstStmtExpr->getArgs()[0] |
| 150 | + ->value; |
| 151 | + } |
| 152 | + |
| 153 | + private function isTrueReturn(Return_ $return): bool |
| 154 | + { |
| 155 | + return $return->expr instanceof ConstFetch && $this->isName($return->expr, 'true'); |
| 156 | + } |
| 157 | +} |
0 commit comments