diff --git a/src/main/php/PHPMD/AbstractNode.php b/src/main/php/PHPMD/AbstractNode.php index dc13f8bc2..50a59e638 100644 --- a/src/main/php/PHPMD/AbstractNode.php +++ b/src/main/php/PHPMD/AbstractNode.php @@ -18,6 +18,7 @@ namespace PHPMD; use PDepend\Source\AST\AbstractASTArtifact; +use PDepend\Source\AST\ASTVariable; use PHPMD\Node\ASTNode; /** @@ -138,6 +139,17 @@ public function findChildrenOfType($type) return $nodes; } + /** + * Searches recursive for all children of this node that are of variable. + * + * @return ASTVariable[] + * @todo Cover by a test. + */ + public function findChildrenOfTypeVariable() + { + return $this->findChildrenOfType('Variable'); + } + /** * Tests if this node represents the the given type. * diff --git a/src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php b/src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php index 510e0c2df..091714d88 100644 --- a/src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php +++ b/src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php @@ -72,9 +72,7 @@ public function apply(AbstractNode $node) } } - foreach ($node->findChildrenOfType('Variable') as $variable) { - /** @var ASTVariable $variable */ - + foreach ($node->findChildrenOfTypeVariable() as $variable) { if ($this->isSuperGlobal($variable) || $this->isPassedByReference($variable)) { $this->addVariableDefinition($variable); } elseif (!$this->checkVariableDefined($variable, $node)) { @@ -251,7 +249,7 @@ protected function collectAssignments(AbstractCallableNode $node) $variable = $assignment->getChild(0); if ($variable->getNode() instanceof ASTArray) { - foreach ($variable->findChildrenOfType('Variable') as $unpackedVariable) { + foreach ($variable->findChildrenOfTypeVariable() as $unpackedVariable) { $this->addVariableDefinition($unpackedVariable); } diff --git a/src/main/php/PHPMD/Rule/Controversial/CamelCaseVariableName.php b/src/main/php/PHPMD/Rule/Controversial/CamelCaseVariableName.php index 620711f0b..4dbfd87ec 100644 --- a/src/main/php/PHPMD/Rule/Controversial/CamelCaseVariableName.php +++ b/src/main/php/PHPMD/Rule/Controversial/CamelCaseVariableName.php @@ -56,7 +56,7 @@ class CamelCaseVariableName extends AbstractRule implements MethodAware, Functio */ public function apply(AbstractNode $node) { - foreach ($node->findChildrenOfType('Variable') as $variable) { + foreach ($node->findChildrenOfTypeVariable() as $variable) { if (!$this->isValid($variable)) { $this->addViolation( $node, diff --git a/src/main/php/PHPMD/Rule/Controversial/Superglobals.php b/src/main/php/PHPMD/Rule/Controversial/Superglobals.php index 81224e66d..559b5b14b 100644 --- a/src/main/php/PHPMD/Rule/Controversial/Superglobals.php +++ b/src/main/php/PHPMD/Rule/Controversial/Superglobals.php @@ -58,7 +58,7 @@ class Superglobals extends AbstractRule implements MethodAware, FunctionAware */ public function apply(AbstractNode $node) { - foreach ($node->findChildrenOfType('Variable') as $variable) { + foreach ($node->findChildrenOfTypeVariable() as $variable) { if (in_array($variable->getImage(), $this->superglobals)) { $this->addViolation( $node, diff --git a/src/main/php/PHPMD/Rule/Naming/LongVariable.php b/src/main/php/PHPMD/Rule/Naming/LongVariable.php index 159aac559..3b26c390e 100644 --- a/src/main/php/PHPMD/Rule/Naming/LongVariable.php +++ b/src/main/php/PHPMD/Rule/Naming/LongVariable.php @@ -74,7 +74,7 @@ public function apply(AbstractNode $node) $this->checkNodeImage($declarator); } - $variables = $node->findChildrenOfType('Variable'); + $variables = $node->findChildrenOfTypeVariable(); foreach ($variables as $variable) { $this->checkNodeImage($variable); } diff --git a/src/main/php/PHPMD/Rule/Naming/ShortVariable.php b/src/main/php/PHPMD/Rule/Naming/ShortVariable.php index 29f25e486..b61544882 100644 --- a/src/main/php/PHPMD/Rule/Naming/ShortVariable.php +++ b/src/main/php/PHPMD/Rule/Naming/ShortVariable.php @@ -96,7 +96,7 @@ protected function applyNonClass(AbstractNode $node) $this->checkNodeImage($declarator); } - $variables = $node->findChildrenOfType('Variable'); + $variables = $node->findChildrenOfTypeVariable(); foreach ($variables as $variable) { $this->checkNodeImage($variable); } diff --git a/src/main/php/PHPMD/Rule/UnusedFormalParameter.php b/src/main/php/PHPMD/Rule/UnusedFormalParameter.php index 2538ef959..33498cd9b 100644 --- a/src/main/php/PHPMD/Rule/UnusedFormalParameter.php +++ b/src/main/php/PHPMD/Rule/UnusedFormalParameter.php @@ -190,7 +190,7 @@ protected function removeUsedParameters(AbstractNode $node) */ protected function removeRegularVariables(AbstractNode $node) { - $variables = $node->findChildrenOfType('Variable'); + $variables = $node->findChildrenOfTypeVariable(); foreach ($variables as $variable) { /** @var $variable ASTNode */ diff --git a/src/main/php/PHPMD/Rule/UnusedLocalVariable.php b/src/main/php/PHPMD/Rule/UnusedLocalVariable.php index 8cef7a45d..9d66e4145 100644 --- a/src/main/php/PHPMD/Rule/UnusedLocalVariable.php +++ b/src/main/php/PHPMD/Rule/UnusedLocalVariable.php @@ -116,8 +116,7 @@ protected function removeParameters(AbstractCallableNode $node) */ protected function collectVariables(AbstractCallableNode $node) { - foreach ($node->findChildrenOfType('Variable') as $variable) { - /** @var $variable ASTNode */ + foreach ($node->findChildrenOfTypeVariable() as $variable) { if ($this->isLocal($variable)) { $this->collectVariable($variable); }