Skip to content

Commit

Permalink
Handle static properties definition
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed May 16, 2020
1 parent ef1705f commit 6f4f898
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/AbstractLocalVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected function getVariableImage($variable)
*
* @return \PDepend\Source\AST\ASTArtifact|\PDepend\Source\AST\ASTNode
*/
private function getNode($node)
protected function getNode($node)
{
if ($node instanceof ASTNode) {
return $node->getNode();
Expand Down
23 changes: 23 additions & 0 deletions src/main/php/PHPMD/Rule/CleanCode/UndefinedVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace PHPMD\Rule\CleanCode;

use PDepend\Source\AST\ASTArray;
use PDepend\Source\AST\ASTClass;
use PDepend\Source\AST\ASTPropertyPostfix;
use PDepend\Source\AST\ASTUnaryExpression;
use PDepend\Source\AST\ASTVariable;
Expand Down Expand Up @@ -55,9 +56,17 @@ public function apply(AbstractNode $node)
{
$this->images = array();

if ($node instanceof MethodNode) {
$this->collectProperties($this->getNode($node->getNode()->getParent()));
}

$this->collect($node);

foreach ($node->findChildrenOfType('Class') as $class) {
/** @var ASTClass $class */

$this->collectProperties($class);

foreach ($class->getMethods() as $method) {
$this->collect(new MethodNode($method));
}
Expand All @@ -67,6 +76,7 @@ public function apply(AbstractNode $node)
if (!$this->isNotSuperGlobal($variable)) {
$this->addVariableDefinition($variable);
}

if (!$this->checkVariableDefined($variable, $node)) {
$this->addViolation($variable, array($this->getVariableImage($variable)));
}
Expand All @@ -90,6 +100,19 @@ private function collect(AbstractNode $node)
$this->collectGlobalStatements($node);
}

private function collectProperties($node)
{
if (!($node instanceof ASTClass)) {
return;
}

foreach ($node->getProperties() as $property) {
if ($property->isStatic()) {
$this->images['::'.$property->getName()] = $property;
}
}
}

/**
* Stores the given literal node in an global of found variables.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* This file is part of PHP Mess Detector.
*
* Copyright (c) Manuel Pichler <mapi@phpmd.org>.
* All rights reserved.
*
* Licensed under BSD License
* For full copyright and license information, please see the LICENSE file.
* Redistributions of files must retain the above copyright notice.
*
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright Manuel Pichler. All rights reserved.
* @license https://opensource.org/licenses/bsd-license.php BSD License
* @link http://phpmd.org/
*/

class testRuleAppliesToStaticProperties
{
function testRuleAppliesToStaticProperties($key)
{
return static::$array[$key];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class testRuleDoesNotApplyToStaticProperties
{
protected $foo = [];
protected static $array = [];

function testRuleDoesNotApplyToStaticProperties($key)
Expand All @@ -25,6 +26,6 @@ function testRuleDoesNotApplyToStaticProperties($key)
return static::$array[$key];
}

return static::$array[$key] = $key;
return $key;
}
}

0 comments on commit 6f4f898

Please sign in to comment.