From d6fc25bb254bbc61e00f6451e84524de8f49d408 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 5 Sep 2025 14:14:58 +0200 Subject: [PATCH] Add nullable support to ScalarArgumentToExpectedParamTypeRector --- .../Fixture/pass_to_nullable_string.php.inc | 35 ++++++++++++++++++ .../Source/NullableSetter.php | 12 +++++++ ...calarArgumentToExpectedParamTypeRector.php | 36 +++++++++++-------- 3 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_to_nullable_string.php.inc create mode 100644 rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/NullableSetter.php diff --git a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_to_nullable_string.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_to_nullable_string.php.inc new file mode 100644 index 00000000..c4873bee --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_to_nullable_string.php.inc @@ -0,0 +1,35 @@ +setMaybe(123456); + } +} + +?> +----- +setMaybe('123456'); + } +} + +?> diff --git a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/NullableSetter.php b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/NullableSetter.php new file mode 100644 index 00000000..1de7575c --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Source/NullableSetter.php @@ -0,0 +1,12 @@ +testsNodeAnalyzer->isInTestClass($node)) { - return null; - } - - if ($node->isFirstClassCallable()) { - return null; - } - - if ($node->getArgs() === []) { + if ($this->shouldSkipCall($node)) { return null; } $hasChanged = false; - - if (! $this->hasStringOrNumberArguments($node)) { - return null; - } - $callParameterTypes = $this->methodParametersAndReturnTypesResolver->resolveCallParameterTypes($node); foreach ($node->getArgs() as $key => $arg) { @@ -123,6 +111,9 @@ public function refactor(Node $node): ?Node continue; } + // remove null + $knownParameterType = TypeCombinator::removeNull($knownParameterType); + if ($knownParameterType instanceof StringType && $arg->value instanceof Int_) { $arg->value = new String_((string) $arg->value->value); $hasChanged = true; @@ -141,6 +132,23 @@ public function refactor(Node $node): ?Node return $node; } + private function shouldSkipCall(StaticCall|MethodCall $call): bool + { + if (! $this->testsNodeAnalyzer->isInTestClass($call)) { + return true; + } + + if ($call->isFirstClassCallable()) { + return true; + } + + if ($call->getArgs() === []) { + return true; + } + + return ! $this->hasStringOrNumberArguments($call); + } + private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool { foreach ($call->getArgs() as $arg) {