|
| 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\Closure; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\Node\Identifier; |
| 12 | +use PhpParser\Node\Stmt\Return_; |
| 13 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 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\MergeWithCallableAndWillReturnRector\MergeWithCallableAndWillReturnRectorTest |
| 21 | + */ |
| 22 | +final class MergeWithCallableAndWillReturnRector extends AbstractRector |
| 23 | +{ |
| 24 | + public function __construct( |
| 25 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 26 | + private readonly ValueResolver $valueResolver, |
| 27 | + ) { |
| 28 | + } |
| 29 | + |
| 30 | + public function getRuleDefinition(): RuleDefinition |
| 31 | + { |
| 32 | + return new RuleDefinition( |
| 33 | + 'Merge split mocking method ->with($this->callback(...)) and ->willReturn(expr) to single ->willReturnCallback() call', |
| 34 | + [ |
| 35 | + new CodeSample( |
| 36 | + <<<'CODE_SAMPLE' |
| 37 | +use PHPUnit\Framework\TestCase; |
| 38 | +
|
| 39 | +final class SomeTest extends TestCase |
| 40 | +{ |
| 41 | + public function test() |
| 42 | + { |
| 43 | + $this->createMock('SomeClass') |
| 44 | + ->expects($this->once()) |
| 45 | + ->method('someMethod') |
| 46 | + ->with($this->callback(function (array $args): bool { |
| 47 | + return true; |
| 48 | + })) |
| 49 | + ->willReturn(['some item']); |
| 50 | + } |
| 51 | +} |
| 52 | +CODE_SAMPLE |
| 53 | + , |
| 54 | + <<<'CODE_SAMPLE' |
| 55 | +use PHPUnit\Framework\TestCase; |
| 56 | +
|
| 57 | +final class SomeTest extends TestCase |
| 58 | +{ |
| 59 | + public function test() |
| 60 | + { |
| 61 | + $this->createMock('SomeClass') |
| 62 | + ->expects($this->once()) |
| 63 | + ->method('someMethod') |
| 64 | + ->willReturnCallback(function (array $args): array { |
| 65 | + return ['some item']; |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
| 69 | +CODE_SAMPLE |
| 70 | + ), |
| 71 | + ] |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @return array<class-string<MethodCall>> |
| 77 | + */ |
| 78 | + public function getNodeTypes(): array |
| 79 | + { |
| 80 | + return [MethodCall::class]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @param MethodCall $node |
| 85 | + */ |
| 86 | + public function refactor(Node $node): MethodCall|null |
| 87 | + { |
| 88 | + if ($node->isFirstClassCallable()) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 93 | + return null; |
| 94 | + } |
| 95 | + |
| 96 | + if (! $this->isName($node->name, 'willReturn')) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + $parentCaller = $node->var; |
| 101 | + if (! $parentCaller instanceof MethodCall) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + |
| 105 | + if (! $this->isName($parentCaller->name, 'with')) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + |
| 109 | + $willReturnMethodCall = $node; |
| 110 | + |
| 111 | + $withMethodCall = $parentCaller; |
| 112 | + |
| 113 | + $callbackMethodCall = $this->matchFirstArgCallbackMethodCall($withMethodCall); |
| 114 | + if (! $callbackMethodCall instanceof MethodCall) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + $innerClosure = $callbackMethodCall->getArgs()[0] |
| 119 | + ->value; |
| 120 | + if (! $innerClosure instanceof Closure) { |
| 121 | + return null; |
| 122 | + } |
| 123 | + |
| 124 | + if ($innerClosure->stmts === []) { |
| 125 | + return null; |
| 126 | + } |
| 127 | + |
| 128 | + if (! $this->isLastStmtReturnTrue($innerClosure)) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + /** @var Return_ $return */ |
| 133 | + $return = $innerClosure->stmts[count($innerClosure->stmts) - 1]; |
| 134 | + $return->expr = $willReturnMethodCall->getArgs()[0] |
| 135 | + ->value; |
| 136 | + |
| 137 | + $parentCaller->name = new Identifier('willReturnCallback'); |
| 138 | + $parentCaller->args = [new Arg($innerClosure)]; |
| 139 | + |
| 140 | + return $parentCaller; |
| 141 | + } |
| 142 | + |
| 143 | + private function matchFirstArgCallbackMethodCall(MethodCall $withMethodCall): ?MethodCall |
| 144 | + { |
| 145 | + $firstArgValue = $withMethodCall->getArgs()[0] |
| 146 | + ->value; |
| 147 | + if (! $firstArgValue instanceof MethodCall) { |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + if (! $this->isName($firstArgValue->name, 'callback')) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + |
| 155 | + return $firstArgValue; |
| 156 | + } |
| 157 | + |
| 158 | + private function isLastStmtReturnTrue(Closure $closure): bool |
| 159 | + { |
| 160 | + $lastStmt = $closure->stmts[count($closure->stmts) - 1]; |
| 161 | + |
| 162 | + if (! $lastStmt instanceof Return_) { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + if (! $lastStmt->expr instanceof Node) { |
| 167 | + return false; |
| 168 | + } |
| 169 | + |
| 170 | + return $this->valueResolver->isTrue($lastStmt->expr); |
| 171 | + } |
| 172 | +} |
0 commit comments