From 84f612e9b1a33c21274871f54febbfb91a1bcb9e Mon Sep 17 00:00:00 2001 From: Manuel Pichler Date: Mon, 30 Jan 2012 00:41:31 +0100 Subject: [PATCH] Implemented #8927307: Precedence handling implemented. This should be the last commit for PHP_Depend's trait implementation. --- src/main/php/PHP/Depend/Code/ASTNodeI.php | 2 +- .../PHP/Depend/Code/ASTTraitUseStatement.php | 33 +++++++++++++++++++ src/main/php/PHP/Depend/Code/Trait.php | 5 +++ src/test/php/PHP/Depend/Code/TraitTest.php | 27 +++++++++++++++ ...ethodsExcludeTraitMethodWithPrecedence.php | 25 ++++++++++++++ ...AllMethodsHandlesTraitMethodPrecedence.php | 21 ++++++++++++ 6 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/test/resources/files/Code/Trait/testGetAllMethodsExcludeTraitMethodWithPrecedence.php create mode 100644 src/test/resources/files/Code/Trait/testGetAllMethodsHandlesTraitMethodPrecedence.php diff --git a/src/main/php/PHP/Depend/Code/ASTNodeI.php b/src/main/php/PHP/Depend/Code/ASTNodeI.php index 7d3e1e916f..3be841215d 100644 --- a/src/main/php/PHP/Depend/Code/ASTNodeI.php +++ b/src/main/php/PHP/Depend/Code/ASTNodeI.php @@ -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(); diff --git a/src/main/php/PHP/Depend/Code/ASTTraitUseStatement.php b/src/main/php/PHP/Depend/Code/ASTTraitUseStatement.php index 17cc1ee62a..5e0ee7b55b 100644 --- a/src/main/php/PHP/Depend/Code/ASTTraitUseStatement.php +++ b/src/main/php/PHP/Depend/Code/ASTTraitUseStatement.php @@ -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 true + * if the given $method is excluded, otherwise the return value of + * this method will be false. + * + * @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} diff --git a/src/main/php/PHP/Depend/Code/Trait.php b/src/main/php/PHP/Depend/Code/Trait.php index ccbf8ec0ad..118d0e0972 100644 --- a/src/main/php/PHP/Depend/Code/Trait.php +++ b/src/main/php/PHP/Depend/Code/Trait.php @@ -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()); diff --git a/src/test/php/PHP/Depend/Code/TraitTest.php b/src/test/php/PHP/Depend/Code/TraitTest.php index f232a2eb70..59467a406a 100644 --- a/src/test/php/PHP/Depend/Code/TraitTest.php +++ b/src/test/php/PHP/Depend/Code/TraitTest.php @@ -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 * diff --git a/src/test/resources/files/Code/Trait/testGetAllMethodsExcludeTraitMethodWithPrecedence.php b/src/test/resources/files/Code/Trait/testGetAllMethodsExcludeTraitMethodWithPrecedence.php new file mode 100644 index 0000000000..bbd2a529fe --- /dev/null +++ b/src/test/resources/files/Code/Trait/testGetAllMethodsExcludeTraitMethodWithPrecedence.php @@ -0,0 +1,25 @@ +