diff --git a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/cover_behat_context.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/cover_behat_context.php.inc new file mode 100644 index 00000000..012c644e --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/cover_behat_context.php.inc @@ -0,0 +1,35 @@ +setItems('1', 2, 3, 4); + } +} + +?> +----- +setItems('1', 2, '3', 4); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/multiple_args.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/multiple_args.php.inc new file mode 100644 index 00000000..8d9e113f --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/multiple_args.php.inc @@ -0,0 +1,35 @@ +setItems('1', 2, 3, 4); + } +} + +?> +----- +setItems('1', 2, '3', 4); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/SomeClassWithSetter.php b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/SomeClassWithSetter.php index 824c7620..799c46fc 100644 --- a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/SomeClassWithSetter.php +++ b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/SomeClassWithSetter.php @@ -22,4 +22,9 @@ public function setUnionType(int|string $unionValue) { } + + public function setItems(string $one, int $two, string $three, int $four) + { + + } } diff --git a/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php index edacb36c..c2ec0e50 100644 --- a/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php +++ b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php @@ -12,13 +12,16 @@ use PhpParser\Node\Scalar\Float_; use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Scalar\String_; +use PHPStan\Reflection\ClassReflection; use PHPStan\Type\IntegerType; use PHPStan\Type\StringType; use PHPStan\Type\Type; use PHPStan\Type\TypeCombinator; use Rector\PHPUnit\CodeQuality\Reflection\MethodParametersAndReturnTypesResolver; +use Rector\PHPUnit\Enum\BehatClassName; use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; use Rector\Rector\AbstractRector; +use Rector\Reflection\ReflectionResolver; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -30,6 +33,7 @@ final class ScalarArgumentToExpectedParamTypeRector extends AbstractRector public function __construct( private readonly TestsNodeAnalyzer $testsNodeAnalyzer, private readonly MethodParametersAndReturnTypesResolver $methodParametersAndReturnTypesResolver, + private readonly ReflectionResolver $reflectionResolver, ) { } @@ -154,7 +158,7 @@ public function refactor(Node $node): ?Node private function shouldSkipCall(StaticCall|MethodCall $call): bool { - if (! $this->testsNodeAnalyzer->isInTestClass($call)) { + if (! $this->isInTestClass($call)) { return true; } @@ -187,4 +191,18 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool return false; } + + private function isInTestClass(StaticCall|MethodCall $call): bool + { + $callerClassReflection = $this->reflectionResolver->resolveClassReflection($call); + if (! $callerClassReflection instanceof ClassReflection) { + return $this->testsNodeAnalyzer->isInTestClass($call); + } + + if ($callerClassReflection->is(BehatClassName::CONTEXT)) { + return true; + } + + return $this->testsNodeAnalyzer->isInTestClass($call); + } } diff --git a/src/Enum/BehatClassName.php b/src/Enum/BehatClassName.php new file mode 100644 index 00000000..5f22950e --- /dev/null +++ b/src/Enum/BehatClassName.php @@ -0,0 +1,13 @@ +