From 51a6ca52201c3e26064a2d989ee831637d4aa744 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 19 May 2023 11:20:36 +0000 Subject: [PATCH] Updated Rector to commit aef093ee8a5a19bb1351345770ac761ae9e0f655 https://github.com/rectorphp/rector-src/commit/aef093ee8a5a19bb1351345770ac761ae9e0f655 [Traverser] Remove next attribute in BetterNodeFinder (#3887) --- .../Scope/NodeVisitor/StmtKeyNodeVisitor.php | 33 +++++++++++++++++-- src/Application/VersionResolver.php | 4 +-- src/PhpParser/Node/BetterNodeFinder.php | 20 ++++++++--- vendor/autoload.php | 2 +- vendor/composer/autoload_real.php | 10 +++--- vendor/composer/autoload_static.php | 8 ++--- 6 files changed, 58 insertions(+), 19 deletions(-) diff --git a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php index 9330c710696..bb9059ab6d3 100644 --- a/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php +++ b/packages/NodeTypeResolver/PHPStan/Scope/NodeVisitor/StmtKeyNodeVisitor.php @@ -4,13 +4,35 @@ declare (strict_types=1); namespace Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor; use PhpParser\Node; -use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\Namespace_; use PhpParser\NodeVisitorAbstract; use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface; +use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface; final class StmtKeyNodeVisitor extends NodeVisitorAbstract implements ScopeResolverNodeVisitorInterface { + /** + * @param Node[] $nodes + * @return Node[] + */ + public function beforeTraverse(array $nodes) : array + { + // count = 1 is essential here as FileWithoutNamespace can merge with other Stmt + if (\count($nodes) === 1) { + $currentNode = \current($nodes); + if ($currentNode instanceof FileWithoutNamespace) { + foreach ($currentNode->stmts as $key => $stmt) { + $stmt->setAttribute(AttributeKey::STMT_KEY, $key); + } + } + return $nodes; + } + foreach ($nodes as $key => $node) { + $node->setAttribute(AttributeKey::STMT_KEY, $key); + } + return $nodes; + } /** * @param Node[] $nodes * @return Node[] @@ -18,9 +40,10 @@ final class StmtKeyNodeVisitor extends NodeVisitorAbstract implements ScopeResol public function afterTraverse(array $nodes) : array { foreach ($nodes as $key => $node) { - if ($node instanceof Stmt) { - $node->setAttribute(AttributeKey::STMT_KEY, $key); + if (!$node instanceof Namespace_) { + return $nodes; } + $node->setAttribute(AttributeKey::STMT_KEY, $key); } return $nodes; } @@ -29,6 +52,10 @@ final class StmtKeyNodeVisitor extends NodeVisitorAbstract implements ScopeResol if (!$node instanceof StmtsAwareInterface) { return null; } + // covered on beforeTraverse() as top level node handling + if ($node instanceof FileWithoutNamespace) { + return null; + } if ($node->stmts === null) { return null; } diff --git a/src/Application/VersionResolver.php b/src/Application/VersionResolver.php index 7573c48cb5a..53beabaa476 100644 --- a/src/Application/VersionResolver.php +++ b/src/Application/VersionResolver.php @@ -19,12 +19,12 @@ final class VersionResolver * @api * @var string */ - public const PACKAGE_VERSION = '9fff8d32f3c52769e97ba3e358e8c43f143f0f92'; + public const PACKAGE_VERSION = 'aef093ee8a5a19bb1351345770ac761ae9e0f655'; /** * @api * @var string */ - public const RELEASE_DATE = '2023-05-19 08:43:50'; + public const RELEASE_DATE = '2023-05-19 18:15:35'; /** * @var int */ diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 37feb0aaeb0..6c7e3a176a5 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -478,6 +478,19 @@ final class BetterNodeFinder } return null; } + private function resolveNeighborNextStmt(StmtsAwareInterface $stmtsAware, Stmt $stmt, ?int $key) : ?Node + { + if ($key === null) { + $key = 0; + } + if (!isset($stmtsAware->stmts[$key - 1])) { + return $stmtsAware->stmts[$key + 1] ?? null; + } + if ($stmtsAware->stmts[$key - 1]->getStartTokenPos() !== $stmt->getStartTokenPos()) { + return $stmtsAware->stmts[$key + 1] ?? null; + } + return $stmt; + } /** * Only search in next Node/Stmt * @@ -492,9 +505,8 @@ final class BetterNodeFinder if (!$parentNode instanceof StmtsAwareInterface) { return null; } - // todo: use +1 key once all next node attribute reference and NodeConnectingVisitor removed - // left with add SlimNodeConnectingVisitor for only lookup parent - $nextNode = $node->getAttribute(AttributeKey::NEXT_NODE); + $currentStmtKey = $node->getAttribute(AttributeKey::STMT_KEY); + $nextNode = $this->resolveNeighborNextStmt($parentNode, $node, $currentStmtKey); } else { $nextNode = $this->resolveNextNodeFromOtherNode($node); } @@ -635,7 +647,7 @@ final class BetterNodeFinder if (!isset($parentNode->stmts[$currentStmtKey - 1])) { return $this->findFirstInTopLevelStmtsAware($parentNode, $filter); } - $previousNode = $parentNode->stmts[$currentStmtKey - 1]; + $previousNode = $parentNode->stmts[$currentStmtKey - 1] ?? null; } else { $previousNode = $this->resolvePreviousNodeFromOtherNode($node); } diff --git a/vendor/autoload.php b/vendor/autoload.php index dca28415b58..e4ae4386ffb 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -22,4 +22,4 @@ if (PHP_VERSION_ID < 50600) { require_once __DIR__ . '/composer/autoload_real.php'; -return ComposerAutoloaderInit3f2d70c7039a59850ffa4eb4f5609c48::getLoader(); +return ComposerAutoloaderInitbcc7e257db5496ceaf0dccb1d9cb5ac4::getLoader(); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 43cb49cd9e3..2a6fd1f54a1 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -2,7 +2,7 @@ // autoload_real.php @generated by Composer -class ComposerAutoloaderInit3f2d70c7039a59850ffa4eb4f5609c48 +class ComposerAutoloaderInitbcc7e257db5496ceaf0dccb1d9cb5ac4 { private static $loader; @@ -22,17 +22,17 @@ class ComposerAutoloaderInit3f2d70c7039a59850ffa4eb4f5609c48 return self::$loader; } - spl_autoload_register(array('ComposerAutoloaderInit3f2d70c7039a59850ffa4eb4f5609c48', 'loadClassLoader'), true, true); + spl_autoload_register(array('ComposerAutoloaderInitbcc7e257db5496ceaf0dccb1d9cb5ac4', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(\dirname(__DIR__)); - spl_autoload_unregister(array('ComposerAutoloaderInit3f2d70c7039a59850ffa4eb4f5609c48', 'loadClassLoader')); + spl_autoload_unregister(array('ComposerAutoloaderInitbcc7e257db5496ceaf0dccb1d9cb5ac4', 'loadClassLoader')); require __DIR__ . '/autoload_static.php'; - call_user_func(\Composer\Autoload\ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48::getInitializer($loader)); + call_user_func(\Composer\Autoload\ComposerStaticInitbcc7e257db5496ceaf0dccb1d9cb5ac4::getInitializer($loader)); $loader->setClassMapAuthoritative(true); $loader->register(true); - $filesToLoad = \Composer\Autoload\ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48::$files; + $filesToLoad = \Composer\Autoload\ComposerStaticInitbcc7e257db5496ceaf0dccb1d9cb5ac4::$files; $requireFile = \Closure::bind(static function ($fileIdentifier, $file) { if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; diff --git a/vendor/composer/autoload_static.php b/vendor/composer/autoload_static.php index d4b43ccde4b..57cbb7a0a8a 100644 --- a/vendor/composer/autoload_static.php +++ b/vendor/composer/autoload_static.php @@ -4,7 +4,7 @@ namespace Composer\Autoload; -class ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48 +class ComposerStaticInitbcc7e257db5496ceaf0dccb1d9cb5ac4 { public static $files = array ( 'ad155f8f1cf0d418fe49e248db8c661b' => __DIR__ . '/..' . '/react/promise/src/functions_include.php', @@ -3114,9 +3114,9 @@ class ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48 public static function getInitializer(ClassLoader $loader) { return \Closure::bind(function () use ($loader) { - $loader->prefixLengthsPsr4 = ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48::$prefixLengthsPsr4; - $loader->prefixDirsPsr4 = ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48::$prefixDirsPsr4; - $loader->classMap = ComposerStaticInit3f2d70c7039a59850ffa4eb4f5609c48::$classMap; + $loader->prefixLengthsPsr4 = ComposerStaticInitbcc7e257db5496ceaf0dccb1d9cb5ac4::$prefixLengthsPsr4; + $loader->prefixDirsPsr4 = ComposerStaticInitbcc7e257db5496ceaf0dccb1d9cb5ac4::$prefixDirsPsr4; + $loader->classMap = ComposerStaticInitbcc7e257db5496ceaf0dccb1d9cb5ac4::$classMap; }, null, ClassLoader::class); }