Skip to content

Commit

Permalink
[Php56] Skip after exit() on AddDefaultValueForUndefinedVariableRect…
Browse files Browse the repository at this point in the history
…or (#2949)

* [Php56] Skip after exit() on AddDefaultValueForUndefinedVariableRector

* fixture
  • Loading branch information
samsonasik committed Sep 23, 2022
1 parent 3335ea4 commit 1743bb1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php56\Rector\FunctionLike\AddDefaultValueForUndefinedVariableRector\Fixture;

final class SkipAfterExit
{
public function run()
{
exit();
$test = true;

return $test;
}
}
19 changes: 15 additions & 4 deletions rules/Php56/NodeAnalyzer/UndefinedVariableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Case_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Foreach_;
Expand Down Expand Up @@ -82,10 +83,7 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
return null;
}

// defined 100 %
/** @var Scope $scope */
$scope = $node->getAttribute(AttributeKey::SCOPE);
if ($scope->hasVariableType($variableName)->yes()) {
if ($this->hasVariableTypeOrCurrentStmtUnreachable($node, $variableName)) {
return null;
}

Expand All @@ -97,6 +95,19 @@ public function resolve(ClassMethod | Function_ | Closure $node): array
return array_unique($undefinedVariables);
}

private function hasVariableTypeOrCurrentStmtUnreachable(Variable $variable, string $variableName): bool
{
// defined 100 %
/** @var Scope $scope */
$scope = $variable->getAttribute(AttributeKey::SCOPE);
if ($scope->hasVariableType($variableName)->yes()) {
return true;
}

$currentStmt = $this->betterNodeFinder->resolveCurrentStatement($variable);
return $currentStmt instanceof Stmt && $currentStmt->getAttribute(AttributeKey::IS_UNREACHABLE) === true;
}

private function issetOrUnsetOrEmptyParent(Node $parentNode): bool
{
return in_array($parentNode::class, [Unset_::class, UnsetCast::class, Isset_::class, Empty_::class], true);
Expand Down

0 comments on commit 1743bb1

Please sign in to comment.