diff --git a/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_float_to_nullable_string.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_float_to_nullable_string.php.inc new file mode 100644 index 00000000..544e52ee --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector/Fixture/pass_float_to_nullable_string.php.inc @@ -0,0 +1,35 @@ +setMaybe(123.456); + } +} + +?> +----- +setMaybe('123.456'); + } +} + +?> diff --git a/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php index b94bb8c1..2be5242c 100644 --- a/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php +++ b/rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php @@ -8,6 +8,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Scalar; +use PhpParser\Node\Scalar\Float_; use PhpParser\Node\Scalar\Int_; use PhpParser\Node\Scalar\String_; use PHPStan\Type\IntegerType; @@ -114,9 +115,16 @@ public function refactor(Node $node): ?Node // remove null $knownParameterType = TypeCombinator::removeNull($knownParameterType); - if ($knownParameterType instanceof StringType && $arg->value instanceof Int_) { - $arg->value = new String_((string) $arg->value->value); - $hasChanged = true; + if ($knownParameterType instanceof StringType) { + if ($arg->value instanceof Int_) { + $arg->value = new String_((string) $arg->value->value); + $hasChanged = true; + } + + if ($arg->value instanceof Float_) { + $arg->value = new String_((string) $arg->value->value); + $hasChanged = true; + } } if ($knownParameterType instanceof IntegerType && $arg->value instanceof String_) { @@ -159,6 +167,10 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool if ($arg->value instanceof String_) { return true; } + + if ($arg->value instanceof Float_) { + return true; + } } return false;