From dbdcaa188c139032272572f327fc13f1daab5f02 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 8 Mar 2022 01:48:25 +0700 Subject: [PATCH] [Php81] Skip override __construct from interface on NewInInitializerRector (#1913) Co-authored-by: GitHub Action --- .../NodeAnalyzer/ClassChildAnalyzer.php | 12 ++++++++---- .../skip_override_interface_method.php.inc | 17 +++++++++++++++++ .../Source/OverrideInterfaceMethod.php | 14 ++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_override_interface_method.php.inc create mode 100644 rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/OverrideInterfaceMethod.php diff --git a/packages/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php b/packages/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php index b0dbccfec9f..42c87954e6e 100644 --- a/packages/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php +++ b/packages/FamilyTree/NodeAnalyzer/ClassChildAnalyzer.php @@ -46,6 +46,9 @@ public function hasParentClassMethod(ClassReflection $classReflection, string $m return $this->resolveParentClassMethods($classReflection, $methodName) !== []; } + /** + * Look both parent class and interface, yes, all PHP interface methods are abstract + */ public function hasAbstractParentClassMethod(ClassReflection $classReflection, string $methodName): bool { $parentClassMethods = $this->resolveParentClassMethods($classReflection, $methodName); @@ -87,18 +90,19 @@ public function resolveParentClassMethodReturnType(ClassReflection $classReflect private function resolveParentClassMethods(ClassReflection $classReflection, string $methodName): array { $parentClassMethods = []; - foreach ($classReflection->getParents() as $parentClassReflections) { - if (! $parentClassReflections->hasMethod($methodName)) { + $parents = array_merge($classReflection->getParents(), $classReflection->getInterfaces()); + foreach ($parents as $parent) { + if (! $parent->hasMethod($methodName)) { continue; } - $methodReflection = $parentClassReflections->getNativeMethod($methodName); + $methodReflection = $parent->getNativeMethod($methodName); if (! $methodReflection instanceof PhpMethodReflection) { continue; } $methodDeclaringMethodClass = $methodReflection->getDeclaringClass(); - if ($methodDeclaringMethodClass->getName() === $parentClassReflections->getName()) { + if ($methodDeclaringMethodClass->getName() === $parent->getName()) { $parentClassMethods[] = $methodReflection; } } diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_override_interface_method.php.inc b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_override_interface_method.php.inc new file mode 100644 index 00000000000..d5a927051a8 --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Fixture/skip_override_interface_method.php.inc @@ -0,0 +1,17 @@ +dateTime = $dateTime ?? new DateTime('now'); + } +} diff --git a/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/OverrideInterfaceMethod.php b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/OverrideInterfaceMethod.php new file mode 100644 index 00000000000..9e722a5bf5f --- /dev/null +++ b/rules-tests/Php81/Rector/ClassMethod/NewInInitializerRector/Source/OverrideInterfaceMethod.php @@ -0,0 +1,14 @@ +