2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2019-05-25 23:15:21 +00:00
|
|
|
|
|
|
|
namespace Rector\CodingStyle\Rector\Catch_;
|
|
|
|
|
2020-07-21 17:04:31 +00:00
|
|
|
use Nette\Utils\Strings;
|
2019-05-25 23:15:21 +00:00
|
|
|
use PhpParser\Node;
|
2020-12-21 02:12:42 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2019-05-25 23:15:21 +00:00
|
|
|
use PhpParser\Node\Stmt\Catch_;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-05-25 23:15:21 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\CodingStyle\Tests\Rector\Catch_\CatchExceptionNameMatchingTypeRector\CatchExceptionNameMatchingTypeRectorTest
|
|
|
|
*/
|
2019-05-25 23:15:21 +00:00
|
|
|
final class CatchExceptionNameMatchingTypeRector extends AbstractRector
|
|
|
|
{
|
2020-07-21 17:04:31 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
2020-10-29 18:04:33 +00:00
|
|
|
* @see https://regex101.com/r/xmfMAX/1
|
2020-07-21 17:04:31 +00:00
|
|
|
*/
|
|
|
|
private const STARTS_WITH_ABBREVIATION_REGEX = '#^([A-Za-z]+?)([A-Z]{1}[a-z]{1})([A-Za-z]*)#';
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2019-05-25 23:15:21 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Type and name of catch exception should match',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-05-25 23:15:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// ...
|
|
|
|
} catch (SomeException $typoException) {
|
|
|
|
$typoException->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
2019-05-25 23:15:21 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
// ...
|
|
|
|
} catch (SomeException $someException) {
|
|
|
|
$someException->getMessage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2019-05-25 23:15:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Catch_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Catch_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-12-25 00:22:45 +00:00
|
|
|
if (count($node->types) !== 1) {
|
2019-05-25 23:15:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-06-03 09:39:18 +00:00
|
|
|
if ($node->var === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-05-25 23:15:21 +00:00
|
|
|
$oldVariableName = $this->getName($node->var);
|
|
|
|
if (! $oldVariableName) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-07-21 17:04:31 +00:00
|
|
|
$type = $node->types[0];
|
|
|
|
$typeShortName = $this->getShortName($type);
|
|
|
|
|
|
|
|
$newVariableName = Strings::replace(
|
|
|
|
lcfirst($typeShortName),
|
|
|
|
self::STARTS_WITH_ABBREVIATION_REGEX,
|
|
|
|
function (array $matches): string {
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
$output .= isset($matches[1]) ? strtolower($matches[1]) : '';
|
|
|
|
$output .= $matches[2] ?? '';
|
|
|
|
$output .= $matches[3] ?? '';
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-05-25 23:15:21 +00:00
|
|
|
if ($oldVariableName === $newVariableName) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-23 14:33:14 +00:00
|
|
|
$newVariable = new Variable($newVariableName);
|
|
|
|
$isFoundInPrevious = (bool) $this->betterNodeFinder->findFirstPrevious($node, function (Node $n) use (
|
|
|
|
$newVariable
|
|
|
|
): bool {
|
|
|
|
return $this->areNodesEqual($n, $newVariable);
|
|
|
|
});
|
|
|
|
|
|
|
|
if ($isFoundInPrevious) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-05-25 23:15:21 +00:00
|
|
|
|
2020-12-23 14:33:14 +00:00
|
|
|
$node->var->name = $newVariableName;
|
2019-05-25 23:15:21 +00:00
|
|
|
$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 {
|
2020-12-21 02:12:42 +00:00
|
|
|
if (! $node instanceof Variable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-29 01:35:25 +00:00
|
|
|
if (! $this->isVariableName($node, $oldVariableName)) {
|
2019-05-25 23:15:21 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$node->name = $newVariableName;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|