Skip to content

Commit

Permalink
Apply ParametersAcceptorSelectorVariantsWrapper::select() take 2 (#2718)
Browse files Browse the repository at this point in the history
* Apply ParametersAcceptorSelectorVariantsWrapper::select() take 2

* [ci-review] Rector Rectify

* clean up

* [ci-review] Rector Rectify

* final touch: clean up

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 28, 2022
1 parent 89bd84e commit ec9c15a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
26 changes: 21 additions & 5 deletions packages/NodeTypeResolver/MethodParameterTypeResolver.php
Expand Up @@ -6,11 +6,14 @@

use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;

final class MethodParameterTypeResolver
{
Expand All @@ -29,7 +32,7 @@ public function provideParameterTypesByStaticCall(StaticCall $staticCall): array
return [];
}

return $this->provideParameterTypesFromMethodReflection($methodReflection);
return $this->provideParameterTypesFromMethodReflection($methodReflection, $staticCall);
}

/**
Expand All @@ -42,22 +45,35 @@ public function provideParameterTypesByClassMethod(ClassMethod $classMethod): ar
return [];
}

return $this->provideParameterTypesFromMethodReflection($methodReflection);
return $this->provideParameterTypesFromMethodReflection($methodReflection, $classMethod);
}

/**
* @return Type[]
*/
private function provideParameterTypesFromMethodReflection(MethodReflection $methodReflection): array
{
private function provideParameterTypesFromMethodReflection(
MethodReflection $methodReflection,
ClassMethod|StaticCall $node
): array {
if ($methodReflection instanceof NativeMethodReflection) {
// method "getParameters()" does not exist there
return [];
}

$parameterTypes = [];

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
if ($node instanceof ClassMethod) {
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
} else {
$scope = $node->getAttribute(AttributeKey::SCOPE);

if (! $scope instanceof Scope) {
return [];
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
}

foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$parameterTypes[] = $parameterReflection->getType();
}
Expand Down
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType;
Expand All @@ -17,6 +16,7 @@
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Symfony\Contracts\Service\Attribute\Required;

/**
Expand Down Expand Up @@ -75,7 +75,7 @@ public function resolve(Node $node): Type
}

foreach ($callerType->getReferencedClasses() as $referencedClass) {
$classMethodReturnType = $this->resolveClassMethodReturnType($referencedClass, $methodName, $scope);
$classMethodReturnType = $this->resolveClassMethodReturnType($referencedClass, $node, $methodName, $scope);
if (! $classMethodReturnType instanceof MixedType) {
return $classMethodReturnType;
}
Expand All @@ -84,8 +84,12 @@ public function resolve(Node $node): Type
return new MixedType();
}

private function resolveClassMethodReturnType(string $referencedClass, string $methodName, Scope $scope): Type
{
private function resolveClassMethodReturnType(
string $referencedClass,
StaticCall|MethodCall $node,
string $methodName,
Scope $scope
): Type {
if (! $this->reflectionProvider->hasClass($referencedClass)) {
return new MixedType();
}
Expand All @@ -99,8 +103,10 @@ private function resolveClassMethodReturnType(string $referencedClass, string $m

$methodReflection = $ancestorClassReflection->getMethod($methodName, $scope);
if ($methodReflection instanceof PhpMethodReflection) {
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle(
$methodReflection->getVariants()
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelectorVariantsWrapper::select(
$methodReflection,
$node,
$scope
);
return $parametersAcceptorWithPhpDocs->getReturnType();
}
Expand Down
Expand Up @@ -8,11 +8,14 @@
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php80\NodeResolver\ArgumentSorter;
use Rector\Php80\NodeResolver\RequireOptionalParamResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -94,7 +97,7 @@ private function refactorClassMethod(ClassMethod $classMethod): ?ClassMethod
return null;
}

$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($classMethodReflection);
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($classMethodReflection, $classMethod);
if ($expectedArgOrParamOrder === null) {
return null;
}
Expand All @@ -118,7 +121,7 @@ private function refactorNew(New_ $new): ?New_
return null;
}

$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection);
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $new);
if ($expectedArgOrParamOrder === null) {
return null;
}
Expand All @@ -135,7 +138,7 @@ private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
return null;
}

$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection);
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $methodCall);
if ($expectedArgOrParamOrder === null) {
return null;
}
Expand All @@ -156,13 +159,26 @@ private function refactorMethodCall(MethodCall $methodCall): ?MethodCall
/**
* @return int[]|null
*/
private function resolveExpectedArgParamOrderIfDifferent(MethodReflection $methodReflection): ?array
{
private function resolveExpectedArgParamOrderIfDifferent(
MethodReflection $methodReflection,
New_|MethodCall|ClassMethod $node
): ?array {
if ($this->vendorLocationDetector->detectMethodReflection($methodReflection)) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
if ($node instanceof ClassMethod) {
$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());
} else {
$scope = $node->getAttribute(AttributeKey::SCOPE);

if (! $scope instanceof Scope) {
return null;
}

$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
}

$expectedParameterReflections = $this->requireOptionalParamResolver->resolveFromReflection(
$methodReflection
);
Expand Down

0 comments on commit ec9c15a

Please sign in to comment.