Skip to content

Commit

Permalink
[Renaming] Handle crash on RenameFunctionRector with die() and $_SESS…
Browse files Browse the repository at this point in the history
…ION usage (#5646)

* [Renaming] Handle crash on RenameFunctionRector with die() and $_SESSION usage

* Fix

* fix
  • Loading branch information
samsonasik committed Feb 20, 2024
1 parent 29562ce commit 00876da
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 5 deletions.
@@ -0,0 +1,65 @@
<?php

if ($_SESSION['order_in_progress'] != 1) {
$isNewUIEnabled = true;

if ($isNewUIEnabled) {
if (
isset($deliveryRequestResponse) &&
!preg_match('/^Error/m', $deliveryRequestResponse) &&
!empty($deliveryRequestResponse->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']);

?>
-----
<?php

if ($_SESSION['order_in_progress'] != 1) {
$isNewUIEnabled = true;

if ($isNewUIEnabled) {
if (
isset($deliveryRequestResponse) &&
!preg_match('/^Error/m', $deliveryRequestResponse) &&
!empty($deliveryRequestResponse->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']);

?>
Expand Up @@ -10,5 +10,6 @@
->ruleWithConfiguration(RenameFunctionRector::class, [
'view' => 'Laravel\Templating\render',
'sprintf' => 'Safe\sprintf',
'abc2' => 'abc',
]);
};
25 changes: 20 additions & 5 deletions src/PHPStan/NodeVisitor/UnreachableStatementNodeVisitor.php
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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
);
}
}

0 comments on commit 00876da

Please sign in to comment.