From 219e57280bee92f0161dbfdf9deb40b4a04d6433 Mon Sep 17 00:00:00 2001 From: James Gilliland Date: Fri, 10 Jun 2022 01:13:00 -0500 Subject: [PATCH] Support methods prefixed with underscore Fixes #108 While it was a common convention to prefix "private" methods with an underscore in earlier version of php, with support for method visibility this is no longer common. Additionally there are a number of exception such as the Soap library where this convention did not hold and code was not mockable. --- src/Prophecy/Doubler/Generator/ClassMirror.php | 14 -------------- tests/Doubler/Generator/ClassMirrorTest.php | 6 +++--- 2 files changed, 3 insertions(+), 17 deletions(-) diff --git a/src/Prophecy/Doubler/Generator/ClassMirror.php b/src/Prophecy/Doubler/Generator/ClassMirror.php index f5f3b7a4e..c69137867 100644 --- a/src/Prophecy/Doubler/Generator/ClassMirror.php +++ b/src/Prophecy/Doubler/Generator/ClassMirror.php @@ -31,15 +31,6 @@ */ class ClassMirror { - private const REFLECTABLE_METHODS = array( - '__construct', - '__destruct', - '__sleep', - '__wakeup', - '__toString', - '__call', - '__invoke' - ); /** * Reflects provided arguments into class node. @@ -116,11 +107,6 @@ private function reflectClassToNode(ReflectionClass $class, Node\ClassNode $node } foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { - if (0 === strpos($method->getName(), '_') - && !in_array($method->getName(), self::REFLECTABLE_METHODS)) { - continue; - } - if (true === $method->isFinal()) { $node->addUnextendableMethod($method->getName()); continue; diff --git a/tests/Doubler/Generator/ClassMirrorTest.php b/tests/Doubler/Generator/ClassMirrorTest.php index b77a9c3c3..271ae2492 100644 --- a/tests/Doubler/Generator/ClassMirrorTest.php +++ b/tests/Doubler/Generator/ClassMirrorTest.php @@ -302,7 +302,7 @@ public function it_reflects_all_interfaces_methods() /** * @test */ - public function it_ignores_virtually_private_methods() + public function it_does_not_ignores_virtually_private_methods() { $class = new \ReflectionClass('Fixtures\Prophecy\WithVirtuallyPrivateMethod'); @@ -310,10 +310,10 @@ public function it_ignores_virtually_private_methods() $classNode = $mirror->reflect($class, array()); - $this->assertCount(2, $classNode->getMethods()); + $this->assertCount(3, $classNode->getMethods()); $this->assertTrue($classNode->hasMethod('isAbstract')); $this->assertTrue($classNode->hasMethod('__toString')); - $this->assertFalse($classNode->hasMethod('_getName')); + $this->assertTrue($classNode->hasMethod('_getName')); } /**