Updated Rector to commit 2e82ab4969246050e9fe47df0cf549cc96fdbfd5

2e82ab4969 [PHPStan ^2.1.1] Handle next unreachable statement via UnreachableStatementNode->getNextUnreachableStatements() (#6642)
This commit is contained in:
Tomas Votruba 2025-01-05 17:07:39 +00:00
parent fcafd42fb3
commit 40b5c4746e
6 changed files with 10 additions and 88 deletions

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '92b4999c34e9fad614aedb1d98d05829dfbdaa2b';
public const PACKAGE_VERSION = '2e82ab4969246050e9fe47df0cf549cc96fdbfd5';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2025-01-06 00:00:40';
public const RELEASE_DATE = '2025-01-06 00:05:08';
/**
* @var int
*/

View File

@ -61,10 +61,6 @@ final class AttributeKey
* Use often in php-parser
*/
public const KIND = 'kind';
/**
* @var string
*/
public const IS_UNREACHABLE = 'isUnreachable';
/**
* @var string
*/

View File

@ -104,7 +104,6 @@ use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor;
use Rector\Util\Reflection\PrivatesAccessor;
use RectorPrefix202501\Webmozart\Assert\Assert;
/**
@ -174,8 +173,7 @@ final class PHPStanNodeScopeResolver
Assert::allIsInstanceOf($stmts, Stmt::class);
$this->nodeTraverser->traverse($stmts);
$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);
$hasUnreachableStatementNode = \false;
$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use(&$nodeCallback, $filePath, &$hasUnreachableStatementNode) : void {
$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use(&$nodeCallback, $filePath) : void {
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
if ($node instanceof Class_ || $node instanceof Interface_ || $node instanceof Enum_) {
@ -192,8 +190,7 @@ final class PHPStanNodeScopeResolver
// early check here as UnreachableStatementNode is special VirtualNode
// so node to be checked inside
if ($node instanceof UnreachableStatementNode) {
$this->processUnreachableStatementNode($node, $filePath, $mutatingScope);
$hasUnreachableStatementNode = \true;
$this->processUnreachableStatementNode($node, $mutatingScope, $nodeCallback);
return;
}
// init current Node set Attribute
@ -347,11 +344,7 @@ final class PHPStanNodeScopeResolver
// fallback to fill by found scope
\Rector\NodeTypeResolver\PHPStan\Scope\RectorNodeScopeResolver::processNodes($stmts, $scope);
}
if (!$hasUnreachableStatementNode) {
return $stmts;
}
$nodeTraverser = new NodeTraverser(new UnreachableStatementNodeVisitor($this, $filePath, $scope));
return $nodeTraverser->traverse($stmts);
return $stmts;
}
private function processYield(Yield_ $yield, MutatingScope $mutatingScope) : void
{
@ -485,12 +478,13 @@ final class PHPStanNodeScopeResolver
$tryCatch->finally->setAttribute(AttributeKey::SCOPE, $mutatingScope);
}
}
private function processUnreachableStatementNode(UnreachableStatementNode $unreachableStatementNode, string $filePath, MutatingScope $mutatingScope) : void
/**
* @param callable(Node $node, MutatingScope $scope): void $nodeCallback
*/
private function processUnreachableStatementNode(UnreachableStatementNode $unreachableStatementNode, MutatingScope $mutatingScope, callable $nodeCallback) : void
{
$originalStmt = $unreachableStatementNode->getOriginalStatement();
$originalStmt->setAttribute(AttributeKey::IS_UNREACHABLE, \true);
$originalStmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->processNodes([$originalStmt], $filePath, $mutatingScope);
$this->nodeScopeResolverProcessNodes(\array_merge([$originalStmt], $unreachableStatementNode->getNextStatements()), $mutatingScope, $nodeCallback);
}
/**
* @param callable(Node $node, MutatingScope $scope): void $nodeCallback

View File

@ -1,66 +0,0 @@
<?php
declare (strict_types=1);
namespace Rector\PHPStan\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Stmt\Block;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\Scope;
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
final class UnreachableStatementNodeVisitor extends NodeVisitorAbstract
{
/**
* @readonly
*/
private PHPStanNodeScopeResolver $phpStanNodeScopeResolver;
/**
* @readonly
*/
private string $filePath;
/**
* @readonly
*/
private MutatingScope $mutatingScope;
public function __construct(PHPStanNodeScopeResolver $phpStanNodeScopeResolver, string $filePath, MutatingScope $mutatingScope)
{
$this->phpStanNodeScopeResolver = $phpStanNodeScopeResolver;
$this->filePath = $filePath;
$this->mutatingScope = $mutatingScope;
}
public function enterNode(Node $node) : ?Node
{
if (!$node instanceof StmtsAwareInterface && !$node instanceof ClassLike && !$node instanceof Declare_ && !$node instanceof Block) {
return null;
}
if ($node->stmts === null) {
return null;
}
$isPassedUnreachableStmt = \false;
$mutatingScope = $this->resolveScope($node->getAttribute(AttributeKey::SCOPE));
foreach ($node->stmts as $stmt) {
$hasMutatingScope = $stmt->getAttribute(AttributeKey::SCOPE) instanceof MutatingScope;
if (!$hasMutatingScope) {
$stmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->phpStanNodeScopeResolver->processNodes([$stmt], $this->filePath, $mutatingScope);
}
if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === \true) {
$isPassedUnreachableStmt = \true;
continue;
}
if ($isPassedUnreachableStmt) {
$stmt->setAttribute(AttributeKey::IS_UNREACHABLE, \true);
}
}
return null;
}
private function resolveScope(?Scope $mutatingScope) : MutatingScope
{
return $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->mutatingScope;
}
}

View File

@ -1757,7 +1757,6 @@ return array(
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\UnionTypeMapper' => $baseDir . '/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\VoidTypeMapper' => $baseDir . '/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\Utils\\TypeUnwrapper' => $baseDir . '/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php',
'Rector\\PHPStan\\NodeVisitor\\UnreachableStatementNodeVisitor' => $baseDir . '/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php',
'Rector\\PHPStan\\ScopeFetcher' => $baseDir . '/src/PHPStan/ScopeFetcher.php',
'Rector\\PHPUnit\\AnnotationsToAttributes\\Rector\\ClassMethod\\DataProviderAnnotationToAttributeRector' => $vendorDir . '/rector/rector-phpunit/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php',
'Rector\\PHPUnit\\AnnotationsToAttributes\\Rector\\ClassMethod\\DependsAnnotationWithValueToAttributeRector' => $vendorDir . '/rector/rector-phpunit/rules/AnnotationsToAttributes/Rector/ClassMethod/DependsAnnotationWithValueToAttributeRector.php',

View File

@ -1976,7 +1976,6 @@ class ComposerStaticInit4349f48e2d711e343a1bf0101d0ae855
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\UnionTypeMapper' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/TypeMapper/UnionTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\TypeMapper\\VoidTypeMapper' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/TypeMapper/VoidTypeMapper.php',
'Rector\\PHPStanStaticTypeMapper\\Utils\\TypeUnwrapper' => __DIR__ . '/../..' . '/src/PHPStanStaticTypeMapper/Utils/TypeUnwrapper.php',
'Rector\\PHPStan\\NodeVisitor\\UnreachableStatementNodeVisitor' => __DIR__ . '/../..' . '/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php',
'Rector\\PHPStan\\ScopeFetcher' => __DIR__ . '/../..' . '/src/PHPStan/ScopeFetcher.php',
'Rector\\PHPUnit\\AnnotationsToAttributes\\Rector\\ClassMethod\\DataProviderAnnotationToAttributeRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/AnnotationsToAttributes/Rector/ClassMethod/DataProviderAnnotationToAttributeRector.php',
'Rector\\PHPUnit\\AnnotationsToAttributes\\Rector\\ClassMethod\\DependsAnnotationWithValueToAttributeRector' => __DIR__ . '/..' . '/rector/rector-phpunit/rules/AnnotationsToAttributes/Rector/ClassMethod/DependsAnnotationWithValueToAttributeRector.php',