From 5acf36baffc0083faddfc2a7843b986535eb5997 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 20 Mar 2023 08:30:24 +0000 Subject: [PATCH] Updated Rector to commit 7dcd5f6503afa3ee7ab086cbfe6afb71cbd6d6ec https://github.com/rectorphp/rector-src/commit/7dcd5f6503afa3ee7ab086cbfe6afb71cbd6d6ec Performance: Improve AstResolver caching (#3485) --- src/Application/VersionResolver.php | 4 +- src/PhpParser/AstResolver.php | 85 +++++++++-------------------- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 10 ++-- vendor/composer/autoload_static.php | 8 +-- 5 files changed, 37 insertions(+), 72 deletions(-) diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 0375c307e437..f942ed237473 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 = '5371408babdacb17ccbbe89b4c74281dcb4825c1'; + public const PACKAGE_VERSION = '7dcd5f6503afa3ee7ab086cbfe6afb71cbd6d6ec'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-03-19 21:23:49'; + public const RELEASE_DATE = '2023-03-20 15:25:39'; /** * @var int */ diff --git a/src/PhpParser/AstResolver.php b/src/PhpParser/AstResolver.php index 5e7395d09a2b..09ee5bb97b89 100644 --- a/src/PhpParser/AstResolver.php +++ b/src/PhpParser/AstResolver.php @@ -42,18 +42,11 @@ final class AstResolver { /** * Parsing files is very heavy performance, so this will help to leverage it - * The value can be also null, as the method might not exist in the class. + * The value can be also null, when no statements could be parsed from the file. * - * @var array> + * @var array */ - private $classMethodsByClassAndMethod = []; - /** - * Parsing files is very heavy performance, so this will help to leverage it - * The value can be also null, as the method might not exist in the class. - * - * @var array> - */ - private $functionsByName = []; + private $parsedFileNodes = []; /** * @readonly * @var \Rector\PhpDocParser\PhpParser\SmartPhpParser @@ -121,13 +114,6 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR $classReflection = $methodReflection->getDeclaringClass(); $classLikeName = $classReflection->getName(); $methodName = $methodReflection->getName(); - if (isset($this->classMethodsByClassAndMethod[$classLikeName][$methodName])) { - return $this->classMethodsByClassAndMethod[$classLikeName][$methodName]; - } - // saved as null data - if (\array_key_exists($classLikeName, $this->classMethodsByClassAndMethod) && \array_key_exists($methodName, $this->classMethodsByClassAndMethod[$classLikeName])) { - return null; - } $fileName = $classReflection->getFileName(); // probably native PHP method → un-parseable if ($fileName === null) { @@ -137,21 +123,13 @@ public function resolveClassMethodFromMethodReflection(MethodReflection $methodR if ($nodes === null) { return null; } - /** @var ClassLike[] $classLikes */ - $classLikes = $this->betterNodeFinder->findInstanceOf($nodes, ClassLike::class); - foreach ($classLikes as $classLike) { - if (!$this->nodeNameResolver->isName($classLike, $classLikeName)) { - continue; - } - $classMethod = $classLike->getMethod($methodName); - if (!$classMethod instanceof ClassMethod) { - continue; - } - $this->classMethodsByClassAndMethod[$classLikeName][$methodName] = $classMethod; - return $classMethod; + /** @var ClassLike|null $classLike */ + $classLike = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($classLikeName, $methodName) : bool { + return $node instanceof ClassLike && $this->nodeNameResolver->isName($node, $classLikeName) && $node->getMethod($methodName) instanceof ClassMethod; + }); + if ($classLike instanceof ClassLike && ($method = $classLike->getMethod($methodName)) instanceof ClassMethod) { + return $method; } - // avoids looking for a class in a file where is not present - $this->classMethodsByClassAndMethod[$classLikeName][$methodName] = null; return null; } /** @@ -168,13 +146,6 @@ public function resolveClassMethodOrFunctionFromCall($call, Scope $scope) public function resolveFunctionFromFunctionReflection(FunctionReflection $functionReflection) : ?Function_ { $functionName = $functionReflection->getName(); - if (isset($this->functionsByName[$functionName])) { - return $this->functionsByName[$functionName]; - } - // saved as null data - if (\array_key_exists($functionName, $this->functionsByName)) { - return null; - } $fileName = $functionReflection->getFileName(); if ($fileName === null) { return null; @@ -183,19 +154,11 @@ public function resolveFunctionFromFunctionReflection(FunctionReflection $functi if ($nodes === null) { return null; } - /** @var Function_[] $functions */ - $functions = $this->betterNodeFinder->findInstanceOf($nodes, Function_::class); - foreach ($functions as $function) { - if (!$this->nodeNameResolver->isName($function, $functionName)) { - continue; - } - // to avoid parsing missing function again - $this->functionsByName[$functionName] = $function; - return $function; - } - // to avoid parsing missing function again - $this->functionsByName[$functionName] = null; - return null; + /** @var Function_|null $function */ + $function = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($functionName) : bool { + return $node instanceof Function_ && $this->nodeNameResolver->isName($node, $functionName); + }); + return $function; } /** * @param class-string $className @@ -282,12 +245,12 @@ public function resolvePropertyFromPropertyReflection(PhpPropertyReflection $php } $nativeReflectionProperty = $phpPropertyReflection->getNativeReflection(); $desiredPropertyName = $nativeReflectionProperty->getName(); - /** @var Property[] $properties */ - $properties = $this->betterNodeFinder->findInstanceOf($nodes, Property::class); - foreach ($properties as $property) { - if ($this->nodeNameResolver->isName($property, $desiredPropertyName)) { - return $property; - } + /** @var Property|null $property */ + $property = $this->betterNodeFinder->findFirst($nodes, function (Node $node) use($desiredPropertyName) : bool { + return $node instanceof Property && $this->nodeNameResolver->isName($node, $desiredPropertyName); + }); + if ($property instanceof Property) { + return $property; } // promoted property return $this->findPromotedPropertyByName($nodes, $desiredPropertyName); @@ -300,7 +263,6 @@ private function locateClassMethodInTrait(string $methodName, MethodReflection $ $classMethod = $this->betterNodeFinder->findFirst($traits, function (Node $node) use($methodName) : bool { return $node instanceof ClassMethod && $this->nodeNameResolver->isName($node, $methodName); }); - $this->classMethodsByClassAndMethod[$classReflection->getName()][$methodName] = $classMethod; if ($classMethod instanceof ClassMethod) { return $classMethod; } @@ -311,12 +273,15 @@ private function locateClassMethodInTrait(string $methodName, MethodReflection $ */ private function parseFileNameToDecoratedNodes(string $fileName) : ?array { + if (isset($this->parsedFileNodes[$fileName])) { + return $this->parsedFileNodes[$fileName]; + } $stmts = $this->smartPhpParser->parseFile($fileName); if ($stmts === []) { - return null; + return $this->parsedFileNodes[$fileName] = null; } $file = new File($fileName, FileSystem::read($fileName)); - return $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts); + return $this->parsedFileNodes[$fileName] = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts); } /** * @param Stmt[] $stmts diff --git a/vendor/autoload.php b/vendor/autoload.php index d96ff7b52daf..9b4863532ed4 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e::getLoader(); +return ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 7ed93b6fc9f6..2c5f0a2eb902 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e +class ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67 { private static $loader; @@ -22,17 +22,17 @@ public static function getLoader() return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInitb7db68037830ca516c20ec2b65c9251e', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInit42dbbea9f76bc2ad016504e8bbeaac67', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$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 009b918951b2..4e799e52e3dc 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInitb7db68037830ca516c20ec2b65c9251e +class ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3127,9 +3127,9 @@ class ComposerStaticInitb7db68037830ca516c20ec2b65c9251e public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInitb7db68037830ca516c20ec2b65c9251e::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInit42dbbea9f76bc2ad016504e8bbeaac67::$classMap; }, null, ClassLoader::class); }