Skip to content

Commit

Permalink
[Php56] Skip multiple catch with same variable on AddDefaultValueForU…
Browse files Browse the repository at this point in the history
…ndefinedVariableRector (#2533)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 20, 2022
1 parent 87b3dad commit 4eeadae
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Class_;
Expand Down Expand Up @@ -41,6 +42,7 @@
use Rector\Core\StaticReflection\SourceLocator\ParentAttributeSourceLocator;
use Rector\Core\StaticReflection\SourceLocator\RenamedClassesSourceLocator;
use Rector\Core\Util\StringUtils;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\RemoveDeepChainMethodCallNodeVisitor;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
Expand Down Expand Up @@ -74,6 +76,7 @@ public function __construct(
private readonly PrivatesAccessor $privatesAccessor,
private readonly RenamedClassesSourceLocator $renamedClassesSourceLocator,
private readonly ParentAttributeSourceLocator $parentAttributeSourceLocator,
private readonly NodeNameResolver $nodeNameResolver
) {
}

Expand Down Expand Up @@ -138,7 +141,11 @@ public function processNodes(

if ($node instanceof TryCatch) {
foreach ($node->catches as $catch) {
$this->processNodes($catch->stmts, $smartFileInfo, $mutatingScope);
$varName = $catch->var instanceof Variable
? $this->nodeNameResolver->getName($catch->var)
: null;
$catchMutatingScope = $mutatingScope->enterCatch($catch->types, $varName);
$this->processNodes($catch->stmts, $smartFileInfo, $catchMutatingScope);
}

if ($node->finally instanceof Finally_) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

class SkipCatchUseVariable
{
public function run()
{
try {
} catch (\Exception $e) {
var_dump($e);
}
}

public function run2()
{
try {
} catch (\Exception $e) {
$e;
}
}

public function run3()
{
try {
} catch (\Exception $e) {
echo $e::class;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

class SkipMultipleCatchWithUseSameVariable
{
public function run()
{
try {
} catch (\Exception $e) {
var_dump($e);
} catch (\Throwable $e) {
var_dump($e);
}
}

public function run2()
{
try {
} catch (\Exception $e) {
$e;
} catch (\Throwable $e) {
$e;
}
}

public function run3()
{
try {
} catch (\Exception $e) {
echo $e::class;
} catch (\Throwable $e) {
echo $e::class;
}
}
}

0 comments on commit 4eeadae

Please sign in to comment.