From c5a15d559ab3cdab1d8a602f714a05feb3e00145 Mon Sep 17 00:00:00 2001 From: Leonardo Losoviz Date: Mon, 14 Dec 2020 19:41:36 +0800 Subject: [PATCH] [Downgrade] Fix parameter type widening issue, when method lives on the ancestor's interface (#4877) Co-authored-by: rector-bot --- .../DowngradeParameterTypeWideningRector.php | 15 +++++- .../Fixture/interface_on_parent_class.php.inc | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 rules/downgrade-php72/tests/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/interface_on_parent_class.php.inc diff --git a/rules/downgrade-php72/src/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php b/rules/downgrade-php72/src/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php index c543031b28b..b9d207667ff 100644 --- a/rules/downgrade-php72/src/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php +++ b/rules/downgrade-php72/src/Rector/ClassMethod/DowngradeParameterTypeWideningRector.php @@ -150,8 +150,21 @@ CODE_SAMPLE foreach ($ancestorAndInterfaceClassNames as $ancestorClassOrInterface) { /** @var string */ $parentClassName = $ancestorClassOrInterface->getAttribute(AttributeKey::CLASS_NAME); - /** @var ClassMethod */ $classMethod = $this->nodeRepository->findClassMethod($parentClassName, $methodName); + /** + * If it doesn't find the method, it's because the method + * lives somewhere else. + * For instance, in test "interface_on_parent_class.php.inc", + * the ancestor abstract class is also retrieved + * as containing the method, but it does not: it is + * in its implemented interface. That happens because + * `ReflectionMethod` doesn't allow to do do the distinction. + * The interface is also retrieve though, so that method + * will eventually be refactored. + */ + if ($classMethod === null) { + continue; + } $this->removeParamTypeFromMethod($ancestorClassOrInterface, $position, $classMethod); $this->removeParamTypeFromMethodForChildren($parentClassName, $methodName, $position); } diff --git a/rules/downgrade-php72/tests/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/interface_on_parent_class.php.inc b/rules/downgrade-php72/tests/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/interface_on_parent_class.php.inc new file mode 100644 index 00000000000..6550916a16a --- /dev/null +++ b/rules/downgrade-php72/tests/Rector/ClassMethod/DowngradeParameterTypeWideningRector/Fixture/interface_on_parent_class.php.inc @@ -0,0 +1,48 @@ + +----- +