Skip to content

Commit

Permalink
Closes #128: Problem when I use parent:: in trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Manuel Pichler committed Dec 4, 2014
1 parent 7aa2c5c commit a73e6de
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/main/php/PDepend/Source/Builder/Builder.php
Expand Up @@ -44,6 +44,7 @@

use PDepend\Source\AST\AbstractASTClassOrInterface;
use PDepend\Source\AST\ASTClass;
use PDepend\Source\AST\ASTClassOrInterfaceReference;
use PDepend\Source\AST\ASTFunction;
use PDepend\Source\AST\ASTInterface;
use PDepend\Source\AST\ASTTrait;
Expand Down Expand Up @@ -233,12 +234,10 @@ public function buildAstSelfReference(AbstractASTClassOrInterface $type);
*
* @param \PDepend\Source\AST\ASTClassOrInterfaceReference $reference The type
* instance that reference the concrete target of parent.
*
* @return \PDepend\Source\AST\ASTParentReference
* @since 0.9.6
*/
public function buildAstParentReference(
\PDepend\Source\AST\ASTClassOrInterfaceReference $reference
public function buildAstParentReference(ASTClassOrInterfaceReference $reference
);

/**
Expand Down
Expand Up @@ -52,6 +52,7 @@
use PDepend\Source\AST\ASTNode;
use PDepend\Source\AST\ASTStatement;
use PDepend\Source\AST\ASTSwitchStatement;
use PDepend\Source\AST\ASTTrait;
use PDepend\Source\AST\ASTValue;
use PDepend\Source\AST\State;
use PDepend\Source\Builder\Builder;
Expand Down Expand Up @@ -4416,7 +4417,12 @@ private function parseParentReference(Token $token)
);
}

$classReference = $this->classOrInterface->getParentClassReference();
if ($this->classOrInterface instanceof ASTTrait) {
$classReference = $this->builder->buildAstClassReference('__PDepend_TraitRuntimeReference');
} else {
$classReference = $this->classOrInterface->getParentClassReference();
}

if ($classReference === null) {
throw new InvalidStateException(
$token->startLine,
Expand Down
22 changes: 22 additions & 0 deletions src/test/php/PDepend/Source/AST/ASTTraitTest.php
Expand Up @@ -358,6 +358,28 @@ public function testGetMethodsReturnsExpectedNumberOfMethods()
$this->assertEquals(3, count($trait->getMethods()));
}

/**
* testTraitCanUseParentKeywordInMethodBody
*
* @return void
*/
public function testTraitCanUseParentKeywordInMethodBody()
{
$trait = $this->getFirstTraitForTest();
$this->assertNotNull($trait);
}

/**
* testTraitCanUseParentKeywordAsMethodTypeHint
*
* @return void
*/
public function testTraitCanUseParentKeywordAsMethodTypeHint()
{
$trait = $this->getFirstTraitForTest();
$this->assertNotNull($trait);
}

/**
* testAcceptInvokesVisitTraitOnGivenVisitor
*
Expand Down
@@ -0,0 +1,21 @@
<?php
trait testTraitCanUseParentKeywordAsMethodTypeHint
{
public function baz(parent $foo)
{
echo get_class($foo), PHP_EOL;
}
}

class Foo {
use testTraitCanUseParentKeywordAsMethodTypeHint;
}

class Bar {
use testTraitCanUseParentKeywordAsMethodTypeHint;
}

$foo = new Foo();
$bar = new Bar();

$foo->baz($foo);
@@ -0,0 +1,8 @@
<?php
trait testTraitCanUseParentKeywordInMethodBody
{
public function foo()
{
parent::foo();
}
}

0 comments on commit a73e6de

Please sign in to comment.