Updated Rector to commit fb0ae6dd929596556c2e7454068ecdec5e326378

fb0ae6dd92 [Performance] Add $hasUnreachableStatementNode flag on PHPStanNodeScopeResolver to ensure execute UnreachableStatementNodeVisitor only when it has (#4416)
This commit is contained in:
Tomas Votruba 2023-07-04 13:33:50 +00:00
parent 7251a47b0d
commit abbf3c7b97
8 changed files with 43 additions and 31 deletions

View File

@ -135,7 +135,7 @@ return static function (RectorConfig $rectorConfig) : void {
foreach ($extensionConfigFiles as $extensionConfigFile) {
$rectorConfig->import($extensionConfigFile);
}
$services->load('Rector\\Core\\', __DIR__ . '/../src')->exclude([__DIR__ . '/../src/Rector', __DIR__ . '/../src/Console/Style/RectorConsoleOutputStyle.php', __DIR__ . '/../src/Exception', __DIR__ . '/../src/DependencyInjection/CompilerPass', __DIR__ . '/../src/DependencyInjection/Loader', __DIR__ . '/../src/Kernel', __DIR__ . '/../src/ValueObject', __DIR__ . '/../src/Bootstrap', __DIR__ . '/../src/Enum', __DIR__ . '/../src/functions', __DIR__ . '/../src/PhpParser/Node/CustomNode', __DIR__ . '/../src/PhpParser/ValueObject', __DIR__ . '/../src/constants.php']);
$services->load('Rector\\Core\\', __DIR__ . '/../src')->exclude([__DIR__ . '/../src/Rector', __DIR__ . '/../src/Console/Style/RectorConsoleOutputStyle.php', __DIR__ . '/../src/Exception', __DIR__ . '/../src/DependencyInjection/CompilerPass', __DIR__ . '/../src/DependencyInjection/Loader', __DIR__ . '/../src/Kernel', __DIR__ . '/../src/ValueObject', __DIR__ . '/../src/Bootstrap', __DIR__ . '/../src/Enum', __DIR__ . '/../src/functions', __DIR__ . '/../src/PhpParser/Node/CustomNode', __DIR__ . '/../src/PhpParser/ValueObject', __DIR__ . '/../src/PHPStan/NodeVisitor', __DIR__ . '/../src/constants.php']);
$services->set(ConsoleApplication::class)->arg('$commands', tagged_iterator(Command::class));
$services->alias(Application::class, ConsoleApplication::class);
$services->set(EmptyConfigurableRectorCollector::class)->arg('$containerBuilder', service('service_container'));

View File

@ -12,6 +12,7 @@ use Rector\Core\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeVisitor\FunctionLikeParamArgPositionNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory;
final class NodeScopeAndMetadataDecorator
{
/**
@ -19,6 +20,11 @@ final class NodeScopeAndMetadataDecorator
* @var \Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver
*/
private $phpStanNodeScopeResolver;
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory
*/
private $scopeFactory;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
@ -29,9 +35,10 @@ final class NodeScopeAndMetadataDecorator
* @var \PhpParser\NodeTraverser
*/
private $nodeTraverser;
public function __construct(CloningVisitor $cloningVisitor, PHPStanNodeScopeResolver $phpStanNodeScopeResolver, ParentConnectingVisitor $parentConnectingVisitor, FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor, UnreachableStatementNodeVisitor $unreachableStatementNodeVisitor, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
public function __construct(CloningVisitor $cloningVisitor, PHPStanNodeScopeResolver $phpStanNodeScopeResolver, ParentConnectingVisitor $parentConnectingVisitor, FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor, ScopeFactory $scopeFactory, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
{
$this->phpStanNodeScopeResolver = $phpStanNodeScopeResolver;
$this->scopeFactory = $scopeFactory;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
$this->nodeTraverser = new NodeTraverser();
// needed also for format preserving printing
@ -39,7 +46,6 @@ final class NodeScopeAndMetadataDecorator
// this one has to be run again to re-connect parent nodes with new attributes
$this->nodeTraverser->addVisitor($parentConnectingVisitor);
$this->nodeTraverser->addVisitor($functionLikeParamArgPositionNodeVisitor);
$this->nodeTraverser->addVisitor($unreachableStatementNodeVisitor);
}
/**
* @param Stmt[] $stmts
@ -49,6 +55,9 @@ final class NodeScopeAndMetadataDecorator
{
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$stmts = $this->phpStanNodeScopeResolver->processNodes($stmts, $file->getFilePath());
if ($this->phpStanNodeScopeResolver->hasUnreachableStatementNode()) {
$this->nodeTraverser->addVisitor(new UnreachableStatementNodeVisitor($this->phpStanNodeScopeResolver, $file->getFilePath(), $this->scopeFactory));
}
return $this->nodeTraverser->traverse($stmts);
}
}

View File

@ -126,6 +126,10 @@ final class PHPStanNodeScopeResolver
* @var \PhpParser\NodeTraverser
*/
private $nodeTraverser;
/**
* @var bool
*/
private $hasUnreachableStatementNode = \false;
/**
* @param ScopeResolverNodeVisitorInterface[] $nodeVisitors
*/
@ -151,6 +155,7 @@ final class PHPStanNodeScopeResolver
public function processNodes(array $stmts, string $filePath, ?MutatingScope $formerMutatingScope = null) : array
{
$isScopeRefreshing = $formerMutatingScope instanceof MutatingScope;
$this->hasUnreachableStatementNode = \false;
/**
* The stmts must be array of Stmt, or it will be silently skipped by PHPStan
* @see vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:282
@ -419,6 +424,11 @@ final class PHPStanNodeScopeResolver
$originalStmt->setAttribute(AttributeKey::IS_UNREACHABLE, \true);
$originalStmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->processNodes([$originalStmt], $filePath, $mutatingScope);
$this->hasUnreachableStatementNode = \true;
}
public function hasUnreachableStatementNode() : bool
{
return $this->hasUnreachableStatementNode;
}
private function processProperty(Property $property, MutatingScope $mutatingScope) : void
{

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '93a4b2b15eec15c506ae55c25b23a40dd0390cff';
public const PACKAGE_VERSION = 'fb0ae6dd929596556c2e7454068ecdec5e326378';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-07-04 15:32:33';
public const RELEASE_DATE = '2023-07-04 20:29:36';
/**
* @var int
*/

View File

@ -9,32 +9,30 @@ use PhpParser\Node\Stmt\Declare_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory;
final class UnreachableStatementNodeVisitor extends NodeVisitorAbstract
{
/**
* @readonly
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver
*/
private $phpStanNodeScopeResolver;
/**
* @readonly
* @var string
*/
private $filePath;
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory
*/
private $scopeFactory;
public function __construct(CurrentFileProvider $currentFileProvider, PHPStanNodeScopeResolver $phpStanNodeScopeResolver, ScopeFactory $scopeFactory)
public function __construct(PHPStanNodeScopeResolver $phpStanNodeScopeResolver, string $filePath, ScopeFactory $scopeFactory)
{
$this->currentFileProvider = $currentFileProvider;
$this->phpStanNodeScopeResolver = $phpStanNodeScopeResolver;
$this->filePath = $filePath;
$this->scopeFactory = $scopeFactory;
}
public function enterNode(Node $node) : ?Node
@ -45,14 +43,9 @@ final class UnreachableStatementNodeVisitor extends NodeVisitorAbstract
if ($node->stmts === null) {
return null;
}
$file = $this->currentFileProvider->getFile();
if (!$file instanceof File) {
return null;
}
$filePath = $file->getFilePath();
$isPassedUnreachableStmt = \false;
$mutatingScope = $node->getAttribute(AttributeKey::SCOPE);
$mutatingScope = $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeFactory->createFromFile($filePath);
$mutatingScope = $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeFactory->createFromFile($this->filePath);
foreach ($node->stmts as $stmt) {
if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === \true) {
$isPassedUnreachableStmt = \true;
@ -61,7 +54,7 @@ final class UnreachableStatementNodeVisitor extends NodeVisitorAbstract
if ($isPassedUnreachableStmt) {
$stmt->setAttribute(AttributeKey::IS_UNREACHABLE, \true);
$stmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->phpStanNodeScopeResolver->processNodes([$stmt], $filePath, $mutatingScope);
$this->phpStanNodeScopeResolver->processNodes([$stmt], $this->filePath, $mutatingScope);
}
}
return null;

2
vendor/autoload.php vendored
View File

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

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitd7f678f0c3ee659fc396d7770917fe96
class ComposerAutoloaderInit21870fdc7eea33227b7b302034227275
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitd7f678f0c3ee659fc396d7770917fe96
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitd7f678f0c3ee659fc396d7770917fe96', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit21870fdc7eea33227b7b302034227275', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitd7f678f0c3ee659fc396d7770917fe96', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit21870fdc7eea33227b7b302034227275', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit21870fdc7eea33227b7b302034227275::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit21870fdc7eea33227b7b302034227275::$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 ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96
class ComposerStaticInit21870fdc7eea33227b7b302034227275
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -3101,9 +3101,9 @@ class ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitd7f678f0c3ee659fc396d7770917fe96::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit21870fdc7eea33227b7b302034227275::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit21870fdc7eea33227b7b302034227275::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit21870fdc7eea33227b7b302034227275::$classMap;
}, null, ClassLoader::class);
}