|
| 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\Closure; |
| 9 | +use PhpParser\Node\Expr\ConstFetch; |
| 10 | +use PhpParser\Node\Expr\MethodCall; |
| 11 | +use PhpParser\Node\Stmt\Return_; |
| 12 | +use Rector\PhpParser\Node\BetterNodeFinder; |
| 13 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 14 | +use Rector\Rector\AbstractRector; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 16 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 17 | + |
| 18 | +/** |
| 19 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\WillReturnCallbackFallbackToReturnFalseRector\WillReturnCallbackFallbackToReturnFalseRectorTest |
| 20 | + */ |
| 21 | +final class WillReturnCallbackFallbackToReturnFalseRector extends AbstractRector |
| 22 | +{ |
| 23 | + public function __construct( |
| 24 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 25 | + private readonly BetterNodeFinder $betterNodeFinder, |
| 26 | + ) { |
| 27 | + } |
| 28 | + |
| 29 | + public function getRuleDefinition(): RuleDefinition |
| 30 | + { |
| 31 | + return new RuleDefinition( |
| 32 | + 'Add a "return false" fallback to a willReturnCallback() closure that only returns booleans, but can fall through without a return', |
| 33 | + [ |
| 34 | + new CodeSample( |
| 35 | + <<<'CODE_SAMPLE' |
| 36 | +use PHPUnit\Framework\TestCase; |
| 37 | +
|
| 38 | +final class SomeTest extends TestCase |
| 39 | +{ |
| 40 | + public function test() |
| 41 | + { |
| 42 | + $matcher = $this->exactly(2); |
| 43 | +
|
| 44 | + $this->eventMock->expects($matcher) |
| 45 | + ->method('checkContext') |
| 46 | + ->willReturnCallback(function (...$parameters) use ($matcher) { |
| 47 | + if ($matcher->numberOfInvocations() === 1) { |
| 48 | + return true; |
| 49 | + } |
| 50 | + if ($matcher->numberOfInvocations() === 2) { |
| 51 | + return false; |
| 52 | + } |
| 53 | + }); |
| 54 | + } |
| 55 | +} |
| 56 | +CODE_SAMPLE |
| 57 | + , |
| 58 | + <<<'CODE_SAMPLE' |
| 59 | +use PHPUnit\Framework\TestCase; |
| 60 | +
|
| 61 | +final class SomeTest extends TestCase |
| 62 | +{ |
| 63 | + public function test() |
| 64 | + { |
| 65 | + $matcher = $this->exactly(2); |
| 66 | +
|
| 67 | + $this->eventMock->expects($matcher) |
| 68 | + ->method('checkContext') |
| 69 | + ->willReturnCallback(function (...$parameters) use ($matcher) { |
| 70 | + if ($matcher->numberOfInvocations() === 1) { |
| 71 | + return true; |
| 72 | + } |
| 73 | + if ($matcher->numberOfInvocations() === 2) { |
| 74 | + return false; |
| 75 | + } |
| 76 | +
|
| 77 | + return false; |
| 78 | + }); |
| 79 | + } |
| 80 | +} |
| 81 | +CODE_SAMPLE |
| 82 | + ), |
| 83 | + ] |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return array<class-string<Node>> |
| 89 | + */ |
| 90 | + public function getNodeTypes(): array |
| 91 | + { |
| 92 | + return [MethodCall::class]; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param MethodCall $node |
| 97 | + */ |
| 98 | + public function refactor(Node $node): ?MethodCall |
| 99 | + { |
| 100 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if (! $this->isName($node->name, 'willReturnCallback')) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + if ($node->isFirstClassCallable()) { |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + if (count($node->getArgs()) !== 1) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + $closure = $node->getArgs()[0] |
| 117 | + ->value; |
| 118 | + if (! $closure instanceof Closure) { |
| 119 | + return null; |
| 120 | + } |
| 121 | + |
| 122 | + if ($closure->stmts === []) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + |
| 126 | + // already ends with an explicit return, no fallback needed |
| 127 | + $lastStmt = $closure->stmts[count($closure->stmts) - 1]; |
| 128 | + if ($lastStmt instanceof Return_) { |
| 129 | + return null; |
| 130 | + } |
| 131 | + |
| 132 | + if (! $this->hasOnlyBoolReturns($closure)) { |
| 133 | + return null; |
| 134 | + } |
| 135 | + |
| 136 | + $closure->stmts[] = new Return_($this->nodeFactory->createFalse()); |
| 137 | + |
| 138 | + return $node; |
| 139 | + } |
| 140 | + |
| 141 | + private function hasOnlyBoolReturns(Closure $closure): bool |
| 142 | + { |
| 143 | + /** @var Return_[] $returns */ |
| 144 | + $returns = $this->betterNodeFinder->findInstancesOfScoped($closure->stmts, [Return_::class]); |
| 145 | + if ($returns === []) { |
| 146 | + return false; |
| 147 | + } |
| 148 | + |
| 149 | + foreach ($returns as $return) { |
| 150 | + if (! $return->expr instanceof ConstFetch) { |
| 151 | + return false; |
| 152 | + } |
| 153 | + |
| 154 | + if (! $this->isNames($return->expr->name, ['true', 'false'])) { |
| 155 | + return false; |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return true; |
| 160 | + } |
| 161 | +} |
0 commit comments