|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Rector\PHPUnit\PHPUnit120\Rector\Class_; |
| 6 | + |
| 7 | +use PhpParser\Node; |
| 8 | +use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\Expr\MethodCall; |
| 10 | +use PhpParser\Node\Expr\StaticCall; |
| 11 | +use Rector\PhpParser\Node\Value\ValueResolver; |
| 12 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 13 | +use Rector\Rector\AbstractRector; |
| 14 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 15 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 16 | + |
| 17 | +/** |
| 18 | + * @see https://github.com/sebastianbergmann/phpunit/issues/6053 |
| 19 | + * @see https://github.com/sebastianbergmann/phpunit/blob/12.0.0/ChangeLog-12.0.md |
| 20 | + * @see \Rector\PHPUnit\Tests\PHPUnit120\Rector\MethodCall\AssertIsTypeMethodCallRector\AssertIsTypeMethodCallRectorTest |
| 21 | + */ |
| 22 | +final class AssertIsTypeMethodCallRector extends AbstractRector |
| 23 | +{ |
| 24 | + private const IS_TYPE_VALUE_TO_METHOD = [ |
| 25 | + 'array' => 'isArray', |
| 26 | + 'bool' => 'isBool', |
| 27 | + 'boolean' => 'isBool', |
| 28 | + 'callable' => 'isCallable', |
| 29 | + 'double' => 'isFloat', |
| 30 | + 'float' => 'isFloat', |
| 31 | + 'integer' => 'isInt', |
| 32 | + 'int' => 'isInt', |
| 33 | + 'iterable' => 'isIterable', |
| 34 | + 'null' => 'isNull', |
| 35 | + 'numeric' => 'isNumeric', |
| 36 | + 'object' => 'isObject', |
| 37 | + 'real' => 'isFloat', |
| 38 | + 'resource' => 'isResource', |
| 39 | + 'resource (closed)' => 'isClosedResource', |
| 40 | + 'scalar' => 'isScalar', |
| 41 | + 'string' => 'isString', |
| 42 | + ]; |
| 43 | + |
| 44 | + public function __construct( |
| 45 | + private readonly ValueResolver $valueResolver, |
| 46 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 47 | + ) { |
| 48 | + } |
| 49 | + |
| 50 | + public function getRuleDefinition(): RuleDefinition |
| 51 | + { |
| 52 | + return new RuleDefinition( |
| 53 | + 'Replaces `Assert::isType()` calls with type-specific `Assert::is*()` calls', |
| 54 | + [ |
| 55 | + new CodeSample( |
| 56 | + <<<'CODE_SAMPLE' |
| 57 | + use PHPUnit\Framework\TestCase; |
| 58 | +
|
| 59 | + final class SomeClass extends TestCase |
| 60 | + { |
| 61 | + public function testMethod(): void |
| 62 | + { |
| 63 | + $this->assertThat([], $this->isType('array')); |
| 64 | + } |
| 65 | + } |
| 66 | + CODE_SAMPLE |
| 67 | + , |
| 68 | + <<<'CODE_SAMPLE' |
| 69 | + use PHPUnit\Framework\TestCase; |
| 70 | +
|
| 71 | + final class SomeClass extends TestCase |
| 72 | + { |
| 73 | + public function testMethod(): void |
| 74 | + { |
| 75 | + $this->assertThat([], $this->isArray()); |
| 76 | + } |
| 77 | + } |
| 78 | + CODE_SAMPLE |
| 79 | + , |
| 80 | + ), |
| 81 | + ], |
| 82 | + ); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array<class-string<Node>> |
| 87 | + */ |
| 88 | + public function getNodeTypes(): array |
| 89 | + { |
| 90 | + return [MethodCall::class, StaticCall::class]; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @param MethodCall|StaticCall $node |
| 95 | + */ |
| 96 | + public function refactor(Node $node): Node|null |
| 97 | + { |
| 98 | + if (! $this->testsNodeAnalyzer->isPHPUnitTestCaseCall($node) || ! $this->isName($node->name, 'isType')) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + |
| 102 | + if (count($node->getArgs()) !== 1) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + $arg = $node->getArg('type', 0); |
| 107 | + if (! $arg instanceof Arg) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + $argValue = $this->valueResolver->getValue($arg); |
| 112 | + if (isset(self::IS_TYPE_VALUE_TO_METHOD[$argValue])) { |
| 113 | + if ($node instanceof MethodCall) { |
| 114 | + return new MethodCall($node->var, self::IS_TYPE_VALUE_TO_METHOD[$argValue]); |
| 115 | + } |
| 116 | + |
| 117 | + return new StaticCall($node->class, self::IS_TYPE_VALUE_TO_METHOD[$argValue]); |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + return null; |
| 122 | + } |
| 123 | +} |
0 commit comments