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

Dedupe violation for CamelCaseVariableName #1026

Merged
merged 1 commit into from
Aug 21, 2023
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
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);
}
}