Skip to content

Commit

Permalink
Updated Rector to commit 6947eb7c4cf8cefe0e7a4cb95bbae23ab64362bd
Browse files Browse the repository at this point in the history
rectorphp/rector-src@6947eb7 [NodeTypeResolver] Only use FileWithoutNamespaceNodeTraverser once on NodeScopeAndMetadataDecorator (#4369)
  • Loading branch information
TomasVotruba committed Jun 27, 2023
1 parent a3231bb commit bbcd259
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 69 deletions.
10 changes: 1 addition & 9 deletions packages/FileSystemRector/Parser/FileInfoParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use RectorPrefix202306\Nette\Utils\FileSystem;
use PhpParser\Node\Stmt;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -20,11 +19,6 @@ final class FileInfoParser
* @var \Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator
*/
private $nodeScopeAndMetadataDecorator;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
*/
private $fileWithoutNamespaceNodeTraverser;
/**
* @readonly
* @var \Rector\Core\PhpParser\Parser\RectorParser
Expand All @@ -35,10 +29,9 @@ final class FileInfoParser
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser, RectorParser $rectorParser, CurrentFileProvider $currentFileProvider)
public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, RectorParser $rectorParser, CurrentFileProvider $currentFileProvider)
{
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
$this->rectorParser = $rectorParser;
$this->currentFileProvider = $currentFileProvider;
}
Expand All @@ -49,7 +42,6 @@ public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataD
public function parseFileInfoToNodesAndDecorate(string $filePath) : array
{
$stmts = $this->rectorParser->parseFile($filePath);
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$file = new File($filePath, FileSystem::read($filePath));
$stmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $stmts);
$file->hydrateStmtsAndTokens($stmts, $stmts, []);
Expand Down
10 changes: 9 additions & 1 deletion packages/NodeTypeResolver/NodeScopeAndMetadataDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor\CloningVisitor;
use PhpParser\NodeVisitor\ParentConnectingVisitor;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeVisitor\FunctionLikeParamArgPositionNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
Expand All @@ -17,14 +18,20 @@ final class NodeScopeAndMetadataDecorator
* @var \Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver
*/
private $phpStanNodeScopeResolver;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
*/
private $fileWithoutNamespaceNodeTraverser;
/**
* @readonly
* @var \PhpParser\NodeTraverser
*/
private $nodeTraverser;
public function __construct(CloningVisitor $cloningVisitor, PHPStanNodeScopeResolver $phpStanNodeScopeResolver, ParentConnectingVisitor $parentConnectingVisitor, FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor)
public function __construct(CloningVisitor $cloningVisitor, PHPStanNodeScopeResolver $phpStanNodeScopeResolver, ParentConnectingVisitor $parentConnectingVisitor, FunctionLikeParamArgPositionNodeVisitor $functionLikeParamArgPositionNodeVisitor, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
{
$this->phpStanNodeScopeResolver = $phpStanNodeScopeResolver;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
$this->nodeTraverser = new NodeTraverser();
// needed also for format preserving printing
$this->nodeTraverser->addVisitor($cloningVisitor);
Expand All @@ -38,6 +45,7 @@ public function __construct(CloningVisitor $cloningVisitor, PHPStanNodeScopeReso
*/
public function decorateNodesFromFile(File $file, array $stmts) : array
{
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$stmts = $this->phpStanNodeScopeResolver->processNodes($stmts, $file->getFilePath());
return $this->nodeTraverser->traverse($stmts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
Expand Down Expand Up @@ -113,11 +112,6 @@ final class PHPStanNodeScopeResolver
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
*/
private $classAnalyzer;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
*/
private $fileWithoutNamespaceNodeTraverser;
/**
* @var string
*/
Expand All @@ -130,7 +124,7 @@ final class PHPStanNodeScopeResolver
/**
* @param ScopeResolverNodeVisitorInterface[] $nodeVisitors
*/
public function __construct(ChangedFilesDetector $changedFilesDetector, DependencyResolver $dependencyResolver, NodeScopeResolver $nodeScopeResolver, ReflectionProvider $reflectionProvider, iterable $nodeVisitors, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, PrivatesAccessor $privatesAccessor, NodeNameResolver $nodeNameResolver, ClassAnalyzer $classAnalyzer, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
public function __construct(ChangedFilesDetector $changedFilesDetector, DependencyResolver $dependencyResolver, NodeScopeResolver $nodeScopeResolver, ReflectionProvider $reflectionProvider, iterable $nodeVisitors, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, PrivatesAccessor $privatesAccessor, NodeNameResolver $nodeNameResolver, ClassAnalyzer $classAnalyzer)
{
$this->changedFilesDetector = $changedFilesDetector;
$this->dependencyResolver = $dependencyResolver;
Expand All @@ -140,7 +134,6 @@ public function __construct(ChangedFilesDetector $changedFilesDetector, Dependen
$this->privatesAccessor = $privatesAccessor;
$this->nodeNameResolver = $nodeNameResolver;
$this->classAnalyzer = $classAnalyzer;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
$this->nodeTraverser = new NodeTraverser();
foreach ($nodeVisitors as $nodeVisitor) {
$this->nodeTraverser->addVisitor($nodeVisitor);
Expand All @@ -158,22 +151,14 @@ public function processNodes(array $stmts, string $filePath, ?MutatingScope $for
* @see vendor/phpstan/phpstan/phpstan.phar/src/Analyser/NodeScopeResolver.php:282
*/
Assert::allIsInstanceOf($stmts, Stmt::class);
$isInitFileWithoutNamespace = \false;
if (!$isScopeRefreshing && !\current($stmts) instanceof FileWithoutNamespace) {
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);
$currentStmt = \current($stmts);
if ($currentStmt instanceof FileWithoutNamespace) {
$this->nodeTraverser->traverse($stmts);
$stmts = $currentStmt->stmts;
$isInitFileWithoutNamespace = \true;
}
}
if (!$isInitFileWithoutNamespace) {
$this->nodeTraverser->traverse($stmts);
}
$this->nodeTraverser->traverse($stmts);
$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);
// skip chain method calls, performance issue: https://github.com/phpstan/phpstan/issues/254
$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use(&$nodeCallback, $isScopeRefreshing, $filePath) : void {
if ($node instanceof FileWithoutNamespace) {
$this->nodeScopeResolver->processNodes($node->stmts, $mutatingScope, $nodeCallback);
return;
}
if (($node instanceof Expression || $node instanceof Return_ || $node instanceof Assign || $node instanceof EnumCase || $node instanceof AssignOp || $node instanceof Cast) && $node->expr instanceof Expr) {
$node->expr->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
Expand Down
16 changes: 12 additions & 4 deletions rules/Arguments/Rector/ClassMethod/ArgumentAdderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,19 @@ private function shouldSkipParameter($node, ArgumentAdder $argumentAdder) : bool
}
// Check if default value is the same
$classMethod = $this->astResolver->resolveClassMethodFromCall($node);
if ($classMethod instanceof ClassMethod && isset($classMethod->params[$position]) && !$this->changedArgumentsDetector->isDefaultValueChanged($classMethod->params[$position], $argumentAdder->getArgumentDefaultValue())) {
return \true;
if (!$classMethod instanceof ClassMethod) {
// is correct scope?
return !$this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
if (!isset($classMethod->params[$position])) {
// is correct scope?
return !$this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
if ($this->changedArgumentsDetector->isDefaultValueChanged($classMethod->params[$position], $argumentAdder->getArgumentDefaultValue())) {
// is correct scope?
return !$this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
}
// is correct scope?
return !$this->argumentAddingScope->isInCorrectScope($node, $argumentAdder);
return \true;
}
/**
* @param mixed $defaultValue
Expand Down
14 changes: 2 additions & 12 deletions rules/Naming/Naming/UseImportsResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Use_;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
final class UseImportsResolver
Expand All @@ -19,15 +18,9 @@ final class UseImportsResolver
* @var \Rector\Core\Provider\CurrentFileProvider
*/
private $currentFileProvider;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
*/
private $fileWithoutNamespaceNodeTraverser;
public function __construct(CurrentFileProvider $currentFileProvider, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
public function __construct(CurrentFileProvider $currentFileProvider)
{
$this->currentFileProvider = $currentFileProvider;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
}
/**
* @return \PhpParser\Node\Stmt\Namespace_|\Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace|null
Expand Down Expand Up @@ -56,10 +49,7 @@ private function resolveNamespace()
}
$currentStmt = \current($newStmts);
if (!$currentStmt instanceof FileWithoutNamespace) {
$newStmts = $this->fileWithoutNamespaceNodeTraverser->traverse($newStmts);
/** @var FileWithoutNamespace $currentStmt */
$currentStmt = \current($newStmts);
return $currentStmt;
return null;
}
return $currentStmt;
}
Expand Down
12 changes: 2 additions & 10 deletions src/Application/FileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare (strict_types=1);
namespace Rector\Core\Application;

use Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser;
use Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -25,17 +24,11 @@ final class FileProcessor
* @var \Rector\Core\PhpParser\NodeTraverser\RectorNodeTraverser
*/
private $rectorNodeTraverser;
/**
* @readonly
* @var \Rector\Core\PhpParser\NodeTraverser\FileWithoutNamespaceNodeTraverser
*/
private $fileWithoutNamespaceNodeTraverser;
public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, RectorParser $rectorParser, RectorNodeTraverser $rectorNodeTraverser, FileWithoutNamespaceNodeTraverser $fileWithoutNamespaceNodeTraverser)
public function __construct(NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, RectorParser $rectorParser, RectorNodeTraverser $rectorNodeTraverser)
{
$this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator;
$this->rectorParser = $rectorParser;
$this->rectorNodeTraverser = $rectorNodeTraverser;
$this->fileWithoutNamespaceNodeTraverser = $fileWithoutNamespaceNodeTraverser;
}
public function parseFileInfoToLocalCache(File $file) : void
{
Expand All @@ -48,8 +41,7 @@ public function parseFileInfoToLocalCache(File $file) : void
}
public function refactor(File $file) : void
{
$newStmts = $this->fileWithoutNamespaceNodeTraverser->traverse($file->getNewStmts());
$newStmts = $this->rectorNodeTraverser->traverse($newStmts);
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts());
$file->changeNewStmts($newStmts);
}
}
4 changes: 2 additions & 2 deletions src/Application/VersionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '40a1f34b12c548f7be05f02a084be54e8374a58a';
public const PACKAGE_VERSION = '6947eb7c4cf8cefe0e7a4cb95bbae23ab64362bd';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-06-28 00:27:59';
public const RELEASE_DATE = '2023-06-28 03:22:05';
/**
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion vendor/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@

require_once __DIR__ . '/composer/autoload_real.php';

return ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89::getLoader();
return ComposerAutoloaderInite7dd856a68ca53c3b92f1068f8078756::getLoader();
10 changes: 5 additions & 5 deletions vendor/composer/autoload_real.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// autoload_real.php @generated by Composer

class ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89
class ComposerAutoloaderInite7dd856a68ca53c3b92f1068f8078756
{
private static $loader;

Expand All @@ -22,17 +22,17 @@ public static function getLoader()
return self::$loader;
}

spl_autoload_register(array('ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInite7dd856a68ca53c3b92f1068f8078756', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInit164e89c268f0277d2e62d6c923fb9f89', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInite7dd856a68ca53c3b92f1068f8078756', 'loadClassLoader'));

require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInite7dd856a68ca53c3b92f1068f8078756::getInitializer($loader));

$loader->setClassMapAuthoritative(true);
$loader->register(true);

$filesToLoad = \Composer\Autoload\ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInite7dd856a68ca53c3b92f1068f8078756::$files;
$requireFile = \Closure::bind(static function ($fileIdentifier, $file) {
if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
$GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
Expand Down
8 changes: 4 additions & 4 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Composer\Autoload;

class ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89
class ComposerStaticInite7dd856a68ca53c3b92f1068f8078756
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
Expand Down Expand Up @@ -3098,9 +3098,9 @@ class ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit164e89c268f0277d2e62d6c923fb9f89::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInite7dd856a68ca53c3b92f1068f8078756::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInite7dd856a68ca53c3b92f1068f8078756::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInite7dd856a68ca53c3b92f1068f8078756::$classMap;

}, null, ClassLoader::class);
}
Expand Down

0 comments on commit bbcd259

Please sign in to comment.