[Php56] Skip multiple catch with same variable on AddDefaultValueForUndefinedVariableRector (#2533)

Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
Abdul Malik Ikhsan 2022-06-20 14:03:13 +07:00 committed by GitHub
parent 87b3dad341
commit 4eeadae7bf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 1 deletions

View File

@ -11,6 +11,7 @@ use PhpParser\Node\Expr\Assign;
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_;
@ -41,6 +42,7 @@ use Rector\Core\Exception\ShouldNotHappenException;
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;
@ -74,6 +76,7 @@ final class PHPStanNodeScopeResolver
private readonly PrivatesAccessor $privatesAccessor,
private readonly RenamedClassesSourceLocator $renamedClassesSourceLocator,
private readonly ParentAttributeSourceLocator $parentAttributeSourceLocator,
private readonly NodeNameResolver $nodeNameResolver
) {
}
@ -138,7 +141,11 @@ final class PHPStanNodeScopeResolver
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_) {

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}