Skip to content

Commit

Permalink
[NodeTypeResolver] Refactor ParametersAcceptorSelectorVariantsWrapper…
Browse files Browse the repository at this point in the history
… to pass CallLike instead of Arg (#2632)

* [NodeTypeResolver] Refactor ParametersAcceptorSelectorVariantsWrapper to pass CallLike instead of Arg

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 5, 2022
1 parent d7a1c70 commit 329b4f2
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Rector\NodeTypeResolver\PHPStan;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\CallLike;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
Expand All @@ -13,18 +13,15 @@

final class ParametersAcceptorSelectorVariantsWrapper
{
/**
* @param Arg[] $args
*/
public static function select(
FunctionReflection|MethodReflection $reflection,
array $args,
CallLike $callLike,
Scope $scope
): ParametersAcceptor {
$variants = $reflection->getVariants();

return count($variants) > 1
? ParametersAcceptorSelector::selectFromArgs($scope, $args, $variants)
? ParametersAcceptorSelector::selectFromArgs($scope, $callLike->getArgs(), $variants)
: ParametersAcceptorSelector::selectSingle($variants);
}
}
13 changes: 9 additions & 4 deletions packages/ReadWrite/Guard/VariableToConstantGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\ReadWrite\Guard;

use PhpParser\Node\Arg;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
Expand Down Expand Up @@ -55,9 +56,14 @@ public function isReadArg(Arg $arg): bool
return true;
}

$parentArg = $arg->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentArg instanceof CallLike) {
return true;
}

$referenceParametersPositions = $this->resolveFunctionReferencePositions(
$functionReflection,
[$arg],
$parentArg,
$argScope
);
if ($referenceParametersPositions === []) {
Expand All @@ -70,12 +76,11 @@ public function isReadArg(Arg $arg): bool
}

/**
* @param Arg[] $args
* @return int[]
*/
private function resolveFunctionReferencePositions(
FunctionReflection $functionReflection,
array $args,
CallLike $callLike,
Scope $scope
): array {
if (isset($this->referencePositionsByFunctionName[$functionReflection->getName()])) {
Expand All @@ -86,7 +91,7 @@ private function resolveFunctionReferencePositions(

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionReflection,
$args,
$callLike,
$scope
);
foreach ($parametersAcceptor->getParameters() as $position => $parameterReflection) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ private function resolveOriginalPositions(FuncCall $funcCall): array

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionReflection,
$funcCall->getArgs(),
$funcCall,
$scope
);
$functionName = $this->nodeNameResolver->getName($funcCall);
Expand Down
6 changes: 1 addition & 5 deletions rules/TypeDeclaration/NodeTypeAnalyzer/CallTypeAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ public function resolveMethodParameterTypes(MethodCall | StaticCall $call): arra
return [];
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$methodReflection,
$call->getArgs(),
$scope
);
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $call, $scope);

$parameterTypes = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private function resolveFuncCallType(FuncCall $funcCall): ?Type

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionReflection,
$funcCall->getArgs(),
$funcCall,
$scope
);

Expand Down
2 changes: 1 addition & 1 deletion src/NodeManipulator/PropertyManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ private function isFoundByRefParam(MethodCall | StaticCall $node): bool

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionLikeReflection,
$node->getArgs(),
$node,
$scope
);
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
Expand Down

0 comments on commit 329b4f2

Please sign in to comment.