Skip to content

Commit

Permalink
[CodingStyle] Handle Interface suffix on CatchExceptionNameMatchingTy…
Browse files Browse the repository at this point in the history
…peRector (#1918)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Mar 10, 2022
1 parent 888f483 commit 8b4c2d2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

class WithInterfaceSuffix
{
public function run()
{
try {
$a = 1;
} catch (ExceptionInterface $test) {
}
}
}

?>
-----
<?php

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

class WithInterfaceSuffix
{
public function run()
{
try {
$a = 1;
} catch (ExceptionInterface $exception) {
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\TryCatch;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Naming\PropertyNaming;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -27,6 +29,11 @@ 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
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -107,6 +114,8 @@ function (array $matches): string {
return $output;
}
);
$objectType = new ObjectType($newVariableName);
$newVariableName = $this->propertyNaming->fqnToVariableName($objectType);

if ($oldVariableName === $newVariableName) {
return null;
Expand Down

0 comments on commit 8b4c2d2

Please sign in to comment.