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 allow-underscore option for CamelCaseParameterName & CamelCaseVariableName #749

Merged
merged 6 commits into from Apr 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
11 changes: 10 additions & 1 deletion src/main/php/PHPMD/Rule/Controversial/CamelCaseParameterName.php
Expand Up @@ -40,7 +40,7 @@ class CamelCaseParameterName extends AbstractRule implements MethodAware, Functi
public function apply(AbstractNode $node)
{
foreach ($node->getParameters() as $parameter) {
if (!preg_match('/^\$[a-z][a-zA-Z0-9]*$/', $parameter->getName())) {
if (!$this->isValid($parameter->getName())) {
$this->addViolation(
$node,
array(
Expand All @@ -50,4 +50,13 @@ public function apply(AbstractNode $node)
}
}
}

private function isValid($parameterName)
{
if ($this->getBooleanProperty('allow-underscore')) {
return preg_match('/^\$[_]?[a-z][a-zA-Z0-9]*$/', $parameterName);
}

return preg_match('/^\$[a-z][a-zA-Z0-9]*$/', $parameterName);
}
}
38 changes: 28 additions & 10 deletions src/main/php/PHPMD/Rule/Controversial/CamelCaseVariableName.php
Expand Up @@ -57,21 +57,39 @@ class CamelCaseVariableName extends AbstractRule implements MethodAware, Functio
public function apply(AbstractNode $node)
{
foreach ($node->findChildrenOfType('Variable') as $variable) {
$image = $variable->getImage();

if (in_array($image, $this->exceptions)) {
continue;
if (!$this->isValid($variable)) {
$this->addViolation(
$node,
array(
$variable->getImage(),
)
);
}
}
}

if (preg_match('/^\$[a-z][a-zA-Z0-9]*$/', $image)) {
continue;
}
private function isValid($variable)
{
$image = $variable->getImage();

if ($variable->getParent()->isInstanceOf('PropertyPostfix')) {
continue;
if (in_array($image, $this->exceptions)) {
return true;
}

if ($this->getBooleanProperty('allow-underscore')) {
if (preg_match('/^\$[_]?[a-z][a-zA-Z0-9]*$/', $image)) {
return true;
}
}

$this->addViolation($node, array($image));
if (preg_match('/^\$[a-z][a-zA-Z0-9]*$/', $image)) {
return true;
}

if ($variable->getParent()->isInstanceOf('PropertyPostfix')) {
return true;
}

return false;
}
}
12 changes: 10 additions & 2 deletions src/main/resources/rulesets/controversial.xml
Expand Up @@ -122,7 +122,11 @@ It is considered best practice to use the camelCase notation to name parameters.
]]>
</description>
<priority>1</priority>
<properties />
<properties>
<property name="allow-underscore"
description="Allow an optional, single underscore at the beginning."
value="false" />
</properties>
<example>
<![CDATA[
class ClassName {
Expand All @@ -144,7 +148,11 @@ It is considered best practice to use the camelCase notation to name variables.
]]>
</description>
<priority>1</priority>
<properties />
<properties>
<property name="allow-underscore"
description="Allow an optional, single underscore at the beginning."
value="false" />
</properties>
<example>
<![CDATA[
class ClassName {
Expand Down
16 changes: 16 additions & 0 deletions src/site/rst/rules/controversial.rst
Expand Up @@ -92,6 +92,14 @@ Example: ::
}
}

This rule has the following properties:

+-----------------------------------+---------------+---------------------------------------------------------+
| Name | Default Value | Description |
+===================================+===============+=========================================================+
| allow-underscore | false | Allow an optional, single underscore at the beginning. |
+-----------------------------------+---------------+---------------------------------------------------------+

CamelCaseVariableName
=====================

Expand All @@ -107,6 +115,14 @@ Example: ::
}
}

This rule has the following properties:

+-----------------------------------+---------------+---------------------------------------------------------+
| Name | Default Value | Description |
+===================================+===============+=========================================================+
| allow-underscore | false | Allow an optional, single underscore at the beginning. |
+-----------------------------------+---------------+---------------------------------------------------------+

Remark
======

Expand Down
Expand Up @@ -36,6 +36,7 @@ public function testRuleDoesApplyForInvariableNameWithUnderscore()

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

Expand All @@ -51,6 +52,7 @@ public function testRuleDoesApplyForVariableNameWithCapital()

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

Expand All @@ -65,6 +67,7 @@ public function testRuleDoesNotApplyForValidVariableName()

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

Expand All @@ -79,6 +82,23 @@ public function testRuleDoesNotApplyForStaticVariableAccess()

$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.
*
* @return void
*/
public function testRuleDoesNotApplyForValidVariableNameWithUnderscoreWhenAllowed()
{
$report = $this->getReportMock(0);

$rule = new CamelCaseVariableName();
$rule->setReport($report);
$rule->addProperty('allow-underscore', 'true');
$rule->apply($this->getClass());
}
}
@@ -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 testRuleDoesNotApplyForValidVariableNameWithUnderscoreWhenAllowed
{
public function validVariableNameWithUnderscoreWhenAllowed()
{
$_dataModule = 'foo';
}
}