Skip to content

Commit

Permalink
Merge pull request #504 from ciaranmcnulty/inherited-self
Browse files Browse the repository at this point in the history
Fix handling of inherited 'self'
  • Loading branch information
ciaranmcnulty authored Sep 29, 2020
2 parents 765cd5d + 2a681a0 commit 54d7320
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
12 changes: 12 additions & 0 deletions fixtures/AbstractBaseClassWithMethodWithReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Fixtures\Prophecy;

abstract class AbstractBaseClassWithMethodWithReturnType
{
public function returnSelf(?\DateTimeInterface $test): self
{
}
}
9 changes: 9 additions & 0 deletions fixtures/ClassExtendAbstractWithMethodWithReturnType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Fixtures\Prophecy;

class ClassExtendAbstractWithMethodWithReturnType extends AbstractBaseClassWithMethodWithReturnType
{
}
10 changes: 5 additions & 5 deletions src/Prophecy/Doubler/Generator/ClassMirror.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private function reflectClassToNode(ReflectionClass $class, Node\ClassNode $node
continue;
}

$this->reflectMethodToNode($method, $node, $class);
$this->reflectMethodToNode($method, $node);
}

foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
Expand All @@ -118,7 +118,7 @@ private function reflectClassToNode(ReflectionClass $class, Node\ClassNode $node
continue;
}

$this->reflectMethodToNode($method, $node, $class);
$this->reflectMethodToNode($method, $node);
}
}

Expand All @@ -127,11 +127,11 @@ private function reflectInterfaceToNode(ReflectionClass $interface, Node\ClassNo
$node->addInterface($interface->getName());

foreach ($interface->getMethods() as $method) {
$this->reflectMethodToNode($method, $node, $interface);
$this->reflectMethodToNode($method, $node);
}
}

private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode, ReflectionClass $class)
private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode)
{
$node = new Node\MethodNode($method->getName());

Expand All @@ -148,7 +148,7 @@ private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $c
}

if ($method->hasReturnType()) {
$returnTypes = $this->getTypeHints($method->getReturnType(), $class, $method->getReturnType()->allowsNull());
$returnTypes = $this->getTypeHints($method->getReturnType(), $method->getDeclaringClass(), $method->getReturnType()->allowsNull());
$node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes));
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Doubler/Generator/ClassMirrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,14 @@ public function it_can_double_a_class_with_mixed_types()
$this->assertEquals(new ReturnTypeNode('mixed'), $methodNode->getReturnTypeNode());
}

/** @test */
public function it_can_double_inherited_self_return_type()
{
$classNode = (new ClassMirror())->reflect(new \ReflectionClass('Fixtures\Prophecy\ClassExtendAbstractWithMethodWithReturnType'), []);
$methodNode = $classNode->getMethods()['returnSelf'];

$this->assertEquals(new ReturnTypeNode('Fixtures\Prophecy\AbstractBaseClassWithMethodWithReturnType'), $methodNode->getReturnTypeNode());
}

/**
* @test
Expand Down

0 comments on commit 54d7320

Please sign in to comment.