rector/packages/CodingStyle/src/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php

115 lines
2.6 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
namespace Rector\CodingStyle\Rector\Catch_;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Catch_;
use Rector\CodingStyle\Naming\ClassNaming;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
final class CatchExceptionNameMatchingTypeRector extends AbstractRector
{
/**
* @var ClassNaming
*/
private $classNaming;
2019-06-04 20:34:59 +00:00
public function __construct(ClassNaming $classNaming)
{
$this->classNaming = $classNaming;
}
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Type and name of catch exception should match', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
try {
// ...
} catch (SomeException $typoException) {
$typoException->getMessage();
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
try {
// ...
} catch (SomeException $someException) {
$someException->getMessage();
}
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Catch_::class];
}
/**
* @param Catch_ $node
*/
public function refactor(Node $node): ?Node
{
if (count($node->types) !== 1) {
return null;
}
$type = $node->types[0];
$typeShortName = $this->classNaming->getShortName($type);
$oldVariableName = $this->getName($node->var);
if (! $oldVariableName) {
return null;
}
$newVariableName = lcfirst($typeShortName);
if ($oldVariableName === $newVariableName) {
return null;
}
$node->var->name = $newVariableName;
$this->renameVariableInStmts($node, $oldVariableName, $newVariableName);
return $node;
}
private function renameVariableInStmts(Catch_ $catch, string $oldVariableName, string $newVariableName): void
{
2019-06-04 20:34:59 +00:00
$this->traverseNodesWithCallable($catch->stmts, function (Node $node) use (
$oldVariableName,
$newVariableName
): void {
if (! $node instanceof Variable) {
return;
}
if (! $this->isName($node, $oldVariableName)) {
return;
}
$node->name = $newVariableName;
});
}
}