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,33 @@
<?php

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

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

final class PassFloatToNullableString extends TestCase
{
public function test()
{
$valueObject = new ValueObjectWithConstructor(123.456, '123');
}
}

?>
-----
<?php

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

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

final class PassFloatToNullableString extends TestCase
{
public function test()
{
$valueObject = new ValueObjectWithConstructor('123.456', 123);
}
}

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

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ final class NullableSetter
public function setMaybe(?string $status)
{
}

public function setValue(?string $letter, ?int $number = null)
{
}
}
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 ValueObjectWithConstructor
{
public function __construct(?string $letter, ?int $number = null)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar;
Expand Down Expand Up @@ -92,11 +93,11 @@ public function setPhone(string $phone)
*/
public function getNodeTypes(): array
{
return [MethodCall::class, StaticCall::class];
return [MethodCall::class, StaticCall::class, New_::class];
}

/**
* @param MethodCall|StaticCall $node
* @param MethodCall|StaticCall|New_ $node
*/
public function refactor(Node $node): ?Node
{
Expand Down Expand Up @@ -156,26 +157,26 @@ public function refactor(Node $node): ?Node
return $node;
}

private function shouldSkipCall(StaticCall|MethodCall $call): bool
private function shouldSkipCall(StaticCall|MethodCall|New_ $callLike): bool
{
if (! $this->isInTestClass($call)) {
if (! $this->isInTestClass($callLike)) {
return true;
}

if ($call->isFirstClassCallable()) {
if ($callLike->isFirstClassCallable()) {
return true;
}

if ($call->getArgs() === []) {
if ($callLike->getArgs() === []) {
return true;
}

return ! $this->hasStringOrNumberArguments($call);
return ! $this->hasStringOrNumberArguments($callLike);
}

private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool
private function hasStringOrNumberArguments(StaticCall|MethodCall|New_ $callLike): bool
{
foreach ($call->getArgs() as $arg) {
foreach ($callLike->getArgs() as $arg) {
if ($arg->value instanceof Int_) {
return true;
}
Expand All @@ -192,7 +193,7 @@ private function hasStringOrNumberArguments(StaticCall|MethodCall $call): bool
return false;
}

private function isInTestClass(StaticCall|MethodCall $call): bool
private function isInTestClass(StaticCall|MethodCall|New_ $call): bool
{
$callerClassReflection = $this->reflectionResolver->resolveClassReflection($call);
if (! $callerClassReflection instanceof ClassReflection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace Rector\PHPUnit\CodeQuality\Reflection;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\IntersectionType;
use PHPStan\Type\MixedType;
use PHPStan\Type\ObjectType;
Expand All @@ -23,7 +26,8 @@
final readonly class MethodParametersAndReturnTypesResolver
{
public function __construct(
private NodeTypeResolver $nodeTypeResolver
private NodeTypeResolver $nodeTypeResolver,
private ReflectionProvider $reflectionProvider
) {
}

Expand Down Expand Up @@ -64,15 +68,31 @@ public function resolveFromReflection(
/**
* @return null|Type[]
*/
public function resolveCallParameterTypes(MethodCall|StaticCall $call): ?array
public function resolveCallParameterTypes(MethodCall|StaticCall|New_ $callLike): ?array
{
if (! $call->name instanceof Identifier) {
if ($callLike instanceof New_) {
if (! $callLike->class instanceof Name) {
return null;
}

$className = $callLike->class->toString();
if (! $this->reflectionProvider->hasClass($className)) {
return null;
}

$classReflection = $this->reflectionProvider->getClass($className);
return $this->resolveParameterTypes($classReflection->getConstructor(), $classReflection);
}

if (! $callLike->name instanceof Identifier) {
return null;
}

$methodName = $call->name->toString();
$methodName = $callLike->name->toString();

$callerType = $this->nodeTypeResolver->getType($call instanceof MethodCall ? $call->var : $call->class);
$callerType = $this->nodeTypeResolver->getType(
$callLike instanceof MethodCall ? $callLike->var : $callLike->class
);
if ($callerType instanceof ThisType) {
$callerType = $callerType->getStaticObjectType();
}
Expand All @@ -97,15 +117,31 @@ public function resolveCallParameterTypes(MethodCall|StaticCall $call): ?array
/**
* @return string[]
*/
public function resolveCallParameterNames(MethodCall|StaticCall $call): array
public function resolveCallParameterNames(MethodCall|StaticCall|New_ $callLike): array
{
if (! $call->name instanceof Identifier) {
if ($callLike instanceof New_) {
if (! $callLike->class instanceof Name) {
return [];
}

$className = $callLike->class->toString();
if (! $this->reflectionProvider->hasClass($className)) {
return [];
}

$classReflection = $this->reflectionProvider->getClass($className);
return $this->resolveParameterNames($classReflection->getConstructor());
}

if (! $callLike->name instanceof Identifier) {
return [];
}

$methodName = $call->name->toString();
$methodName = $callLike->name->toString();

$callerType = $this->nodeTypeResolver->getType($call instanceof MethodCall ? $call->var : $call->class);
$callerType = $this->nodeTypeResolver->getType(
$callLike instanceof MethodCall ? $callLike->var : $callLike->class
);
if (! $callerType instanceof ObjectType) {
return [];
}
Expand Down