Skip to content

Commit

Permalink
Revert [NodeTypeResolver] Remove parent attribute usage on PHPStanNod…
Browse files Browse the repository at this point in the history
…eScopeResolver for next UnreachableStatementNode detection (#3970) (#3971)

* Revert [NodeTypeResolver] Remove parent attribute usage on PHPStanNodeScopeResolver for next UnreachableStatementNode detection (#3970)

This reverts commit dde5211.

* add ClassLike and Declare_ check

* set scope
  • Loading branch information
samsonasik committed May 26, 2023
1 parent dde5211 commit 6c78d67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ final class PHPStanNodeScopeResolver

private readonly NodeTraverser $nodeTraverser;

private bool $hasUnreachableStmt = false;

/**
* @param ScopeResolverNodeVisitorInterface[] $nodeVisitors
*/
Expand Down Expand Up @@ -220,22 +218,6 @@ public function processNodes(
$mutatingScope = $this->resolveClassOrInterfaceScope($node, $mutatingScope, $isScopeRefreshing);
}

if ($this->hasUnreachableStmt && ($node instanceof StmtsAwareInterface || $node instanceof ClassLike || $node instanceof Declare_) && $node->stmts !== null) {
$isUnreachable = false;
foreach ($node->stmts as $stmt) {
if ($stmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true) {
// already processed from UnreachableStatementNode virtual node
// use $isUnreachable flag to true to make next stmt as unreachable statement
$isUnreachable = true;
continue;
}

if ($isUnreachable) {
$this->processUnreachableStatementNode($stmt, $filePath, $mutatingScope);
}
}
}

if ($node instanceof Stmt) {
$this->setChildOfUnreachableStatementNodeAttribute($node, $mutatingScope);
}
Expand Down Expand Up @@ -329,22 +311,32 @@ private function processTryCatch(TryCatch $tryCatch, string $filePath, MutatingS
}

private function processUnreachableStatementNode(
UnreachableStatementNode|Stmt $unreachableStatementNode,
UnreachableStatementNode $unreachableStatementNode,
string $filePath,
MutatingScope $mutatingScope
): void {
$originalStmt = $unreachableStatementNode instanceof UnreachableStatementNode
? $unreachableStatementNode->getOriginalStatement()
: $unreachableStatementNode;

$originalStmt = $unreachableStatementNode->getOriginalStatement();
$originalStmt->setAttribute(AttributeKey::IS_UNREACHABLE, true);
$originalStmt->setAttribute(AttributeKey::SCOPE, $mutatingScope);

$this->processNodes([$originalStmt], $filePath, $mutatingScope);

$this->setChildOfUnreachableStatementNodeAttribute($originalStmt, $mutatingScope);
$parentNode = $unreachableStatementNode->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof StmtsAwareInterface && ! $parentNode instanceof ClassLike && ! $parentNode instanceof Declare_) {
return;
}

$stmtKey = $unreachableStatementNode->getAttribute(AttributeKey::STMT_KEY);
$totalKeys = $parentNode->stmts === null ? 0 : count($parentNode->stmts);

for ($key = $stmtKey + 1; $key < $totalKeys; ++$key) {
if (! isset($parentNode->stmts[$key])) {
continue;
}

$this->hasUnreachableStmt = true;
$parentNode->stmts[$key]->setAttribute(AttributeKey::IS_UNREACHABLE, true);
$this->processNodes([$parentNode->stmts[$key]], $filePath, $mutatingScope);
}
}

private function processProperty(Property $property, MutatingScope $mutatingScope): void
Expand Down Expand Up @@ -391,12 +383,6 @@ private function processNodesWithDependentFiles(
callable $nodeCallback
): array {
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);

if ($this->hasUnreachableStmt) {
$this->nodeScopeResolver->processNodes($stmts, $mutatingScope, $nodeCallback);
$this->hasUnreachableStmt = false;
}

$this->resolveAndSaveDependentFiles($stmts, $mutatingScope, $filePath);

return $stmts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ final class SkipAfterInfiniteLoop

if ($b = $yes) {
$a[] = 'test';

if ($x = 'true') {
$yyy = 'test';
}
}

return $a;
Expand Down

0 comments on commit 6c78d67

Please sign in to comment.