From 1fce7533a70e5315213474153c66c7a16f56fa99 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 8 Sep 2025 00:45:31 +0200 Subject: [PATCH 1/3] add fixture with multiarsg --- .../Fixture/multiple_args.php.inc | 35 +++++++++++++++++++ .../Source/SomeClassWithSetter.php | 5 +++ 2 files changed, 40 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/multiple_args.php.inc 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) + { + + } } From 21baae039e540659524ade85a3f5bf37c75ff573 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 8 Sep 2025 01:04:19 +0200 Subject: [PATCH 2/3] cover scalar argument in behat as well --- .../Fixture/cover_behat_context.php.inc | 35 +++++++++++++++++++ ...calarArgumentToExpectedParamTypeRector.php | 19 +++++++++- src/Enum/BehatClassName.php | 13 +++++++ src/Enum/PHPUnitClassName.php | 5 +++ src/NodeAnalyzer/TestsNodeAnalyzer.php | 2 +- stubs/Behat/Behat/Context/Context.php | 7 ++++ 6 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/cover_behat_context.php.inc create mode 100644 src/Enum/BehatClassName.php create mode 100644 stubs/Behat/Behat/Context/Context.php 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/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php index edacb36c..a073cb2e 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,17 @@ 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 @@ + Date: Sun, 7 Sep 2025 23:15:04 +0000 Subject: [PATCH 3/3] [ci-review] Rector Rectify --- .../MethodCall/ScalarArgumentToExpectedParamTypeRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php index a073cb2e..c2ec0e50 100644 --- a/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php +++ b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php @@ -195,9 +195,10 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool private function isInTestClass(StaticCall|MethodCall $call): bool { $callerClassReflection = $this->reflectionResolver->resolveClassReflection($call); - if (!$callerClassReflection instanceof ClassReflection) { + if (! $callerClassReflection instanceof ClassReflection) { return $this->testsNodeAnalyzer->isInTestClass($call); } + if ($callerClassReflection->is(BehatClassName::CONTEXT)) { return true; }