Skip to content

Commit

Permalink
[CodingStyle] Use alias name when exists on CatchExceptionNameMatchin…
Browse files Browse the repository at this point in the history
…gTypeRector (#1920)

* [CodingStyle] Use alias name when exists on CatchExceptionNameMatchingTypeRector

* Fixed 🎉

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Mar 11, 2022
1 parent 8d1ea53 commit 07df2bc
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector\Fixture;

use Exception as SomeException;

class UseAlias
{
public function run()
{
try {
// ...
} catch (SomeException $typoException) {
$typoException->getMessage();
}
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector\Fixture;

use Exception as SomeException;

class UseAlias
{
public function run()
{
try {
// ...
} catch (SomeException $someException) {
$someException->getMessage();
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use PhpParser\Node\Stmt\TryCatch;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\AliasNameResolver;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -30,7 +31,8 @@ final class CatchExceptionNameMatchingTypeRector extends AbstractRector
private const STARTS_WITH_ABBREVIATION_REGEX = '#^([A-Za-z]+?)([A-Z]{1}[a-z]{1})([A-Za-z]*)#';

public function __construct(
private readonly PropertyNaming $propertyNaming
private readonly PropertyNaming $propertyNaming,
private readonly AliasNameResolver $aliasNameResolver
) {
}

Expand Down Expand Up @@ -101,6 +103,11 @@ public function refactor(Node $node): ?Node
$type = $node->types[0];
$typeShortName = $this->nodeNameResolver->getShortName($type);

$aliasName = $this->aliasNameResolver->resolveByName($type);
if (is_string($aliasName)) {
$typeShortName = $aliasName;
}

$newVariableName = Strings::replace(
lcfirst($typeShortName),
self::STARTS_WITH_ABBREVIATION_REGEX,
Expand Down
50 changes: 50 additions & 0 deletions rules/Naming/Naming/AliasNameResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Rector\Naming\Naming;

use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Use_;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class AliasNameResolver
{
public function __construct(
private readonly NodeNameResolver $nodeNameResolver
) {
}

public function resolveByName(Name $name): ?string
{
/** @var Use_[] $useNodes */
$useNodes = $name->getAttribute(AttributeKey::USE_NODES);
$nameString = $name->toString();

foreach ($useNodes as $useNode) {
$useUses = $useNode->uses;
if (count($useUses) > 1) {
continue;
}

if (! isset($useUses[0])) {
continue;
}

$useUse = $useUses[0];
if (! $useUse->alias instanceof Identifier) {
continue;
}

if (! $this->nodeNameResolver->isName($useUse->name, $nameString)) {
continue;
}

return (string) $useUse->getAlias();
}

return null;
}
}

0 comments on commit 07df2bc

Please sign in to comment.