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

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

use PHPUnit\Framework\TestCase;

final class SkipNotMatchPositionNamedArgument extends TestCase
{
public function testSomething()
{
self::assertJsonResponse(contentType: 'text/html');
}

protected static function assertJsonResponse(int $statusCode = 200, string $contentType = 'application/json'): void
{
// logic
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Scalar;
use PhpParser\Node\Scalar\Float_;
use PhpParser\Node\Scalar\Int_;
Expand Down Expand Up @@ -101,13 +102,24 @@ public function refactor(Node $node): ?Node

$hasChanged = false;
$callParameterTypes = $this->methodParametersAndReturnTypesResolver->resolveCallParameterTypes($node);
$callParameterNames = $this->methodParametersAndReturnTypesResolver->resolveCallParameterNames($node);

foreach ($node->getArgs() as $key => $arg) {
if (! $arg->value instanceof Scalar) {
continue;
}

$knownParameterType = $callParameterTypes[$key] ?? null;
if ($arg->name instanceof Identifier) {
$argName = $arg->name->toString();
foreach ($callParameterNames as $keyParameterNames => $callParameterName) {
if ($argName === $callParameterName) {
$knownParameterType = $callParameterTypes[$keyParameterNames] ?? null;
break;
}
}
}

if (! $knownParameterType instanceof Type) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,35 @@ public function resolveCallParameterTypes(MethodCall|StaticCall $call): ?array
return $this->resolveParameterTypes($extendedMethodReflection, $classReflection);
}

/**
* @return string[]
*/
public function resolveCallParameterNames(MethodCall|StaticCall $call): array
{
if (! $call->name instanceof Identifier) {
return [];
}

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

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

$classReflection = $callerType->getClassReflection();
if (! $classReflection instanceof ClassReflection) {
return [];
}

if (! $classReflection->hasNativeMethod($methodName)) {
return [];
}

$extendedMethodReflection = $classReflection->getNativeMethod($methodName);
return $this->resolveParameterNames($extendedMethodReflection);
}

/**
* @return Type[]
*/
Expand All @@ -115,6 +144,23 @@ public function resolveParameterTypes(
return $parameterTypes;
}

/**
* @return string[]
*/
private function resolveParameterNames(ExtendedMethodReflection $extendedMethodReflection): array
{
$extendedParametersAcceptor = ParametersAcceptorSelector::combineAcceptors(
$extendedMethodReflection->getVariants()
);

$parameterNames = [];
foreach ($extendedParametersAcceptor->getParameters() as $parameterReflection) {
$parameterNames[] = $parameterReflection->getName();
}

return $parameterNames;
}

private function resolveObjectType(Type $type): ObjectType|Type
{
if ($type instanceof ObjectType) {
Expand Down