Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ parameters:
paths:
- src/CustomRules/SimpleNodeDumper.php
- src/PhpDocParser/PhpDocParser/PhpDocNodeTraverser.php
- src/PhpParser/Printer/BetterStandardPrinter.php

# known node variables
-
Expand Down
33 changes: 6 additions & 27 deletions src/NodeTypeResolver/PHPStan/Scope/PHPStanNodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
use PHPStan\Analyser\MutatingScope;
use PHPStan\Analyser\NodeScopeResolver;
use PHPStan\Analyser\ScopeContext;
use PHPStan\Node\Expr\AlwaysRememberedExpr;
use PHPStan\Node\FunctionCallableNode;
use PHPStan\Node\InstantiationCallableNode;
use PHPStan\Node\MethodCallableNode;
Expand All @@ -107,7 +106,6 @@
use Rector\NodeTypeResolver\PHPStan\Scope\Contract\NodeVisitor\ScopeResolverNodeVisitorInterface;
use Rector\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\PHPStan\NodeVisitor\UnreachableStatementNodeVisitor;
use Rector\PHPStan\NodeVisitor\WrappedNodeRestoringNodeVisitor;
use Rector\Util\Reflection\PrivatesAccessor;
use Webmozart\Assert\Assert;

Expand Down Expand Up @@ -164,13 +162,10 @@ public function processNodes(
$scope = $formerMutatingScope ?? $this->scopeFactory->createFromFile($filePath);

$hasUnreachableStatementNode = false;
$hasAlwaysRememberedExpr = false;

$nodeCallback = function (Node $node, MutatingScope $mutatingScope) use (
&$nodeCallback,
$filePath,
&$hasUnreachableStatementNode,
&$hasAlwaysRememberedExpr
&$hasUnreachableStatementNode
): void {
// the class reflection is resolved AFTER entering to class node
// so we need to get it from the first after this one
Expand Down Expand Up @@ -368,13 +363,8 @@ public function processNodes(
return;
}

if ($node instanceof AlwaysRememberedExpr || $node instanceof Match_) {
$hasAlwaysRememberedExpr = true;

if ($node instanceof Match_) {
$this->processMatch($node, $mutatingScope);
}

if ($node instanceof Match_) {
$this->processMatch($node, $mutatingScope);
return;
}

Expand Down Expand Up @@ -420,23 +410,12 @@ public function processNodes(
RectorNodeScopeResolver::processNodes($stmts, $scope);
}

if (! $hasAlwaysRememberedExpr && ! $hasUnreachableStatementNode) {
if (! $hasUnreachableStatementNode) {
return $stmts;
}

$nodeTraverser = new NodeTraverser();

if ($hasAlwaysRememberedExpr) {
$nodeTraverser->addVisitor(new WrappedNodeRestoringNodeVisitor());
}

if ($hasUnreachableStatementNode) {
$nodeTraverser->addVisitor(new UnreachableStatementNodeVisitor($this, $filePath, $scope));
}

$nodeTraverser->traverse($stmts);

return $stmts;
$nodeTraverser = new NodeTraverser(new UnreachableStatementNodeVisitor($this, $filePath, $scope));
return $nodeTraverser->traverse($stmts);
}

private function processYield(Yield_ $yield, MutatingScope $mutatingScope): void
Expand Down
30 changes: 0 additions & 30 deletions src/PHPStan/NodeVisitor/WrappedNodeRestoringNodeVisitor.php

This file was deleted.

16 changes: 16 additions & 0 deletions src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Yield_;
Expand Down Expand Up @@ -115,10 +116,25 @@ protected function p(
int $lhsPrecedence = self::MAX_PRECEDENCE,
bool $parentFormatPreserved = false
): string {
// handle already AlwaysRememberedExpr
// @see https://github.com/rectorphp/rector/issues/8815#issuecomment-2503453191
while ($node instanceof AlwaysRememberedExpr) {
$node = $node->getExpr();
}

// handle overlapped origNode is Match_
// and its subnodes still have AlwaysRememberedExpr
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE);

if ($originalNode instanceof Match_) {
$subNodeNames = $node->getSubNodeNames();
foreach ($subNodeNames as $subNodeName) {
while ($originalNode->$subNodeName instanceof AlwaysRememberedExpr) {
$originalNode->$subNodeName = $originalNode->$subNodeName->getExpr();
}
}
}

$content = parent::p($node, $precedence, $lhsPrecedence, $parentFormatPreserved);

return $node->getAttribute(AttributeKey::WRAPPED_IN_PARENTHESES) === true
Expand Down
Loading