Updated Rector to commit 87c677e7c304845c18ce1ccfb135aefab85b3bf4

87c677e7c3 [DX] Add ExprScopeFromStmtNodeVisitor, move logic deep Expr from ScopeAnalyzer to it (#4815)
This commit is contained in:
Tomas Votruba 2023-08-19 15:00:46 +00:00
parent 60feccd21a
commit fdac720de2
11 changed files with 76 additions and 34 deletions

View File

@ -53,6 +53,7 @@ use PHPStan\Type\TypeCombinator;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\NodeAnalyzer\ClassAnalyzer;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\PHPStan\NodeVisitor\ExprScopeFromStmtNodeVisitor;
use Rector\Core\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor;
use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
@ -206,6 +207,7 @@ final class PHPStanNodeScopeResolver
$this->nodeScopeResolver->processNodes($stmts, $scope, $nodeCallback);
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
$nodeTraverser->addVisitor(new ExprScopeFromStmtNodeVisitor($this->scopeFactory, $filePath));
$nodeTraverser->traverse($stmts);
return $stmts;
}

View File

@ -55,7 +55,7 @@ final class ChangedNodeScopeRefresher
$this->scopeAnalyzer = $scopeAnalyzer;
$this->currentFileProvider = $currentFileProvider;
}
public function refresh(Node $node, ?MutatingScope $mutatingScope, ?string $filePath = null, ?Stmt $currentStmt = null) : void
public function refresh(Node $node, ?MutatingScope $mutatingScope, ?string $filePath = null) : void
{
// nothing to refresh
if (!$this->scopeAnalyzer->isRefreshable($node)) {
@ -66,7 +66,7 @@ final class ChangedNodeScopeRefresher
$file = $this->currentFileProvider->getFile();
$filePath = $file->getFilePath();
}
$mutatingScope = $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeAnalyzer->resolveScope($node, $filePath, $currentStmt);
$mutatingScope = $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeAnalyzer->resolveScope($node, $filePath);
if (!$mutatingScope instanceof MutatingScope) {
$errorMessage = \sprintf('Node "%s" with is missing scope required for scope refresh', \get_class($node));
throw new ShouldNotHappenException($errorMessage);

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '0477f5bf115172a4abf3f5ec1cef9613d31ba21c';
public const PACKAGE_VERSION = '87c677e7c304845c18ce1ccfb135aefab85b3bf4';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-08-19 16:22:19';
public const RELEASE_DATE = '2023-08-19 16:57:49';
/**
* @var int
*/

View File

@ -5,7 +5,6 @@ namespace Rector\Core\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
@ -38,17 +37,12 @@ final class ScopeAnalyzer
}
return \true;
}
public function resolveScope(Node $node, string $filePath, ?Stmt $currentStmt = null) : ?Scope
public function resolveScope(Node $node, string $filePath) : ?Scope
{
// on File level
if ($node instanceof Stmt && $node->getAttribute(AttributeKey::STATEMENT_DEPTH) === 0) {
return $this->scopeFactory->createFromFile($filePath);
}
// too deep Expr, eg: $$param = $$bar = self::decodeValue($result->getItem()->getTextContent());
if ($node instanceof Expr && $node->getAttribute(AttributeKey::EXPRESSION_DEPTH) >= 2) {
$scope = $currentStmt instanceof Stmt ? $currentStmt->getAttribute(AttributeKey::SCOPE) : $this->scopeFactory->createFromFile($filePath);
return $scope instanceof Scope ? $scope : $this->scopeFactory->createFromFile($filePath);
}
/**
* Node and parent Node doesn't has Scope, and Node Start token pos is < 0,
* it means the node and parent node just re-printed, the Scope need to be resolved from file

View File

@ -0,0 +1,53 @@
<?php
declare (strict_types=1);
namespace Rector\Core\PHPStan\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory;
final class ExprScopeFromStmtNodeVisitor extends NodeVisitorAbstract
{
/**
* @readonly
* @var \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory
*/
private $scopeFactory;
/**
* @var string
*/
private $filePath;
/**
* @var \PhpParser\Node\Stmt|null
*/
private $currentStmt;
public function __construct(ScopeFactory $scopeFactory, string $filePath)
{
$this->scopeFactory = $scopeFactory;
$this->filePath = $filePath;
}
public function enterNode(Node $node) : ?Node
{
if ($node instanceof Stmt) {
$this->currentStmt = $node;
return null;
}
if (!$node instanceof Expr || $node->getAttribute(AttributeKey::EXPRESSION_DEPTH) < 2) {
return null;
}
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope instanceof Scope) {
return null;
}
// too deep Expr, eg: $$param = $$bar = self::decodeValue($result->getItem()->getTextContent());
$filePath = $this->filePath;
$scope = $this->currentStmt instanceof Stmt ? $this->currentStmt->getAttribute(AttributeKey::SCOPE) : $this->scopeFactory->createFromFile($filePath);
$scope = $scope instanceof Scope ? $scope : $this->scopeFactory->createFromFile($filePath);
$node->setAttribute(AttributeKey::SCOPE, $scope);
return null;
}
}

View File

@ -4,7 +4,6 @@ declare (strict_types=1);
namespace Rector\Core\Rector;
use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeTraverser;
@ -85,10 +84,6 @@ CODE_SAMPLE;
* @var \Rector\Core\ValueObject\Application\File
*/
protected $file;
/**
* @var \PhpParser\Node\Stmt|null
*/
protected $currentStmt;
/**
* @var \Rector\Core\Application\ChangedNodeScopeRefresher
*/
@ -308,20 +303,16 @@ CODE_SAMPLE;
{
$nodes = $node instanceof Node ? [$node] : $node;
foreach ($nodes as $node) {
$this->changedNodeScopeRefresher->refresh($node, $mutatingScope, $filePath, $this->currentStmt);
$this->changedNodeScopeRefresher->refresh($node, $mutatingScope, $filePath);
}
}
private function isMatchingNodeType(Node $node) : bool
{
$nodeClass = \get_class($node);
foreach ($this->getNodeTypes() as $nodeType) {
if (!\is_a($nodeClass, $nodeType, \true)) {
if ($node instanceof Stmt) {
$this->currentStmt = $node;
}
continue;
if (\is_a($nodeClass, $nodeType, \true)) {
return \true;
}
return \true;
}
return \false;
}

View File

@ -30,7 +30,7 @@ abstract class AbstractScopeAwareRector extends \Rector\Core\Rector\AbstractRect
/** @var MutatingScope|null $currentScope */
$currentScope = $node->getAttribute(AttributeKey::SCOPE);
if (!$currentScope instanceof MutatingScope) {
$currentScope = $this->scopeAnalyzer->resolveScope($node, $this->file->getFilePath(), $this->currentStmt);
$currentScope = $this->scopeAnalyzer->resolveScope($node, $this->file->getFilePath());
}
if (!$currentScope instanceof Scope) {
$errorMessage = \sprintf('Scope not available on "%s" node, but is required by a refactorWithScope() method of "%s" rule. Fix scope refresh on changed nodes first', \get_class($node), static::class);

2
vendor/autoload.php vendored
View File

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

View File

@ -1257,6 +1257,7 @@ return array(
'Rector\\Core\\NodeManipulator\\PropertyFetchAssignManipulator' => $baseDir . '/src/NodeManipulator/PropertyFetchAssignManipulator.php',
'Rector\\Core\\NodeManipulator\\PropertyManipulator' => $baseDir . '/src/NodeManipulator/PropertyManipulator.php',
'Rector\\Core\\NodeManipulator\\StmtsManipulator' => $baseDir . '/src/NodeManipulator/StmtsManipulator.php',
'Rector\\Core\\PHPStan\\NodeVisitor\\ExprScopeFromStmtNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/ExprScopeFromStmtNodeVisitor.php',
'Rector\\Core\\PHPStan\\NodeVisitor\\UnreachableStatementNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php',
'Rector\\Core\\PHPStan\\NodeVisitor\\WrappedNodeRestoringNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php',
'Rector\\Core\\PHPStan\\Reflection\\TypeToCallReflectionResolver\\ClosureTypeToCallReflectionResolver' => $baseDir . '/src/PHPStan/Reflection/TypeToCallReflectionResolver/ClosureTypeToCallReflectionResolver.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitbecbf5aebe9e3a0a73feebf7723c4c05
class ComposerAutoloaderInit77afcf07e5219d1649b8f2904235e51c
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitbecbf5aebe9e3a0a73feebf7723c4c05
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitbecbf5aebe9e3a0a73feebf7723c4c05', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit77afcf07e5219d1649b8f2904235e51c', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitbecbf5aebe9e3a0a73feebf7723c4c05', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit77afcf07e5219d1649b8f2904235e51c', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit77afcf07e5219d1649b8f2904235e51c::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit77afcf07e5219d1649b8f2904235e51c::$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 ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05
class ComposerStaticInit77afcf07e5219d1649b8f2904235e51c
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -1489,6 +1489,7 @@ class ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05
'Rector\\Core\\NodeManipulator\\PropertyFetchAssignManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/PropertyFetchAssignManipulator.php',
'Rector\\Core\\NodeManipulator\\PropertyManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/PropertyManipulator.php',
'Rector\\Core\\NodeManipulator\\StmtsManipulator' => __DIR__ . '/../..' . '/src/NodeManipulator/StmtsManipulator.php',
'Rector\\Core\\PHPStan\\NodeVisitor\\ExprScopeFromStmtNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/ExprScopeFromStmtNodeVisitor.php',
'Rector\\Core\\PHPStan\\NodeVisitor\\UnreachableStatementNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php',
'Rector\\Core\\PHPStan\\NodeVisitor\\WrappedNodeRestoringNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php',
'Rector\\Core\\PHPStan\\Reflection\\TypeToCallReflectionResolver\\ClosureTypeToCallReflectionResolver' => __DIR__ . '/../..' . '/src/PHPStan/Reflection/TypeToCallReflectionResolver/ClosureTypeToCallReflectionResolver.php',
@ -2638,9 +2639,9 @@ class ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitbecbf5aebe9e3a0a73feebf7723c4c05::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit77afcf07e5219d1649b8f2904235e51c::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit77afcf07e5219d1649b8f2904235e51c::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit77afcf07e5219d1649b8f2904235e51c::$classMap;
}, null, ClassLoader::class);
}