|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Expr\MethodCall; |
9 | 9 | use PhpParser\Node\Identifier; |
| 10 | +use Rector\PhpParser\Node\Value\ValueResolver; |
10 | 11 | use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
11 | 12 | use Rector\Rector\AbstractRector; |
12 | 13 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
|
17 | 18 | */ |
18 | 19 | final class AssertThatToDirectAssertRector extends AbstractRector |
19 | 20 | { |
| 21 | + /** |
| 22 | + * @var array<string, string> |
| 23 | + */ |
| 24 | + private const array IS_TO_ASSERT_METHOD_MAP = [ |
| 25 | + 'isTrue' => 'assertTrue', |
| 26 | + 'isFalse' => 'assertFalse', |
| 27 | + 'isNull' => 'assertNull', |
| 28 | + 'isEmpty' => 'assertEmpty', |
| 29 | + 'isCountable' => 'assertIsCountable', |
| 30 | + 'isArray' => 'assertIsArray', |
| 31 | + 'isString' => 'assertIsString', |
| 32 | + 'isInt' => 'assertIsInt', |
| 33 | + 'isFloat' => 'assertIsFloat', |
| 34 | + 'isBool' => 'assertIsBool', |
| 35 | + ]; |
| 36 | + |
20 | 37 | public function __construct( |
21 | 38 | private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 39 | + private readonly ValueResolver $valueResolver, |
22 | 40 | ) { |
23 | 41 | } |
24 | 42 |
|
@@ -95,8 +113,25 @@ public function refactor(Node $node): ?Node |
95 | 113 | return $node; |
96 | 114 | } |
97 | 115 |
|
98 | | - if ($exactAssertName === 'isTrue') { |
99 | | - $node->name = new Identifier('assertTrue'); |
| 116 | + foreach (self::IS_TO_ASSERT_METHOD_MAP as $isName => $assertName) { |
| 117 | + if (! $this->isName($exactAssertMethodCall->name, $isName)) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + $node->name = new Identifier($assertName); |
| 122 | + unset($node->args[1]); |
| 123 | + |
| 124 | + return $node; |
| 125 | + } |
| 126 | + |
| 127 | + if ($this->isName($exactAssertMethodCall->name, 'isType')) { |
| 128 | + $exactFirstArg = $exactAssertMethodCall->getArgs()[0]; |
| 129 | + $expectedType = $this->valueResolver->getValue($exactFirstArg->value); |
| 130 | + if (! is_string($expectedType)) { |
| 131 | + return null; |
| 132 | + } |
| 133 | + |
| 134 | + $node->name = new Identifier('assertIs' . ucfirst($expectedType)); |
100 | 135 | unset($node->args[1]); |
101 | 136 |
|
102 | 137 | return $node; |
|
0 commit comments