|
| 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\MethodCall; |
| 9 | +use PhpParser\Node\Expr\StaticCall; |
| 10 | +use PhpParser\Node\Scalar; |
| 11 | +use PhpParser\Node\Scalar\Int_; |
| 12 | +use PhpParser\Node\Scalar\String_; |
| 13 | +use PHPStan\Type\IntegerType; |
| 14 | +use PHPStan\Type\StringType; |
| 15 | +use PHPStan\Type\Type; |
| 16 | +use Rector\PHPUnit\CodeQuality\Reflection\MethodParametersAndReturnTypesResolver; |
| 17 | +use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; |
| 18 | +use Rector\Rector\AbstractRector; |
| 19 | +use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
| 20 | +use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
| 21 | + |
| 22 | +/** |
| 23 | + * @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\ScalarArgumentToExpectedParamTypeRectorTest |
| 24 | + */ |
| 25 | +final class ScalarArgumentToExpectedParamTypeRector extends AbstractRector |
| 26 | +{ |
| 27 | + public function __construct( |
| 28 | + private readonly TestsNodeAnalyzer $testsNodeAnalyzer, |
| 29 | + private readonly MethodParametersAndReturnTypesResolver $methodParametersAndReturnTypesResolver, |
| 30 | + ) { |
| 31 | + } |
| 32 | + |
| 33 | + public function getRuleDefinition(): RuleDefinition |
| 34 | + { |
| 35 | + return new RuleDefinition( |
| 36 | + 'Correct expected type in setter of tests, if param type is strictly defined', |
| 37 | + [ |
| 38 | + new CodeSample( |
| 39 | + <<<'CODE_SAMPLE' |
| 40 | +use PHPUnit\Framework\TestCase; |
| 41 | +
|
| 42 | +class SomeTest extends TestCase |
| 43 | +{ |
| 44 | + public function test() |
| 45 | + { |
| 46 | + $someClass = new SomeClass(); |
| 47 | + $someClass->setPhone(12345); |
| 48 | + } |
| 49 | +} |
| 50 | +
|
| 51 | +final class SomeClass |
| 52 | +{ |
| 53 | + public function setPhone(string $phone) |
| 54 | + { |
| 55 | + } |
| 56 | +} |
| 57 | +CODE_SAMPLE |
| 58 | + , |
| 59 | + <<<'CODE_SAMPLE' |
| 60 | +use PHPUnit\Framework\TestCase; |
| 61 | +
|
| 62 | +class SomeTest extends TestCase |
| 63 | +{ |
| 64 | + public function test() |
| 65 | + { |
| 66 | + $someClass = new SomeClass(); |
| 67 | + $someClass->setPhone('12345'); |
| 68 | + } |
| 69 | +} |
| 70 | +
|
| 71 | +final class SomeClass |
| 72 | +{ |
| 73 | + public function setPhone(string $phone) |
| 74 | + { |
| 75 | + } |
| 76 | +} |
| 77 | +CODE_SAMPLE |
| 78 | + ), |
| 79 | + ] |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return array<class-string<Node>> |
| 85 | + */ |
| 86 | + public function getNodeTypes(): array |
| 87 | + { |
| 88 | + return [MethodCall::class, StaticCall::class]; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param MethodCall|StaticCall $node |
| 93 | + */ |
| 94 | + public function refactor(Node $node): ?Node |
| 95 | + { |
| 96 | + if (! $this->testsNodeAnalyzer->isInTestClass($node)) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + |
| 100 | + if ($node->isFirstClassCallable()) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + if ($node->getArgs() === []) { |
| 105 | + return null; |
| 106 | + } |
| 107 | + |
| 108 | + $hasChanged = false; |
| 109 | + |
| 110 | + if (! $this->hasStringOrNumberArguments($node)) { |
| 111 | + return null; |
| 112 | + } |
| 113 | + |
| 114 | + $callParameterTypes = $this->methodParametersAndReturnTypesResolver->resolveCallParameterTypes($node); |
| 115 | + |
| 116 | + foreach ($node->getArgs() as $key => $arg) { |
| 117 | + if (! $arg->value instanceof Scalar) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + $knownParameterType = $callParameterTypes[$key] ?? null; |
| 122 | + if (! $knownParameterType instanceof Type) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + |
| 126 | + if ($knownParameterType instanceof StringType && $arg->value instanceof Int_) { |
| 127 | + $arg->value = new String_((string) $arg->value->value); |
| 128 | + $hasChanged = true; |
| 129 | + } |
| 130 | + |
| 131 | + if ($knownParameterType instanceof IntegerType && $arg->value instanceof String_) { |
| 132 | + $arg->value = new Int_((int) $arg->value->value); |
| 133 | + $hasChanged = true; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (! $hasChanged) { |
| 138 | + return null; |
| 139 | + } |
| 140 | + |
| 141 | + return $node; |
| 142 | + } |
| 143 | + |
| 144 | + private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool |
| 145 | + { |
| 146 | + foreach ($call->getArgs() as $arg) { |
| 147 | + if ($arg->value instanceof Int_) { |
| 148 | + return true; |
| 149 | + } |
| 150 | + |
| 151 | + if ($arg->value instanceof String_) { |
| 152 | + return true; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return false; |
| 157 | + } |
| 158 | +} |
0 commit comments