Skip to content

Commit

Permalink
Merge pull request #786 from phpmd/findChildrenOfVariable
Browse files Browse the repository at this point in the history
Add convenience method AbstractNode::findChildrenOfTypeVariable()
  • Loading branch information
kylekatarnls committed Apr 17, 2021
2 parents c826a7c + 8432720 commit 1a0ddb8
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/main/php/PHPMD/AbstractNode.php
Expand Up @@ -18,6 +18,7 @@
namespace PHPMD;

use PDepend\Source\AST\AbstractASTArtifact;
use PDepend\Source\AST\ASTVariable;
use PHPMD\Node\ASTNode;

/**
Expand Down Expand Up @@ -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.
*
Expand Down
6 changes: 2 additions & 4 deletions src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}

Expand Down
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/Controversial/Superglobals.php
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/Naming/LongVariable.php
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/Naming/ShortVariable.php
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/UnusedFormalParameter.php
Expand Up @@ -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 */
Expand Down
3 changes: 1 addition & 2 deletions src/main/php/PHPMD/Rule/UnusedLocalVariable.php
Expand Up @@ -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);
}
Expand Down

0 comments on commit 1a0ddb8

Please sign in to comment.