diff --git a/rules-tests/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector/Fixture/allow_interface_return_on_private_method.php.inc b/rules-tests/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector/Fixture/allow_interface_return_on_private_method.php.inc new file mode 100644 index 00000000000..d0453c47913 --- /dev/null +++ b/rules-tests/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector/Fixture/allow_interface_return_on_private_method.php.inc @@ -0,0 +1,33 @@ + +----- + \ No newline at end of file diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php index bafc1688487..47ba1665bc8 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php @@ -150,7 +150,8 @@ public function refactor(Node $node): ?Node } // this rule narrows only object or class types, not interfaces - if (! $declaredTypeClassReflection->isClass()) { + // except private methods, as they are not part of contract, and not mockable from tests + if (! $declaredTypeClassReflection->isClass() && ! $node->isPrivate()) { return null; } }