From 47ad54dfc8bbeed5880ba95c0424dd41a01d209b Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 26 Aug 2021 14:51:06 +0700 Subject: [PATCH] [DowngradePhp72] Skip implements built in interface method on DowngradeParameterTypeWideningRector (#766) Co-authored-by: Bobab12 --- ...direct_implements_native_interface.php.inc | 42 ++++++++++++++++++ .../Fixture/skip_native_interface.php.inc | 10 +++++ .../NodeAnalyzer/BuiltInMethodAnalyzer.php | 43 +++++++++++++++++++ .../DowngradeParameterTypeWideningRector.php | 8 +++- 4 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/indirect_implements_native_interface.php.inc create mode 100644 rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/skip_native_interface.php.inc create mode 100644 rules/DowngradePhp72/NodeAnalyzer/BuiltInMethodAnalyzer.php diff --git a/rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/indirect_implements_native_interface.php.inc b/rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/indirect_implements_native_interface.php.inc new file mode 100644 index 00000000000..387650c54de --- /dev/null +++ b/rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/indirect_implements_native_interface.php.inc @@ -0,0 +1,42 @@ + +----- + diff --git a/rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/skip_native_interface.php.inc b/rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/skip_native_interface.php.inc new file mode 100644 index 00000000000..d460f691d9b --- /dev/null +++ b/rules-tests/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/skip_native_interface.php.inc @@ -0,0 +1,10 @@ +isClass()) { + return false; + } + + $methodName = $this->nodeNameResolver->getName($classMethod); + if ($this->classChildAnalyzer->hasChildClassMethod($classReflection, $methodName)) { + return false; + } + + foreach ($classReflection->getInterfaces() as $interfaceReflection) { + if (! $interfaceReflection->isBuiltIn()) { + continue; + } + + if (! $interfaceReflection->hasMethod($methodName)) { + continue; + } + + return true; + } + + return false; + } +} diff --git a/rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php b/rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php index 2ddcdc7eb0a..a678f8a4aa6 100644 --- a/rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php +++ b/rules/DowngradePhp72/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php @@ -11,6 +11,7 @@ use PHPStan\Reflection\ReflectionProvider; use Rector\Core\Contract\Rector\ConfigurableRectorInterface; use Rector\Core\Rector\AbstractRector; +use Rector\DowngradePhp72\NodeAnalyzer\BuiltInMethodAnalyzer; use Rector\DowngradePhp72\PhpDoc\NativeParamToPhpDocDecorator; use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\TypeDeclaration\NodeAnalyzer\AutowiredClassMethodOrPropertyAnalyzer; @@ -49,7 +50,8 @@ final class DowngradeParameterTypeWideningRector extends AbstractRector implemen public function __construct( private NativeParamToPhpDocDecorator $nativeParamToPhpDocDecorator, private ReflectionProvider $reflectionProvider, - private AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer + private AutowiredClassMethodOrPropertyAnalyzer $autowiredClassMethodOrPropertyAnalyzer, + private BuiltInMethodAnalyzer $builtInMethodAnalyzer ) { } @@ -140,6 +142,10 @@ public function refactor(Node $node): ?Node return null; } + if ($this->builtInMethodAnalyzer->isImplementsBuiltInInterface($classReflection, $node)) { + return null; + } + // Downgrade every scalar parameter, just to be sure foreach (array_keys($node->params) as $paramPosition) { $this->removeParamTypeFromMethod($node, $paramPosition);