Skip to content

Commit

Permalink
Added support for short variable name exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillsablin committed Jan 28, 2013
1 parent da62eb0 commit 1484e22
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/php/PHP/PMD/Rule/Naming/ShortVariable.php
Expand Up @@ -140,15 +140,33 @@ protected function checkNodeImage(PHP_PMD_AbstractNode $node)
protected function doCheckNodeImage(PHP_PMD_AbstractNode $node)
{
$threshold = $this->getIntProperty('minimum');
$exceptions = $this->getExceptionsList();

if ($threshold <= strlen($node->getImage()) - 1) {
return;
}

if (in_array(substr($node->getImage(),1), $exceptions)) {
return;
}

if ($this->isNameAllowedInContext($node)) {
return;
}
$this->addViolation($node, array($node->getImage(), $threshold));
}

/**
* Gets array of exceptions from property
*
* @return array
*/
private function getExceptionsList()
{

return explode(',',$this->getStringProperty('exceptions'));
}

/**
* Checks if a short name is acceptable in the current context. For the
* moment these contexts are the init section of a for-loop and short
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/rulesets/naming.xml
Expand Up @@ -19,6 +19,7 @@ Detects when a field, local, or parameter has a very short name.
<priority>3</priority>
<properties>
<property name="minimum" description="Minimum length for a variable, property or parameter name" value="3"/>
<property name="exceptions" description="Comma-separated list of exceptions" value=""/>
</properties>
<example>
<![CDATA[
Expand Down
2 changes: 2 additions & 0 deletions src/site/rst/rules/naming.rst
Expand Up @@ -31,6 +31,8 @@ This rule has the following properties:
=================================== =============== ===========================================================
minimum 3 Minimum length for a variable, property or parameter name
=================================== =============== ===========================================================
exceptions empty Comma-separated list of names that should be allowed
=================================== =============== ===========================================================

LongVariable
============
Expand Down
36 changes: 36 additions & 0 deletions src/test/php/PHP/PMD/Rule/Naming/ShortVariableTest.php
Expand Up @@ -70,6 +70,7 @@
*/
class PHP_PMD_Rule_Naming_ShortVariableTest extends PHP_PMD_AbstractTest
{

/**
* testRuleAppliesToLocalVariableInFunctionWithNameShorterThanThreshold
*
Expand All @@ -79,6 +80,7 @@ public function testRuleAppliesToLocalVariableInFunctionWithNameShorterThanThres
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getFunction());
}
Expand All @@ -92,6 +94,7 @@ public function testRuleNotAppliesToLocalVariableInFunctionWithNameLongerThanThr
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 2);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -105,6 +108,7 @@ public function testRuleNotAppliesToLocalVariableInFunctionWithNameEqualToThresh
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -118,6 +122,7 @@ public function testRuleAppliesToFunctionParameterWithNameShorterThanThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getFunction());
}
Expand All @@ -131,6 +136,7 @@ public function testRuleNotAppliesToFunctionParameterWithNameLongerThanThreshold
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -144,6 +150,7 @@ public function testRuleAppliesToLocalVariableInMethodWithNameShorterThanThresho
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(1));

$class = $this->getClass();
Expand All @@ -163,6 +170,7 @@ public function testRuleNotAppliesToLocalVariableInMethodWithNameEqualToThreshol
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getClass());
}
Expand All @@ -176,6 +184,7 @@ public function testRuleNotAppliesToLocalVariableInMethodWithNameLongerThanThres
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 2);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getClass());
}
Expand All @@ -189,6 +198,7 @@ public function testRuleAppliesToMethodParameterWithNameShorterThanThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(1));

$class = $this->getClass();
Expand All @@ -208,6 +218,7 @@ public function testRuleNotAppliesToMethodParameterWithNameLongerThanThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 2);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getClass());
}
Expand All @@ -221,6 +232,7 @@ public function testRuleAppliesToFieldWithNameShorterThanThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(1));
$rule->apply($this->getClass());
}
Expand All @@ -234,6 +246,7 @@ public function testRuleNotAppliesToFieldWithNameEqualToThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getClass());
}
Expand All @@ -247,6 +260,7 @@ public function testRuleNotAppliesToFieldWithNameGreaterThanThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 2);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getClass());
}
Expand All @@ -260,6 +274,7 @@ public function testRuleAppliesToFieldAndParameterWithNameShorterThanThreshold()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(2));

$class = $this->getClass();
Expand All @@ -279,6 +294,7 @@ public function testRuleNotAppliesToShortVariableNameAsForLoopIndex()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -292,6 +308,7 @@ public function testRuleNotAppliesToShortVariableNameAsForeachLoopIndex()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -305,6 +322,7 @@ public function testRuleNotAppliesToShortVariableNameInCatchStatement()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getFunction());
}
Expand All @@ -318,6 +336,7 @@ public function testRuleNotAppliesToStaticMembersAccessedInMethod()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(0));
$rule->apply($this->getMethod());
}
Expand All @@ -331,6 +350,7 @@ public function testRuleAppliesToIdenticalVariableOnlyOneTime()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(2));
$rule->apply($this->getMethod());
}
Expand All @@ -344,6 +364,7 @@ public function testRuleAppliesToIdenticalVariablesInDifferentContextsSeveralTim
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','');
$rule->setReport($this->getReportMock(2));

$class = $this->getClass();
Expand All @@ -353,4 +374,19 @@ public function testRuleAppliesToIdenticalVariablesInDifferentContextsSeveralTim
$rule->apply($method);
}
}

/**
* testRuleNotAppliesToVariablesFromExceptionsList
*
* @return void
*/
public function testRuleNotAppliesToVariablesFromExceptionsList()
{
$rule = new PHP_PMD_Rule_Naming_ShortVariable();
$rule->addProperty('minimum', 3);
$rule->addProperty('exceptions','id');
$rule->setReport($this->getReportMock(0));

$rule->apply($this->getClass());
}
}
@@ -0,0 +1,11 @@
<?php
class testRuleNotAppliesToVariablesFromExceptionsList
{
private $id;

public function setID($id)
{
$this->id = $id;
}

}

0 comments on commit 1484e22

Please sign in to comment.