Updated Rector to commit a74c640bc1043f19006c7403461108f61d8bf2a5

a74c640bc1 [Traverser] Add ByRefVariableNodeVisitor (#3821)
This commit is contained in:
Tomas Votruba 2023-05-13 17:37:31 +00:00
parent c27fd387f2
commit 6eead6b8f7
9 changed files with 96 additions and 60 deletions

View File

@ -178,4 +178,8 @@ final class AttributeKey
* @var string
*/
public const IS_STATIC_VAR = 'is_static_var';
/**
* @var string
*/
public const IS_BYREF_VAR = 'is_byref_var';
}

View File

@ -0,0 +1,74 @@
<?php
declare (strict_types=1);
namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\NodeVisitorAbstract;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
final class ByRefVariableNodeVisitor extends NodeVisitorAbstract
{
/**
* @readonly
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
public function __construct(SimpleCallableNodeTraverser $simpleCallableNodeTraverser)
{
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
}
public function enterNode(Node $node) : ?Node
{
if ($node instanceof AssignRef) {
$node->expr->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
return null;
}
if (!$node instanceof FunctionLike) {
return null;
}
$byRefVariableNames = $this->resolveClosureUseIsByRefAttribute($node, []);
foreach ($node->getParams() as $param) {
if ($param->byRef && $param->var instanceof Variable) {
$param->var->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
$byRefVariableNames[] = $param->var->name;
}
}
$stmts = $node->getStmts();
if ($stmts === null) {
return null;
}
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, static function (Node $subNode) use($byRefVariableNames) : ?\PhpParser\Node\Expr\Variable {
if (!$subNode instanceof Variable) {
return null;
}
if (!\in_array($subNode->name, $byRefVariableNames, \true)) {
return null;
}
$subNode->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
return $subNode;
});
return null;
}
/**
* @param string[] $byRefVariableNames
* @return string[]
*/
private function resolveClosureUseIsByRefAttribute(FunctionLike $functionLike, array $byRefVariableNames) : array
{
if (!$functionLike instanceof Closure) {
return $byRefVariableNames;
}
foreach ($functionLike->uses as $closureUse) {
if ($closureUse->byRef && \is_string($closureUse->var->name)) {
$closureUse->var->setAttribute(AttributeKey::IS_BYREF_VAR, \true);
$byRefVariableNames[] = $closureUse->var->name;
}
}
return $byRefVariableNames;
}
}

View File

@ -50,6 +50,7 @@ use Rector\Core\Util\Reflection\PrivatesAccessor;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\AssignedToNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\ByRefVariableNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\GlobalVariableNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\StaticVariableNodeVisitor;
@ -114,7 +115,7 @@ final class PHPStanNodeScopeResolver
* @var \Rector\Core\NodeAnalyzer\ClassAnalyzer
*/
private $classAnalyzer;
public function __construct(ChangedFilesDetector $changedFilesDetector, DependencyResolver $dependencyResolver, NodeScopeResolver $nodeScopeResolver, ReflectionProvider $reflectionProvider, RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor, AssignedToNodeVisitor $assignedToNodeVisitor, GlobalVariableNodeVisitor $globalVariableNodeVisitor, StaticVariableNodeVisitor $staticVariableNodeVisitor, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, PrivatesAccessor $privatesAccessor, NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, ClassAnalyzer $classAnalyzer)
public function __construct(ChangedFilesDetector $changedFilesDetector, DependencyResolver $dependencyResolver, NodeScopeResolver $nodeScopeResolver, ReflectionProvider $reflectionProvider, RemoveDeepChainMethodCallNodeVisitor $removeDeepChainMethodCallNodeVisitor, AssignedToNodeVisitor $assignedToNodeVisitor, GlobalVariableNodeVisitor $globalVariableNodeVisitor, StaticVariableNodeVisitor $staticVariableNodeVisitor, ByRefVariableNodeVisitor $byRefVariableNodeVisitor, \Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory $scopeFactory, PrivatesAccessor $privatesAccessor, NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, ClassAnalyzer $classAnalyzer)
{
$this->changedFilesDetector = $changedFilesDetector;
$this->dependencyResolver = $dependencyResolver;
@ -130,6 +131,7 @@ final class PHPStanNodeScopeResolver
$this->nodeTraverser->addVisitor($assignedToNodeVisitor);
$this->nodeTraverser->addVisitor($globalVariableNodeVisitor);
$this->nodeTraverser->addVisitor($staticVariableNodeVisitor);
$this->nodeTraverser->addVisitor($byRefVariableNodeVisitor);
}
/**
* @param Stmt[] $stmts

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '623a0770e9f64738b6e88eb02691fc954b1e230b';
public const PACKAGE_VERSION = 'a74c640bc1043f19006c7403461108f61d8bf2a5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2023-05-13 18:25:30';
public const RELEASE_DATE = '2023-05-14 00:33:42';
/**
* @var int
*/

View File

@ -3,31 +3,10 @@
declare (strict_types=1);
namespace Rector\Core\NodeAnalyzer;
use PhpParser\Node;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\ClosureUse;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class VariableAnalyzer
{
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @readonly
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
*/
private $nodeComparator;
public function __construct(BetterNodeFinder $betterNodeFinder, NodeComparator $nodeComparator)
{
$this->betterNodeFinder = $betterNodeFinder;
$this->nodeComparator = $nodeComparator;
}
public function isStaticOrGlobal(Variable $variable) : bool
{
if ($variable->getAttribute(AttributeKey::IS_GLOBAL_VAR) === \true) {
@ -37,31 +16,6 @@ final class VariableAnalyzer
}
public function isUsedByReference(Variable $variable) : bool
{
return (bool) $this->betterNodeFinder->findFirstPrevious($variable, function (Node $subNode) use($variable) : bool {
if ($this->isParamReferenced($subNode, $variable)) {
return \true;
}
if (!$subNode instanceof Variable) {
return \false;
}
if (!$this->nodeComparator->areNodesEqual($subNode, $variable)) {
return \false;
}
$parentNode = $subNode->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof ClosureUse) {
return $parentNode->byRef;
}
return $parentNode instanceof AssignRef;
});
}
private function isParamReferenced(Node $node, Variable $variable) : bool
{
if (!$node instanceof Param) {
return \false;
}
if (!$this->nodeComparator->areNodesEqual($node->var, $variable)) {
return \false;
}
return $node->byRef;
return $variable->getAttribute(AttributeKey::IS_BYREF_VAR) === \true;
}
}

2
vendor/autoload.php vendored
View File

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

View File

@ -1991,6 +1991,7 @@ return array(
'Rector\\NodeTypeResolver\\PHPStan\\ObjectWithoutClassTypeWithParentTypes' => $baseDir . '/packages/NodeTypeResolver/PHPStan/ObjectWithoutClassTypeWithParentTypes.php',
'Rector\\NodeTypeResolver\\PHPStan\\ParametersAcceptorSelectorVariantsWrapper' => $baseDir . '/packages/NodeTypeResolver/PHPStan/ParametersAcceptorSelectorVariantsWrapper.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\AssignedToNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/AssignedToNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ByRefVariableNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\GlobalVariableNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\RemoveDeepChainMethodCallNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/RemoveDeepChainMethodCallNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\StaticVariableNodeVisitor' => $baseDir . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php',

View File

@ -2,7 +2,7 @@
// autoload_real.php @generated by Composer
class ComposerAutoloaderInitfeccdb98e303aa4438b1a8d89fee276d
class ComposerAutoloaderInit1a3a3ed36ea10c7719447923da5e656e
{
private static $loader;
@ -22,17 +22,17 @@ class ComposerAutoloaderInitfeccdb98e303aa4438b1a8d89fee276d
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInitfeccdb98e303aa4438b1a8d89fee276d', 'loadClassLoader'), true, true);
spl_autoload_register(array('ComposerAutoloaderInit1a3a3ed36ea10c7719447923da5e656e', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__));
spl_autoload_unregister(array('ComposerAutoloaderInitfeccdb98e303aa4438b1a8d89fee276d', 'loadClassLoader'));
spl_autoload_unregister(array('ComposerAutoloaderInit1a3a3ed36ea10c7719447923da5e656e', 'loadClassLoader'));
require __DIR__ . '/autoload_static.php';
call_user_func(\Composer\Autoload\ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d::getInitializer($loader));
call_user_func(\Composer\Autoload\ComposerStaticInit1a3a3ed36ea10c7719447923da5e656e::getInitializer($loader));
$loader->setClassMapAuthoritative(true);
$loader->register(true);
$filesToLoad = \Composer\Autoload\ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d::$files;
$filesToLoad = \Composer\Autoload\ComposerStaticInit1a3a3ed36ea10c7719447923da5e656e::$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 ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d
class ComposerStaticInit1a3a3ed36ea10c7719447923da5e656e
{
public static $files = array (
'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php',
@ -2233,6 +2233,7 @@ class ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d
'Rector\\NodeTypeResolver\\PHPStan\\ObjectWithoutClassTypeWithParentTypes' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/ObjectWithoutClassTypeWithParentTypes.php',
'Rector\\NodeTypeResolver\\PHPStan\\ParametersAcceptorSelectorVariantsWrapper' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/ParametersAcceptorSelectorVariantsWrapper.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\AssignedToNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/AssignedToNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\ByRefVariableNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/ByRefVariableNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\GlobalVariableNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/GlobalVariableNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\RemoveDeepChainMethodCallNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/RemoveDeepChainMethodCallNodeVisitor.php',
'Rector\\NodeTypeResolver\\PHPStan\\Scope\\NodeVisitor\\StaticVariableNodeVisitor' => __DIR__ . '/../..' . '/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StaticVariableNodeVisitor.php',
@ -3110,9 +3111,9 @@ class ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d
public static function getInitializer(ClassLoader $loader)
{
return \Closure::bind(function () use ($loader) {
$loader->prefixLengthsPsr4 = ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInitfeccdb98e303aa4438b1a8d89fee276d::$classMap;
$loader->prefixLengthsPsr4 = ComposerStaticInit1a3a3ed36ea10c7719447923da5e656e::$prefixLengthsPsr4;
$loader->prefixDirsPsr4 = ComposerStaticInit1a3a3ed36ea10c7719447923da5e656e::$prefixDirsPsr4;
$loader->classMap = ComposerStaticInit1a3a3ed36ea10c7719447923da5e656e::$classMap;
}, null, ClassLoader::class);
}