From 00876daa5dbda14d26b36b8c4365362b7e1cb7ac Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Wed, 21 Feb 2024 03:24:01 +0700 Subject: [PATCH] [Renaming] Handle crash on RenameFunctionRector with die() and $_SESSION usage (#5646) * [Renaming] Handle crash on RenameFunctionRector with die() and $_SESSION usage * Fix * fix --- .../Fixture/with_die_and_session.php.inc | 65 +++++++++++++++++++ .../config/configured_rule.php | 1 + .../UnreachableStatementNodeVisitor.php | 25 +++++-- 3 files changed, 86 insertions(+), 5 deletions(-) create mode 100644 rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/Fixture/with_die_and_session.php.inc diff --git a/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/Fixture/with_die_and_session.php.inc b/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/Fixture/with_die_and_session.php.inc new file mode 100644 index 00000000000..8313f4756b0 --- /dev/null +++ b/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/Fixture/with_die_and_session.php.inc @@ -0,0 +1,65 @@ +delivery_tracking_url) + ) { + $trackingUrl = $deliveryRequestResponse->delivery_tracking_url; + $_SESSION['checkout_success_tracking_url'] = $trackingUrl; + } + return; + } +} else { + die('Sorry, this order may already have been placed. If you did not receive a confirmation email, please give us a call to confirm your order.'); +} + +function insertorder() +{ + + $result = abc(''); + if (!$result) { + die(abc2()); + } +} + +unset($_SESSION['cart']); + +?> +----- +delivery_tracking_url) + ) { + $trackingUrl = $deliveryRequestResponse->delivery_tracking_url; + $_SESSION['checkout_success_tracking_url'] = $trackingUrl; + } + return; + } +} else { + die('Sorry, this order may already have been placed. If you did not receive a confirmation email, please give us a call to confirm your order.'); +} + +function insertorder() +{ + + $result = abc(''); + if (!$result) { + die(abc()); + } +} + +unset($_SESSION['cart']); + +?> \ No newline at end of file diff --git a/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/config/configured_rule.php b/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/config/configured_rule.php index 0726be7eb34..5485f1b7e2f 100644 --- a/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/config/configured_rule.php +++ b/rules-tests/Renaming/Rector/FuncCall/RenameFunctionRector/config/configured_rule.php @@ -10,5 +10,6 @@ ->ruleWithConfiguration(RenameFunctionRector::class, [ 'view' => 'Laravel\Templating\render', 'sprintf' => 'Safe\sprintf', + 'abc2' => 'abc', ]); }; diff --git a/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php b/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php index 36ea18a0d87..8fff3eb8469 100644 --- a/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php +++ b/src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php @@ -5,12 +5,14 @@ namespace Rector\PHPStan\NodeVisitor; use PhpParser\Node; +use PhpParser\Node\Expr; use PhpParser\Node\Expr\Exit_; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\Declare_; use PhpParser\Node\Stmt\Expression; 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; @@ -36,15 +38,13 @@ public function enterNode(Node $node): ?Node } $isPassedUnreachableStmt = false; - - $mutatingScope = $node->getAttribute(AttributeKey::SCOPE); - $mutatingScope = $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeFactory->createFromFile( - $this->filePath - ); + $mutatingScope = $this->resolveScope($node->getAttribute(AttributeKey::SCOPE)); foreach ($node->stmts as $stmt) { if ($stmt instanceof Expression && $stmt->expr instanceof Exit_) { $isPassedUnreachableStmt = true; + $this->processExitScope($stmt->expr, $stmt, $mutatingScope); + continue; } @@ -62,4 +62,19 @@ public function enterNode(Node $node): ?Node return null; } + + private function processExitScope(Exit_ $exit, Expression $expression, MutatingScope $mutatingScope): void + { + if ($exit->expr instanceof Expr && ! $exit->expr->getAttribute(AttributeKey::SCOPE) instanceof MutatingScope) { + $expression->setAttribute(AttributeKey::SCOPE, $mutatingScope); + $this->phpStanNodeScopeResolver->processNodes([$expression], $this->filePath, $mutatingScope); + } + } + + private function resolveScope(?Scope $mutatingScope): MutatingScope + { + return $mutatingScope instanceof MutatingScope ? $mutatingScope : $this->scopeFactory->createFromFile( + $this->filePath + ); + } }