From 51486d72b3bfae2a472e66c568906d951bae0920 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Wed, 18 Oct 2023 09:56:20 +0200 Subject: [PATCH] [Naming] Skip DateTime from renaming, to keep convention of named dates --- .../Fixture/keep_date.php.inc | 12 ++++++++++++ ...RemoveUnusedNonEmptyArrayBeforeForeachRector.php | 2 +- .../MatchParamTypeExpectedNameResolver.php | 13 ++++++++++++- src/PhpParser/Node/BetterNodeFinder.php | 2 +- .../config/match_to_switch_reflection.php | 6 +++--- 5 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_date.php.inc diff --git a/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_date.php.inc b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_date.php.inc new file mode 100644 index 00000000000..61b978c8032 --- /dev/null +++ b/rules-tests/Naming/Rector/ClassMethod/RenameParamToMatchTypeRector/Fixture/keep_date.php.inc @@ -0,0 +1,12 @@ +isSuperTypeOf(new ObjectType('DateTimeInterface'))->yes()) { + if ($this->isDateTimeType($staticType)) { return null; } @@ -40,4 +41,14 @@ public function resolve(Param $param): ?string return $expectedName->getName(); } + + private function isDateTimeType(Type $type): bool + { + if ($type->isSuperTypeOf(new ObjectType('DateTimeInterface'))->yes()) { + return true; + } + + return $type->isSuperTypeOf(new ObjectType('DateTime')) + ->yes(); + } } diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 9fcc35ebe0b..8877d2711e0 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -192,7 +192,7 @@ static function (Node $subNode) use ($types, &$isFoundNode): ?int { * @return T[] */ public function findInstancesOfInFunctionLikeScoped( - ClassMethod | Function_ | Closure $functionLike, + ClassMethod | Function_ | Closure $functionLike, string|array $types ): array { if (is_string($types)) { diff --git a/tests/Issues/ScopeNotAvailable/config/match_to_switch_reflection.php b/tests/Issues/ScopeNotAvailable/config/match_to_switch_reflection.php index a5bbf73d86f..708acad8172 100644 --- a/tests/Issues/ScopeNotAvailable/config/match_to_switch_reflection.php +++ b/tests/Issues/ScopeNotAvailable/config/match_to_switch_reflection.php @@ -3,12 +3,12 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector; use Rector\DowngradePhp80\Rector\Expression\DowngradeMatchToSwitchRector; +use Rector\DowngradePhp81\Rector\StmtsAwareInterface\DowngradeSetAccessibleReflectionPropertyRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rules([ - DowngradeSetAccessibleReflectionPropertyRector::class, - DowngradeMatchToSwitchRector::class, + DowngradeSetAccessibleReflectionPropertyRector::class, + DowngradeMatchToSwitchRector::class, ]); };