Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\NullableSetter;

final class PassFloatToNullableString extends TestCase
{
public function test()
{
$nullableSetter = new NullableSetter();
$nullableSetter->setMaybe(123.456);
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;

use PHPUnit\Framework\TestCase;
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\NullableSetter;

final class PassFloatToNullableString extends TestCase
{
public function test()
{
$nullableSetter = new NullableSetter();
$nullableSetter->setMaybe('123.456');
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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;
Expand Down