Skip to content

Commit

Permalink
Fix #672 Handle passing-by-reference in native PHP functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed May 17, 2020
1 parent a6864a2 commit b244ec6
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php
Expand Up @@ -17,6 +17,7 @@

namespace PHPMD\Rule\CleanCode;

use PDepend\Source\AST\ASTArguments;
use PDepend\Source\AST\ASTArray;
use PDepend\Source\AST\ASTClass;
use PDepend\Source\AST\ASTPropertyPostfix;
Expand All @@ -31,6 +32,8 @@
use PHPMD\Rule\AbstractLocalVariable;
use PHPMD\Rule\FunctionAware;
use PHPMD\Rule\MethodAware;
use ReflectionException;
use ReflectionFunction;

/**
* This rule collects all undefined variables within a given function or method
Expand Down Expand Up @@ -77,9 +80,36 @@ public function apply(AbstractNode $node)
$this->addVariableDefinition($variable);
}

if (!$this->checkVariableDefined($variable, $node)) {
$this->addViolation($variable, array($this->getVariableImage($variable)));
$this->doCheckVariable($variable, $node);
}
}

private function doCheckVariable($variable, $node)
{
if (!$this->checkVariableDefined($variable, $node)) {
$parent = $this->getNode($variable->getParent());

if ($parent && $parent instanceof ASTArguments) {
$argumentPosition = array_search($this->getNode($variable), $parent->getChildren());
$function = $this->getNode($parent->getParent());
$functionName = $function->getImage();

try {
$reflectionFunction = new ReflectionFunction($functionName);
$parameters = $reflectionFunction->getParameters();

if ($parameters[$argumentPosition]->isPassedByReference()) {
$this->addVariableDefinition($variable);

return;
}
} catch (ReflectionException $exception) {
// @TODO: Find a way to handle user-land functions
// @TODO: Find a way to handle methods
}
}

$this->addViolation($variable, array($this->getVariableImage($variable)));
}
}

Expand Down

0 comments on commit b244ec6

Please sign in to comment.