Skip to content

Commit 09496bf

Browse files
committed
add float support to scalar argument rule
1 parent 92a3464 commit 09496bf

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\NullableSetter;
7+
8+
final class PassFloatToNullableString extends TestCase
9+
{
10+
public function test()
11+
{
12+
$nullableSetter = new NullableSetter();
13+
$nullableSetter->setMaybe(123.456);
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Fixture;
22+
23+
use PHPUnit\Framework\TestCase;
24+
use Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\ScalarArgumentToExpectedParamTypeRector\Source\NullableSetter;
25+
26+
final class PassFloatToNullableString extends TestCase
27+
{
28+
public function test()
29+
{
30+
$nullableSetter = new NullableSetter();
31+
$nullableSetter->setMaybe('123.456');
32+
}
33+
}
34+
35+
?>

rules/CodeQuality/Rector/MethodCall/ScalarArgumentToExpectedParamTypeRector.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\MethodCall;
99
use PhpParser\Node\Expr\StaticCall;
1010
use PhpParser\Node\Scalar;
11+
use PhpParser\Node\Scalar\Float_;
1112
use PhpParser\Node\Scalar\Int_;
1213
use PhpParser\Node\Scalar\String_;
1314
use PHPStan\Type\IntegerType;
@@ -114,9 +115,16 @@ public function refactor(Node $node): ?Node
114115
// remove null
115116
$knownParameterType = TypeCombinator::removeNull($knownParameterType);
116117

117-
if ($knownParameterType instanceof StringType && $arg->value instanceof Int_) {
118-
$arg->value = new String_((string) $arg->value->value);
119-
$hasChanged = true;
118+
if ($knownParameterType instanceof StringType) {
119+
if ($arg->value instanceof Int_) {
120+
$arg->value = new String_((string) $arg->value->value);
121+
$hasChanged = true;
122+
}
123+
124+
if ($arg->value instanceof Float_) {
125+
$arg->value = new String_((string) $arg->value->value);
126+
$hasChanged = true;
127+
}
120128
}
121129

122130
if ($knownParameterType instanceof IntegerType && $arg->value instanceof String_) {
@@ -159,6 +167,10 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool
159167
if ($arg->value instanceof String_) {
160168
return true;
161169
}
170+
171+
if ($arg->value instanceof Float_) {
172+
return true;
173+
}
162174
}
163175

164176
return false;

0 commit comments

Comments
 (0)