2019-05-25 23:15:21 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodingStyle\Rector\Catch_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2019-05-26 11:47:23 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2019-05-25 23:15:21 +00:00
|
|
|
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)
|
2019-05-25 23:15:21 +00:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2019-05-26 11:47:23 +00:00
|
|
|
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 (
|
2019-05-25 23:15:21 +00:00
|
|
|
$oldVariableName,
|
|
|
|
$newVariableName
|
|
|
|
): void {
|
2019-05-26 11:47:23 +00:00
|
|
|
if (! $node instanceof Variable) {
|
2019-05-25 23:15:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->isName($node, $oldVariableName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->name = $newVariableName;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|