-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rector] Add custom Rector Rule: RemoveErrorSuppressInTryCatchStmtsRe…
…ctor rector rule
- Loading branch information
1 parent
6da0e5b
commit c6e2f49
Showing
3 changed files
with
78 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Utils\Rector; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ErrorSuppress; | ||
use PhpParser\Node\Stmt\TryCatch; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
final class RemoveErrorSuppressInTryCatchStmtsRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Remove error suppress @', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
try { | ||
@rmdir($dirname); | ||
} catch (Exception $e) {} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
rmdir($dirname); | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [ErrorSuppress::class]; | ||
} | ||
|
||
/** | ||
* @param ErrorSuppress $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
// not in try catch | ||
$tryCatch = $this->betterNodeFinder->findParentType($node, TryCatch::class); | ||
if (! $tryCatch instanceof TryCatch) | ||
{ | ||
return null; | ||
} | ||
|
||
// not in stmts, means it in catch or finally | ||
$inStmts = (bool) $this->betterNodeFinder->findFirst((array) $tryCatch->stmts, function (Node $n) use ($node) : bool { | ||
return $n === $node; | ||
}); | ||
|
||
if (! $inStmts) | ||
{ | ||
return null; | ||
} | ||
|
||
// in try { ... } stmts | ||
return $node->expr; | ||
} | ||
} |