From 11a9a5fbbf32625da149e47eb72f2fa9314bdb35 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 2 Dec 2025 16:41:32 +0700 Subject: [PATCH 1/2] [TypeDeclaration] Allow interface return change to exact return on private method on NarrowObjectReturnTypeRector --- ...interface_return_on_private_method.php.inc | 33 +++++++++++++++++++ .../NarrowObjectReturnTypeRector.php | 2 +- 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 rules-tests/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector/Fixture/allow_interface_return_on_private_method.php.inc 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..60f2e22edd3 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php @@ -150,7 +150,7 @@ public function refactor(Node $node): ?Node } // this rule narrows only object or class types, not interfaces - if (! $declaredTypeClassReflection->isClass()) { + if (! $declaredTypeClassReflection->isClass() && ! $node->isPrivate()) { return null; } } From 185289a4bc2becd7f61647a3f8da6e6c13a0819a Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Tue, 2 Dec 2025 16:44:41 +0700 Subject: [PATCH 2/2] add comment --- .../Rector/ClassMethod/NarrowObjectReturnTypeRector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php b/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php index 60f2e22edd3..47ba1665bc8 100644 --- a/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php +++ b/rules/TypeDeclaration/Rector/ClassMethod/NarrowObjectReturnTypeRector.php @@ -150,6 +150,7 @@ public function refactor(Node $node): ?Node } // this rule narrows only object or class types, not interfaces + // except private methods, as they are not part of contract, and not mockable from tests if (! $declaredTypeClassReflection->isClass() && ! $node->isPrivate()) { return null; }