Skip to content

Commit

Permalink
Merge pull request #1026 from phpmd/fix/issue-467-duplicate-camel-cas…
Browse files Browse the repository at this point in the history
…e-violation

Dedupe violation for CamelCaseVariableName
  • Loading branch information
kylekatarnls committed Aug 21, 2023
2 parents 638eb73 + e0a5fb3 commit 208f850
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/main/php/PHPMD/Rule/Controversial/CamelCaseVariableName.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,16 @@ class CamelCaseVariableName extends AbstractRule implements MethodAware, Functio
*/
public function apply(AbstractNode $node)
{
$variables = array();

foreach ($node->findChildrenOfTypeVariable() as $variable) {
if (!$this->isValid($variable)) {
$this->addViolation(
$node,
array(
$variable->getImage(),
)
);
$variableName = $variable->getImage();

if (!isset($variables[$variableName])) {
$variables[$variableName] = true;
$this->addViolation($variable, array($variableName));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,21 @@ public function testRuleDoesNotApplyForStaticVariableAccess()
$rule->apply($this->getClass());
}

/**
* Tests that the rule does NOT apply if name allowed by config
*
* @return void
*/
public function testRuleDoesNotApplyIfExcluded()
{
$report = $this->getReportWithNoViolation();

$rule = new CamelCaseVariableName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'false');
$rule->apply($this->getClass());
}

/**
* Tests that the rule does apply for a valid variable name
* with an underscore at the beginning when it is allowed.
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 testRuleDoesNotApplyIfExcluded
{
public function validVariableName()
{
var_dump($_POST);
}
}

0 comments on commit 208f850

Please sign in to comment.