From 8b4c2d2539c19ebcde14d3bbdaaf34e9d9485791 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 10 Mar 2022 21:38:24 +0700 Subject: [PATCH] [CodingStyle] Handle Interface suffix on CatchExceptionNameMatchingTypeRector (#1918) Co-authored-by: GitHub Action --- .../Fixture/with_interface_suffix.php.inc | 33 +++++++++++++++++++ .../CatchExceptionNameMatchingTypeRector.php | 9 +++++ 2 files changed, 42 insertions(+) create mode 100644 rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/with_interface_suffix.php.inc diff --git a/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/with_interface_suffix.php.inc b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/with_interface_suffix.php.inc new file mode 100644 index 00000000000..f4973c198de --- /dev/null +++ b/rules-tests/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector/Fixture/with_interface_suffix.php.inc @@ -0,0 +1,33 @@ + +----- + diff --git a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php index 31c869915fe..7e2d1c5b46b 100644 --- a/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php +++ b/rules/CodingStyle/Rector/Catch_/CatchExceptionNameMatchingTypeRector.php @@ -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; @@ -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( @@ -107,6 +114,8 @@ function (array $matches): string { return $output; } ); + $objectType = new ObjectType($newVariableName); + $newVariableName = $this->propertyNaming->fqnToVariableName($objectType); if ($oldVariableName === $newVariableName) { return null;