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 PassToNullableString extends TestCase
{
public function test()
{
$nullableSetter = new NullableSetter();
$nullableSetter->setMaybe(123456);
}
}

?>
-----
<?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 PassToNullableString extends TestCase
{
public function test()
{
$nullableSetter = new NullableSetter();
$nullableSetter->setMaybe('123456');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

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

final class NullableSetter
{
public function setMaybe(?string $status)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -93,24 +94,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->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) {
Expand All @@ -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;
Expand All @@ -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) {
Expand Down