|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\CodeQuality\Rector\FuncCall; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr; |
| 10 | +use PhpParser\Node\Expr\BinaryOp\Equal; |
| 11 | +use PhpParser\Node\Expr\BinaryOp\Identical; |
| 12 | +use PhpParser\Node\Expr\BinaryOp\NotIdentical; |
| 13 | +use PhpParser\Node\Expr\Cast\Bool_; |
| 14 | +use PhpParser\Node\Expr\ClassConstFetch; |
| 15 | +use PhpParser\Node\Expr\FuncCall; |
| 16 | +use PhpParser\Node\Expr\Instanceof_; |
| 17 | +use PhpParser\Node\Expr\MethodCall; |
| 18 | +use PhpParser\Node\Expr\StaticCall; |
| 19 | +use PhpParser\Node\Expr\Variable; |
| 20 | +use PhpParser\Node\Name\FullyQualified; |
| 21 | +use PHPStan\Reflection\ClassReflection; |
| 22 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 23 | +use Rector\PHPStan\ScopeFetcher; |
| 24 | +use Rector\PHPUnit\Enum\AssertMethod; |
| 25 | +use Rector\PHPUnit\Enum\PHPUnitClassName; |
| 26 | +use Rector\Rector\AbstractRector; |
| 27 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 28 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 29 | + |
| 30 | +/** |
| 31 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\FuncCall\AssertFuncCallToPHPUnitAssertRector\AssertFuncCallToPHPUnitAssertRectorTest |
| 32 | + */ |
| 33 | +final class AssertFuncCallToPHPUnitAssertRector extends AbstractRector |
| 34 | +{ |
| 35 | + public function __construct( |
| 36 | + private readonly ValueResolver $valueResolver |
| 37 | + ) { |
| 38 | + } |
| 39 | + |
| 40 | + public function getRuleDefinition(): RuleDefinition |
| 41 | + { |
| 42 | + return new RuleDefinition('Turns assert() calls to their explicit PHPUnit assert alternative', [ |
| 43 | + new CodeSample('assert($value === 100, "message");', '$this->assertSame(100, $value, "message");'), |
| 44 | + ]); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @return class-string[] |
| 49 | + */ |
| 50 | + public function getNodeTypes(): array |
| 51 | + { |
| 52 | + return [FuncCall::class]; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param FuncCall $node |
| 57 | + */ |
| 58 | + public function refactor(Node $node): StaticCall|MethodCall|null |
| 59 | + { |
| 60 | + if ($node->isFirstClassCallable()) { |
| 61 | + return null; |
| 62 | + } |
| 63 | + |
| 64 | + if (! $this->isName($node, 'assert')) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + if (! $this->isTestFilePath($node) && ! $this->isBehatContext($node)) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + $comparedExpr = $node->getArgs()[0] |
| 73 | + ->value; |
| 74 | + |
| 75 | + if ($comparedExpr instanceof Equal) { |
| 76 | + $methodName = AssertMethod::ASSERT_EQUALS; |
| 77 | + $exprs = [$comparedExpr->right, $comparedExpr->left]; |
| 78 | + |
| 79 | + } elseif ($comparedExpr instanceof Identical) { |
| 80 | + $methodName = AssertMethod::ASSERT_SAME; |
| 81 | + $exprs = [$comparedExpr->right, $comparedExpr->left]; |
| 82 | + |
| 83 | + } elseif ($comparedExpr instanceof NotIdentical) { |
| 84 | + if ($this->valueResolver->isNull($comparedExpr->right)) { |
| 85 | + $methodName = 'assertNotNull'; |
| 86 | + $exprs = [$comparedExpr->left]; |
| 87 | + } else { |
| 88 | + return null; |
| 89 | + } |
| 90 | + |
| 91 | + } elseif ($comparedExpr instanceof Bool_) { |
| 92 | + $methodName = 'assertTrue'; |
| 93 | + $exprs = [$comparedExpr]; |
| 94 | + } elseif ($comparedExpr instanceof FuncCall) { |
| 95 | + if ($this->isName($comparedExpr, 'method_exists')) { |
| 96 | + $methodName = 'assertTrue'; |
| 97 | + $exprs = [$comparedExpr]; |
| 98 | + } else { |
| 99 | + return null; |
| 100 | + } |
| 101 | + } elseif ($comparedExpr instanceof Instanceof_) { |
| 102 | + // outside TestCase |
| 103 | + $methodName = 'assertInstanceOf'; |
| 104 | + $exprs = []; |
| 105 | + |
| 106 | + if ($comparedExpr->class instanceof FullyQualified) { |
| 107 | + $classConstFetch = new ClassConstFetch($comparedExpr->class, 'class'); |
| 108 | + $exprs[] = $classConstFetch; |
| 109 | + } else { |
| 110 | + return null; |
| 111 | + } |
| 112 | + |
| 113 | + $exprs[] = $comparedExpr->expr; |
| 114 | + } else { |
| 115 | + return null; |
| 116 | + } |
| 117 | + |
| 118 | + // is there a comment message |
| 119 | + if (isset($node->getArgs()[1])) { |
| 120 | + $exprs[] = $node->getArgs()[1]->value; |
| 121 | + } |
| 122 | + |
| 123 | + return $this->createCall($node, $methodName, $exprs); |
| 124 | + } |
| 125 | + |
| 126 | + private function isBehatContext(FuncCall $funcCall): bool |
| 127 | + { |
| 128 | + $scope = ScopeFetcher::fetch($funcCall); |
| 129 | + if (! $scope->getClassReflection() instanceof ClassReflection) { |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + $className = $scope->getClassReflection() |
| 134 | + ->getName(); |
| 135 | + |
| 136 | + // special case with static call |
| 137 | + return str_ends_with($className, 'Context'); |
| 138 | + } |
| 139 | + |
| 140 | + private function isTestFilePath(FuncCall $funcCall): bool |
| 141 | + { |
| 142 | + $scope = ScopeFetcher::fetch($funcCall); |
| 143 | + if (! $scope->getClassReflection() instanceof ClassReflection) { |
| 144 | + return false; |
| 145 | + } |
| 146 | + |
| 147 | + $className = $scope->getClassReflection() |
| 148 | + ->getName(); |
| 149 | + if (str_ends_with($className, 'Test')) { |
| 150 | + return true; |
| 151 | + } |
| 152 | + return str_ends_with($className, 'TestCase'); |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * @param Expr[] $exprs |
| 157 | + */ |
| 158 | + private function createCall(FuncCall $funcCall, string $methodName, array $exprs): MethodCall|StaticCall |
| 159 | + { |
| 160 | + $args = []; |
| 161 | + foreach ($exprs as $expr) { |
| 162 | + $args[] = new Arg($expr); |
| 163 | + } |
| 164 | + |
| 165 | + if ($this->isBehatContext($funcCall)) { |
| 166 | + $assertFullyQualified = new FullyQualified(PHPUnitClassName::ASSERT); |
| 167 | + return new StaticCall($assertFullyQualified, $methodName, $args); |
| 168 | + } |
| 169 | + |
| 170 | + return new MethodCall(new Variable('this'), $methodName, $args); |
| 171 | + } |
| 172 | +} |
0 commit comments