diff --git a/docs/AllRectorsOverview.md b/docs/AllRectorsOverview.md index 8b7d2c3815f..6aa52d59b60 100644 --- a/docs/AllRectorsOverview.md +++ b/docs/AllRectorsOverview.md @@ -3857,7 +3857,7 @@ services: - class: `Rector\Php\Rector\FuncCall\PregReplaceEModifierRector` -The /e modifier is no longer supported, use preg_replace_callback instead +The /e modifier is no longer supported, use preg_replace_callback instead ```diff class SomeClass diff --git a/docs/NodesOverview.md b/docs/NodesOverview.md index d84b5e95620..e5a0a4b3b9d 100644 --- a/docs/NodesOverview.md +++ b/docs/NodesOverview.md @@ -917,7 +917,7 @@ if (true) { ```php ?> -feelfeel diff --git a/packages/NodeTypeResolver/src/NodeScopeAndMetadataDecorator.php b/packages/NodeTypeResolver/src/NodeScopeAndMetadataDecorator.php index a7a0ca37356..d880ce6366b 100644 --- a/packages/NodeTypeResolver/src/NodeScopeAndMetadataDecorator.php +++ b/packages/NodeTypeResolver/src/NodeScopeAndMetadataDecorator.php @@ -13,7 +13,6 @@ use Rector\NodeTypeResolver\NodeVisitor\NamespaceNodeVisitor; use Rector\NodeTypeResolver\NodeVisitor\NodeCollectorNodeVisitor; use Rector\NodeTypeResolver\NodeVisitor\ParentAndNextNodeVisitor; use Rector\NodeTypeResolver\PHPStan\Scope\NodeScopeResolver; -use Rector\PhpParser\Node\BetterNodeFinder; final class NodeScopeAndMetadataDecorator { @@ -57,11 +56,6 @@ final class NodeScopeAndMetadataDecorator */ private $nodeCollectorNodeVisitor; - /** - * @var BetterNodeFinder - */ - private $betterNodeFinder; - public function __construct( NodeScopeResolver $nodeScopeResolver, ParentAndNextNodeVisitor $parentAndNextNodeVisitor, @@ -70,8 +64,7 @@ final class NodeScopeAndMetadataDecorator NamespaceNodeVisitor $namespaceNodeVisitor, ExpressionNodeVisitor $expressionNodeVisitor, FileInfoNodeVisitor $fileInfoNodeVisitor, - NodeCollectorNodeVisitor $nodeCollectorNodeVisitor, - BetterNodeFinder $betterNodeFinder + NodeCollectorNodeVisitor $nodeCollectorNodeVisitor ) { $this->nodeScopeResolver = $nodeScopeResolver; $this->parentAndNextNodeVisitor = $parentAndNextNodeVisitor; @@ -81,7 +74,6 @@ final class NodeScopeAndMetadataDecorator $this->expressionNodeVisitor = $expressionNodeVisitor; $this->fileInfoNodeVisitor = $fileInfoNodeVisitor; $this->nodeCollectorNodeVisitor = $nodeCollectorNodeVisitor; - $this->betterNodeFinder = $betterNodeFinder; } /** @@ -97,15 +89,6 @@ final class NodeScopeAndMetadataDecorator ])); $nodes = $nodeTraverser->traverse($nodes); - // dual run of these is conflicting with anonymous classes, e.g. in \Rector\DeadCode\Rector\ClassMethod\RemoveUnusedParameterRector; solve later - if ($this->betterNodeFinder->findFirstInstanceOf($nodes, Node\Stmt\Trait_::class)) { - // needed for trait scoping - $nodeTraverser = new NodeTraverser(); - $nodeTraverser->addVisitor($this->namespaceNodeVisitor); - $nodeTraverser->addVisitor($this->classAndMethodNodeVisitor); - $nodes = $nodeTraverser->traverse($nodes); - } - $nodes = $this->nodeScopeResolver->processNodes($nodes, $filePath); $nodeTraverser = new NodeTraverser(); diff --git a/packages/NodeTypeResolver/src/NodeTypeResolver.php b/packages/NodeTypeResolver/src/NodeTypeResolver.php index 824e313a5ac..ee8fadae0c8 100644 --- a/packages/NodeTypeResolver/src/NodeTypeResolver.php +++ b/packages/NodeTypeResolver/src/NodeTypeResolver.php @@ -400,7 +400,6 @@ final class NodeTypeResolver { // nodes that cannot be resolver by PHPStan $nodeClass = get_class($node); - if (isset($this->perNodeTypeResolvers[$nodeClass])) { return $this->perNodeTypeResolvers[$nodeClass]->resolve($node); } diff --git a/packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php b/packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php index f67f37f3262..0bcbce38a9d 100644 --- a/packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php +++ b/packages/NodeTypeResolver/src/PHPStan/Scope/NodeScopeResolver.php @@ -69,7 +69,6 @@ final class NodeScopeResolver { $this->removeDeepChainMethodCallNodes($nodes); - $this->phpStanNodeScopeResolver->setAnalysedFiles([$filePath]); $scope = $this->scopeFactory->createFromFile($filePath); // skip chain method calls, performance issue: https://github.com/phpstan/phpstan/issues/254 diff --git a/src/Application/RectorApplication.php b/src/Application/RectorApplication.php index 2c4078195ca..0c8b074b03b 100644 --- a/src/Application/RectorApplication.php +++ b/src/Application/RectorApplication.php @@ -3,6 +3,7 @@ namespace Rector\Application; use PHPStan\AnalysedCodeException; +use PHPStan\Analyser\NodeScopeResolver; use Rector\Application\FileSystem\RemovedAndAddedFilesCollector; use Rector\Application\FileSystem\RemovedAndAddedFilesProcessor; use Rector\Configuration\Configuration; @@ -68,6 +69,11 @@ final class RectorApplication */ private $enabledRectorsProvider; + /** + * @var NodeScopeResolver + */ + private $nodeScopeResolver; + public function __construct( SymfonyStyle $symfonyStyle, FileSystemFileProcessor $fileSystemFileProcessor, @@ -76,7 +82,8 @@ final class RectorApplication FileProcessor $fileProcessor, EnabledRectorsProvider $enabledRectorsProvider, RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, - RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor + RemovedAndAddedFilesProcessor $removedAndAddedFilesProcessor, + NodeScopeResolver $nodeScopeResolver ) { $this->symfonyStyle = $symfonyStyle; $this->fileSystemFileProcessor = $fileSystemFileProcessor; @@ -86,6 +93,7 @@ final class RectorApplication $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; $this->removedAndAddedFilesProcessor = $removedAndAddedFilesProcessor; $this->enabledRectorsProvider = $enabledRectorsProvider; + $this->nodeScopeResolver = $nodeScopeResolver; } /** @@ -103,6 +111,9 @@ final class RectorApplication $this->symfonyStyle->progressStart($fileCount * 3); } + // PHPStan has to know about all files! + $this->configurePHPStanNodeScopeResolver($fileInfos); + // 1. parse files to nodes foreach ($fileInfos as $fileInfo) { $this->tryCatchWrapper($fileInfo, function (SmartFileInfo $smartFileInfo): void { @@ -192,4 +203,17 @@ final class RectorApplication $this->symfonyStyle->progressAdvance(); } } + + /** + * @param SmartFileInfo[] $fileInfos + */ + private function configurePHPStanNodeScopeResolver(array $fileInfos): void + { + $filePaths = []; + foreach ($fileInfos as $fileInfo) { + $filePaths[] = $fileInfo->getRealPath(); + } + + $this->nodeScopeResolver->setAnalysedFiles($filePaths); + } }