From 52e59fd9924ec247fa9259b073a391ee041506c4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 16 Jun 2023 15:32:00 +0000 Subject: [PATCH] Updated Rector to commit 765450f3e2c7ae1ebfb07a708f20640a66681bb9 https://github.com/rectorphp/rector-src/commit/765450f3e2c7ae1ebfb07a708f20640a66681bb9 [TypeDeclaration] Remove parent lookup on ReturnedNodesReturnTypeInfererTypeInferer (#4246) --- ...urnedNodesReturnTypeInfererTypeInferer.php | 55 +++++++++---------- src/Application/VersionResolver.php | 4 +- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 10 ++-- vendor/composer/autoload_static.php | 8 +-- 5 files changed, 39 insertions(+), 40 deletions(-) diff --git a/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php b/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php index 173724745e9..661ae6c5431 100644 --- a/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php +++ b/rules/TypeDeclaration/TypeInferer/ReturnTypeInferer/ReturnedNodesReturnTypeInfererTypeInferer.php @@ -4,22 +4,19 @@ declare (strict_types=1); namespace Rector\TypeDeclaration\TypeInferer\ReturnTypeInferer; use PhpParser\Node; +use PhpParser\Node\Expr\ArrowFunction; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\FunctionLike; -use PhpParser\Node\Stmt\Class_; -use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Interface_; use PhpParser\Node\Stmt\Return_; -use PhpParser\Node\Stmt\Trait_; use PhpParser\NodeTraverser; +use PHPStan\Reflection\ClassReflection; use PHPStan\Reflection\MethodReflection; use PHPStan\Type\ArrayType; use PHPStan\Type\MixedType; use PHPStan\Type\Type; use PHPStan\Type\VoidType; use Rector\Core\PhpParser\AstResolver; -use Rector\Core\PhpParser\Node\BetterNodeFinder; use Rector\Core\PhpParser\Printer\BetterStandardPrinter; use Rector\Core\Reflection\ReflectionResolver; use Rector\NodeTypeResolver\NodeTypeResolver; @@ -72,12 +69,7 @@ final class ReturnedNodesReturnTypeInfererTypeInferer * @var \Rector\Core\Reflection\ReflectionResolver */ private $reflectionResolver; - /** - * @readonly - * @var \Rector\Core\PhpParser\Node\BetterNodeFinder - */ - private $betterNodeFinder; - public function __construct(SilentVoidResolver $silentVoidResolver, NodeTypeResolver $nodeTypeResolver, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, TypeFactory $typeFactory, SplArrayFixedTypeNarrower $splArrayFixedTypeNarrower, AstResolver $reflectionAstResolver, BetterStandardPrinter $betterStandardPrinter, ReflectionResolver $reflectionResolver, BetterNodeFinder $betterNodeFinder) + public function __construct(SilentVoidResolver $silentVoidResolver, NodeTypeResolver $nodeTypeResolver, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, TypeFactory $typeFactory, SplArrayFixedTypeNarrower $splArrayFixedTypeNarrower, AstResolver $reflectionAstResolver, BetterStandardPrinter $betterStandardPrinter, ReflectionResolver $reflectionResolver) { $this->silentVoidResolver = $silentVoidResolver; $this->nodeTypeResolver = $nodeTypeResolver; @@ -87,22 +79,20 @@ final class ReturnedNodesReturnTypeInfererTypeInferer $this->reflectionAstResolver = $reflectionAstResolver; $this->betterStandardPrinter = $betterStandardPrinter; $this->reflectionResolver = $reflectionResolver; - $this->betterNodeFinder = $betterNodeFinder; } public function inferFunctionLike(FunctionLike $functionLike) : Type { - $classLike = $this->betterNodeFinder->findParentType($functionLike, ClassLike::class); - if (!$classLike instanceof ClassLike) { + $classReflection = $this->reflectionResolver->resolveClassReflection($functionLike); + if (!$classReflection instanceof ClassReflection) { return new MixedType(); } - if ($functionLike instanceof ClassMethod && $classLike instanceof Interface_) { + if ($functionLike instanceof ClassMethod && $classReflection->isInterface()) { return new MixedType(); } $types = []; $localReturnNodes = $this->collectReturns($functionLike); if ($localReturnNodes === []) { - /** @var Class_|Interface_|Trait_ $classLike */ - return $this->resolveNoLocalReturnNodes($classLike, $functionLike); + return $this->resolveNoLocalReturnNodes($classReflection, $functionLike); } foreach ($localReturnNodes as $localReturnNode) { $returnedExprType = $this->nodeTypeResolver->getType($localReturnNode); @@ -134,29 +124,25 @@ final class ReturnedNodesReturnTypeInfererTypeInferer return $returns; } /** - * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|\PhpParser\Node\Stmt\Trait_ $classLike * @return \PHPStan\Type\VoidType|\PHPStan\Type\MixedType */ - private function resolveNoLocalReturnNodes($classLike, FunctionLike $functionLike) + private function resolveNoLocalReturnNodes(ClassReflection $classReflection, FunctionLike $functionLike) { // void type - if (!$this->isAbstractMethod($classLike, $functionLike)) { + if (!$this->isAbstractMethod($classReflection, $functionLike)) { return new VoidType(); } return new MixedType(); } - /** - * @param \PhpParser\Node\Stmt\Class_|\PhpParser\Node\Stmt\Interface_|\PhpParser\Node\Stmt\Trait_ $classLike - */ - private function isAbstractMethod($classLike, FunctionLike $functionLike) : bool + private function isAbstractMethod(ClassReflection $classReflection, FunctionLike $functionLike) : bool { if ($functionLike instanceof ClassMethod && $functionLike->isAbstract()) { return \true; } - if (!$classLike instanceof Class_) { + if (!$classReflection->isClass()) { return \false; } - return $classLike->isAbstract(); + return $classReflection->isAbstract(); } private function inferFromReturnedMethodCall(Return_ $return, FunctionLike $originalFunctionLike) : Type { @@ -167,8 +153,21 @@ final class ReturnedNodesReturnTypeInfererTypeInferer if (!$methodReflection instanceof MethodReflection) { return new MixedType(); } - $parentClassMethod = $this->betterNodeFinder->findParentType($return, ClassMethod::class); - if ($parentClassMethod === $originalFunctionLike) { + $isReturnScoped = \false; + $this->simpleCallableNodeTraverser->traverseNodesWithCallable((array) $originalFunctionLike->getStmts(), static function (Node $subNode) use($return, &$isReturnScoped) : ?int { + if ($subNode instanceof FunctionLike && !$subNode instanceof ArrowFunction) { + return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN; + } + if (!$subNode instanceof Return_) { + return null; + } + if ($return === $subNode) { + $isReturnScoped = \true; + return NodeTraverser::STOP_TRAVERSAL; + } + return null; + }); + if ($isReturnScoped) { return new MixedType(); } return $this->resolveClassMethod($methodReflection, $originalFunctionLike); diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 8ea0db2ae75..52b63d90c14 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '06d1d0817a56a9422363beaf2fded111baa92514'; + public const PACKAGE_VERSION = '765450f3e2c7ae1ebfb07a708f20640a66681bb9'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-06-16 21:50:11'; + public const RELEASE_DATE = '2023-06-16 22:27:30'; /** * @var int */ diff --git a/vendor/autoload.php b/vendor/autoload.php index c394796040f..d0be98c3f7f 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit8f1fd784b94e3a483c3bcee5a1fc717c::getLoader(); +return ComposerAutoloaderInit1d10b4255c1e44025e6983c9dc12d0d6::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 453d50c579c..46f2416c088 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit8f1fd784b94e3a483c3bcee5a1fc717c +class ComposerAutoloaderInit1d10b4255c1e44025e6983c9dc12d0d6 { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit8f1fd784b94e3a483c3bcee5a1fc717c return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit8f1fd784b94e3a483c3bcee5a1fc717c', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit1d10b4255c1e44025e6983c9dc12d0d6', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit8f1fd784b94e3a483c3bcee5a1fc717c', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit1d10b4255c1e44025e6983c9dc12d0d6', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit1d10b4255c1e44025e6983c9dc12d0d6::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit1d10b4255c1e44025e6983c9dc12d0d6::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index 80354ba1d06..062c453759d 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c +class ComposerStaticInit1d10b4255c1e44025e6983c9dc12d0d6 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3094,9 +3094,9 @@ class ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit8f1fd784b94e3a483c3bcee5a1fc717c::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit1d10b4255c1e44025e6983c9dc12d0d6::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit1d10b4255c1e44025e6983c9dc12d0d6::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit1d10b4255c1e44025e6983c9dc12d0d6::$classMap; }, null, ClassLoader::class); }