Skip to content

Commit

Permalink
Support methods prefixed with underscore
Browse files Browse the repository at this point in the history
Fixes phpspec#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.
  • Loading branch information
neclimdul committed Jun 10, 2022
1 parent bbcd738 commit c9d1655
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 19 deletions.
15 changes: 0 additions & 15 deletions src/Prophecy/Doubler/Generator/ClassMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,6 @@
*/
class ClassMirror
{
private static $reflectableMethods = array(
'__construct',
'__destruct',
'__sleep',
'__wakeup',
'__toString',
'__call',
'__invoke'
);

/**
* Reflects provided arguments into class node.
*
Expand Down Expand Up @@ -109,11 +99,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::$reflectableMethods)) {
continue;
}

if (true === $method->isFinal()) {
$node->addUnextendableMethod($method->getName());
continue;
Expand Down
8 changes: 4 additions & 4 deletions tests/Doubler/Generator/ClassMirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,18 @@ 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');

$mirror = new ClassMirror();

$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'));
}

/**
Expand Down Expand Up @@ -614,7 +614,7 @@ public function it_can_not_double_an_enum()
}

$this->expectException(ClassMirrorException::class);

$classNode = (new ClassMirror())->reflect(new \ReflectionClass('Fixtures\Prophecy\Enum'), []);
}

Expand Down

0 comments on commit c9d1655

Please sign in to comment.