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 May 16, 2024
1 parent f9e07be commit 219e572
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 17 deletions.
14 changes: 0 additions & 14 deletions src/Prophecy/Doubler/Generator/ClassMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,6 @@
*/
class ClassMirror
{
private const REFLECTABLE_METHODS = array(
'__construct',
'__destruct',
'__sleep',
'__wakeup',
'__toString',
'__call',
'__invoke'
);

/**
* Reflects provided arguments into class node.
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions tests/Doubler/Generator/ClassMirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,18 +302,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

0 comments on commit 219e572

Please sign in to comment.