Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience method AbstractNode::findChildrenOfTypeVariable() #786

Merged
merged 4 commits into from
Apr 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/main/php/PHPMD/AbstractNode.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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