Updated Rector to commit ec9c15adc729988276f3fe7b726609f913266f23

ec9c15adc7 Apply ParametersAcceptorSelectorVariantsWrapper::select() take 2 (#2718)
This commit is contained in:
Tomas Votruba
2022-07-28 14:06:21 +00:00
parent 963e52618c
commit a5ec68bd9c
7 changed files with 54 additions and 27 deletions

View File

@@ -5,11 +5,14 @@ namespace Rector\NodeTypeResolver;
use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeMethodReflection; use PHPStan\Reflection\Native\NativeMethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\Type; use PHPStan\Type\Type;
use Rector\Core\Reflection\ReflectionResolver; use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
final class MethodParameterTypeResolver final class MethodParameterTypeResolver
{ {
/** /**
@@ -30,7 +33,7 @@ final class MethodParameterTypeResolver
if (!$methodReflection instanceof MethodReflection) { if (!$methodReflection instanceof MethodReflection) {
return []; return [];
} }
return $this->provideParameterTypesFromMethodReflection($methodReflection); return $this->provideParameterTypesFromMethodReflection($methodReflection, $staticCall);
} }
/** /**
* @return Type[] * @return Type[]
@@ -41,19 +44,28 @@ final class MethodParameterTypeResolver
if (!$methodReflection instanceof MethodReflection) { if (!$methodReflection instanceof MethodReflection) {
return []; return [];
} }
return $this->provideParameterTypesFromMethodReflection($methodReflection); return $this->provideParameterTypesFromMethodReflection($methodReflection, $classMethod);
} }
/** /**
* @return Type[] * @return Type[]
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\StaticCall $node
*/ */
private function provideParameterTypesFromMethodReflection(MethodReflection $methodReflection) : array private function provideParameterTypesFromMethodReflection(MethodReflection $methodReflection, $node) : array
{ {
if ($methodReflection instanceof NativeMethodReflection) { if ($methodReflection instanceof NativeMethodReflection) {
// method "getParameters()" does not exist there // method "getParameters()" does not exist there
return []; return [];
} }
$parameterTypes = []; $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) { foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$parameterTypes[] = $parameterReflection->getType(); $parameterTypes[] = $parameterReflection->getType();
} }

View File

@@ -7,7 +7,6 @@ use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\StaticCall;
use PHPStan\Analyser\Scope; use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodReflection; use PHPStan\Reflection\Php\PhpMethodReflection;
use PHPStan\Reflection\ReflectionProvider; use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\MixedType; use PHPStan\Type\MixedType;
@@ -16,6 +15,7 @@ use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface; use Rector\NodeTypeResolver\Contract\NodeTypeResolverInterface;
use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver; use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use RectorPrefix202207\Symfony\Contracts\Service\Attribute\Required; use RectorPrefix202207\Symfony\Contracts\Service\Attribute\Required;
/** /**
* @implements NodeTypeResolverInterface<StaticCall|MethodCall> * @implements NodeTypeResolverInterface<StaticCall|MethodCall>
@@ -79,14 +79,17 @@ final class StaticCallMethodCallTypeResolver implements NodeTypeResolverInterfac
$callerType = $this->nodeTypeResolver->getType($node->class); $callerType = $this->nodeTypeResolver->getType($node->class);
} }
foreach ($callerType->getReferencedClasses() as $referencedClass) { foreach ($callerType->getReferencedClasses() as $referencedClass) {
$classMethodReturnType = $this->resolveClassMethodReturnType($referencedClass, $methodName, $scope); $classMethodReturnType = $this->resolveClassMethodReturnType($referencedClass, $node, $methodName, $scope);
if (!$classMethodReturnType instanceof MixedType) { if (!$classMethodReturnType instanceof MixedType) {
return $classMethodReturnType; return $classMethodReturnType;
} }
} }
return new MixedType(); return new MixedType();
} }
private function resolveClassMethodReturnType(string $referencedClass, string $methodName, Scope $scope) : Type /**
* @param \PhpParser\Node\Expr\StaticCall|\PhpParser\Node\Expr\MethodCall $node
*/
private function resolveClassMethodReturnType(string $referencedClass, $node, string $methodName, Scope $scope) : Type
{ {
if (!$this->reflectionProvider->hasClass($referencedClass)) { if (!$this->reflectionProvider->hasClass($referencedClass)) {
return new MixedType(); return new MixedType();
@@ -98,7 +101,7 @@ final class StaticCallMethodCallTypeResolver implements NodeTypeResolverInterfac
} }
$methodReflection = $ancestorClassReflection->getMethod($methodName, $scope); $methodReflection = $ancestorClassReflection->getMethod($methodName, $scope);
if ($methodReflection instanceof PhpMethodReflection) { if ($methodReflection instanceof PhpMethodReflection) {
$parametersAcceptorWithPhpDocs = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants()); $parametersAcceptorWithPhpDocs = ParametersAcceptorSelectorVariantsWrapper::select($methodReflection, $node, $scope);
return $parametersAcceptorWithPhpDocs->getReturnType(); return $parametersAcceptorWithPhpDocs->getReturnType();
} }
} }

View File

@@ -7,11 +7,14 @@ use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\New_; use PhpParser\Node\Expr\New_;
use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection; use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ParametersAcceptorSelector;
use Rector\CodingStyle\Reflection\VendorLocationDetector; use Rector\CodingStyle\Reflection\VendorLocationDetector;
use Rector\Core\Rector\AbstractRector; use Rector\Core\Rector\AbstractRector;
use Rector\Core\Reflection\ReflectionResolver; use Rector\Core\Reflection\ReflectionResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\Php80\NodeResolver\ArgumentSorter; use Rector\Php80\NodeResolver\ArgumentSorter;
use Rector\Php80\NodeResolver\RequireOptionalParamResolver; use Rector\Php80\NodeResolver\RequireOptionalParamResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
@@ -99,7 +102,7 @@ CODE_SAMPLE
if (!$classMethodReflection instanceof MethodReflection) { if (!$classMethodReflection instanceof MethodReflection) {
return null; return null;
} }
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($classMethodReflection); $expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($classMethodReflection, $classMethod);
if ($expectedArgOrParamOrder === null) { if ($expectedArgOrParamOrder === null) {
return null; return null;
} }
@@ -115,7 +118,7 @@ CODE_SAMPLE
if (!$methodReflection instanceof MethodReflection) { if (!$methodReflection instanceof MethodReflection) {
return null; return null;
} }
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection); $expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $new);
if ($expectedArgOrParamOrder === null) { if ($expectedArgOrParamOrder === null) {
return null; return null;
} }
@@ -128,7 +131,7 @@ CODE_SAMPLE
if (!$methodReflection instanceof MethodReflection) { if (!$methodReflection instanceof MethodReflection) {
return null; return null;
} }
$expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection); $expectedArgOrParamOrder = $this->resolveExpectedArgParamOrderIfDifferent($methodReflection, $methodCall);
if ($expectedArgOrParamOrder === null) { if ($expectedArgOrParamOrder === null) {
return null; return null;
} }
@@ -141,13 +144,22 @@ CODE_SAMPLE
} }
/** /**
* @return int[]|null * @return int[]|null
* @param \PhpParser\Node\Expr\New_|\PhpParser\Node\Expr\MethodCall|\PhpParser\Node\Stmt\ClassMethod $node
*/ */
private function resolveExpectedArgParamOrderIfDifferent(MethodReflection $methodReflection) : ?array private function resolveExpectedArgParamOrderIfDifferent(MethodReflection $methodReflection, $node) : ?array
{ {
if ($this->vendorLocationDetector->detectMethodReflection($methodReflection)) { if ($this->vendorLocationDetector->detectMethodReflection($methodReflection)) {
return null; 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); $expectedParameterReflections = $this->requireOptionalParamResolver->resolveFromReflection($methodReflection);
if ($expectedParameterReflections === $parametersAcceptor->getParameters()) { if ($expectedParameterReflections === $parametersAcceptor->getParameters()) {
return null; return null;

View File

@@ -17,12 +17,12 @@ final class VersionResolver
* @api * @api
* @var string * @var string
*/ */
public const PACKAGE_VERSION = '89bd84eafaab9ab66a4fcd6a1a4df67d15040732'; public const PACKAGE_VERSION = 'ec9c15adc729988276f3fe7b726609f913266f23';
/** /**
* @api * @api
* @var string * @var string
*/ */
public const RELEASE_DATE = '2022-07-28 01:47:03'; public const RELEASE_DATE = '2022-07-28 20:59:50';
/** /**
* @var int * @var int
*/ */

2
vendor/autoload.php vendored
View File

@@ -9,4 +9,4 @@ if (PHP_VERSION_ID < 50600) {
require_once __DIR__ . '/composer/autoload_real.php'; require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit2089a995a509cf6e789d4bbc9384b709::getLoader(); return ComposerAutoloaderInit9d1d56e113fc4820715cefd11abc518f::getLoader();

View File

@@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer // autoload_real.php @generated by Composer
class ComposerAutoloaderInit2089a995a509cf6e789d4bbc9384b709 class ComposerAutoloaderInit9d1d56e113fc4820715cefd11abc518f
{ {
private static $loader; private static $loader;
@@ -22,19 +22,19 @@ class ComposerAutoloaderInit2089a995a509cf6e789d4bbc9384b709
return self::$loader; return self::$loader;
} }
spl_autoload_register(array('ComposerAutoloaderInit2089a995a509cf6e789d4bbc9384b709', 'loadClassLoader'), true, true); spl_autoload_register(array('ComposerAutoloaderInit9d1d56e113fc4820715cefd11abc518f', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit2089a995a509cf6e789d4bbc9384b709', 'loadClassLoader')); spl_autoload_unregister(array('ComposerAutoloaderInit9d1d56e113fc4820715cefd11abc518f', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php'; require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit2089a995a509cf6e789d4bbc9384b709::getInitializer($loader)); call_user_func(\Composer\Autoload\ComposerStaticInit9d1d56e113fc4820715cefd11abc518f::getInitializer($loader));
$loader->setClassMapAuthoritative(true); $loader->setClassMapAuthoritative(true);
$loader->register(true); $loader->register(true);
$includeFiles = \Composer\Autoload\ComposerStaticInit2089a995a509cf6e789d4bbc9384b709::$files; $includeFiles = \Composer\Autoload\ComposerStaticInit9d1d56e113fc4820715cefd11abc518f::$files;
foreach ($includeFiles as $fileIdentifier => $file) { foreach ($includeFiles as $fileIdentifier => $file) {
composerRequire2089a995a509cf6e789d4bbc9384b709($fileIdentifier, $file); composerRequire9d1d56e113fc4820715cefd11abc518f($fileIdentifier, $file);
} }
return $loader; return $loader;
@@ -46,7 +46,7 @@ class ComposerAutoloaderInit2089a995a509cf6e789d4bbc9384b709
* @param string $file * @param string $file
* @return void * @return void
*/ */
function composerRequire2089a995a509cf6e789d4bbc9384b709($fileIdentifier, $file) function composerRequire9d1d56e113fc4820715cefd11abc518f($fileIdentifier, $file)
{ {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;

View File

@@ -4,7 +4,7 @@
namespace Composer\Autoload; namespace Composer\Autoload;
class ComposerStaticInit2089a995a509cf6e789d4bbc9384b709 class ComposerStaticInit9d1d56e113fc4820715cefd11abc518f
{ {
public static $files = array ( public static $files = array (
'0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php', '0e6d7bf4a5811bfa5cf40c5ccd6fae6a' => __DIR__ . '/..' . '/symfony/polyfill-mbstring/bootstrap.php',
@@ -3407,9 +3407,9 @@ class ComposerStaticInit2089a995a509cf6e789d4bbc9384b709
public static function getInitializer(ClassLoader $loader) public static function getInitializer(ClassLoader $loader)
{ {
return \Closure::bind(function () use ($loader) { return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit2089a995a509cf6e789d4bbc9384b709::$prefixLengthsPsr4; $loader->prefixLengthsPsr4 = ComposerStaticInit9d1d56e113fc4820715cefd11abc518f::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit2089a995a509cf6e789d4bbc9384b709::$prefixDirsPsr4; $loader->prefixDirsPsr4 = ComposerStaticInit9d1d56e113fc4820715cefd11abc518f::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit2089a995a509cf6e789d4bbc9384b709::$classMap; $loader->classMap = ComposerStaticInit9d1d56e113fc4820715cefd11abc518f::$classMap;
}, null, ClassLoader::class); }, null, ClassLoader::class);
} }