Updated Rector to commit 765450f3e2c7ae1ebfb07a708f20640a66681bb9

765450f3e2 [TypeDeclaration] Remove parent lookup on ReturnedNodesReturnTypeInfererTypeInferer (#4246)
This commit is contained in:
Tomas Votruba 2023-06-16 15:32:00 +00:00
parent 6630b94daa
commit 52e59fd992
5 changed files with 39 additions and 40 deletions

View File

@ -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);

View File

@ -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
*/

2
vendor/autoload.php vendored
View File

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

View File

@ -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;

View File

@ -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);
}