Skip to content

Commit

Permalink
Implemented #8927307: Precedence handling implemented.
Browse files Browse the repository at this point in the history
This should be the last commit for PHP_Depend's trait implementation.
  • Loading branch information
manuelpichler committed Jan 29, 2012
1 parent 0e4d9b2 commit 84f612e
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/php/PHP/Depend/Code/ASTNodeI.php
Expand Up @@ -110,7 +110,7 @@ function getChild($index);
/**
* This method returns all direct children of the actual node.
*
* @return array(PHP_Depend_Code_ASTNodeI)
* @return PHP_Depend_Code_ASTNodeI[]
*/
function getChildren();

Expand Down
33 changes: 33 additions & 0 deletions src/main/php/PHP/Depend/Code/ASTTraitUseStatement.php
Expand Up @@ -89,6 +89,39 @@ public function getAllMethods()
return $this->_allMethods;
}

/**
* This method tests if the given {@link PHP_Depend_Code_Method} is excluded
* by precedence statement in this use statement. It will return <b>true</b>
* if the given <b>$method</b> is excluded, otherwise the return value of
* this method will be <b>false</b>.
*
* @param PHP_Depend_Code_Method $method The method to test for exclusion.
*
* @return boolean
*/
public function hasExcludeFor(PHP_Depend_Code_Method $method)
{
$methodName = strtolower($method->getName());
$methodParent = $method->getParent();

$precedences = $this->findChildrenOfType(
PHP_Depend_Code_ASTTraitAdaptationPrecedence::CLAZZ
);

foreach ($precedences as $precedence) {
if (strtolower($precedence->getImage()) !== $methodName) {
continue;
}
$children = $precedence->getChildren();
for ($i = 1, $count = count($children); $i < $count; ++$i) {
if ($methodParent === $children[$i]->getType()) {
return true;
}
}
}
return false;
}

/**
* Collects all directly defined methods or method aliases for the given
* {@link PHP_Depend_Code_ASTTraitReference}
Expand Down
5 changes: 5 additions & 0 deletions src/main/php/PHP/Depend/Code/Trait.php
Expand Up @@ -83,6 +83,11 @@ public function getAllMethods()

foreach ($uses as $use) {
foreach ($use->getAllMethods() as $method) {
foreach ($uses as $use2) {
if ($use2->hasExcludeFor($method)) {
continue 2;
}
}

$name = strtolower($method->getName());

Expand Down
27 changes: 27 additions & 0 deletions src/test/php/PHP/Depend/Code/TraitTest.php
Expand Up @@ -217,6 +217,33 @@ public function testGetAllMethodsWithVisibilityChangedKeepsStaticModifier()
);
}

/**
* testGetAllMethodsHandlesTraitMethodPrecedence
*
* @return void
*/
public function testGetAllMethodsHandlesTraitMethodPrecedence()
{
$trait = $this->getFirstTraitForTest();
$methods = $trait->getAllMethods();

$this->assertEquals(
'testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne',
$methods['foo']->getParent()->getName()
);
}

/**
* testGetAllMethodsExcludeTraitMethodWithPrecedence
*
* @return void
*/
public function testGetAllMethodsExcludeTraitMethodWithPrecedence()
{
$trait = $this->getFirstTraitForTest();
$this->assertEquals(1, count($trait->getAllMethods()));
}

/**
* testGetAllMethodsWithMethodCollisionThrowsExpectedException
*
Expand Down
@@ -0,0 +1,25 @@
<?php
trait testGetAllMethodsHandlesTraitMethodPrecedence
{
use testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne {
testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo::foo
insteadof
testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne;
}
use testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo {
testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne::foo
insteadof
testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo;
}
}

trait testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne
{
function foo() {}
function bar() {}
}

trait testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo
{
function foo() {}
}
@@ -0,0 +1,21 @@
<?php
trait testGetAllMethodsHandlesTraitMethodPrecedence
{
use testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne;
use testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo {
testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne::foo
insteadof
testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo;
}
}

trait testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitOne
{
function foo() {}
function bar() {}
}

trait testGetAllMethodsHandlesTraitMethodPrecedenceUsedTraitTwo
{
function foo() {}
}

0 comments on commit 84f612e

Please sign in to comment.