Skip to content

Commit

Permalink
Move length calculation inside the if for consistency amongst rules
Browse files Browse the repository at this point in the history
Moved "threshold" to the right side so it's better understandable.
Tightened the LongVariable length constraints in the test case.
  • Loading branch information
frankdekker committed May 5, 2020
1 parent 083628e commit 0e56d78
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/main/php/PHPMD/Rule/Naming/LongClassName.php
Expand Up @@ -46,10 +46,10 @@ class LongClassName extends AbstractRule implements ClassAware, InterfaceAware
public function apply(AbstractNode $node)
{
$threshold = $this->getIntProperty('maximum');
$length = Strings::length($node->getName(), $this->getSubtractSuffixList());
if ($length > $threshold) {
$this->addViolation($node, array($node->getName(), $threshold));
if (Strings::length($node->getName(), $this->getSubtractSuffixList()) <= $threshold) {
return;
}
$this->addViolation($node, array($node->getName(), $threshold));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/main/php/PHPMD/Rule/Naming/LongVariable.php
Expand Up @@ -106,7 +106,7 @@ protected function checkNodeImage(AbstractNode $node)
protected function checkMaximumLength(AbstractNode $node)
{
$threshold = $this->getIntProperty('maximum');
if ($threshold >= Strings::length($node->getImage(), $this->getSubtractSuffixList()) - 1) {
if (Strings::length($node->getImage(), $this->getSubtractSuffixList()) - 1 <= $threshold) {
return;
}
if ($this->isNameAllowedInContext($node)) {
Expand Down
3 changes: 1 addition & 2 deletions src/main/php/PHPMD/Rule/Naming/ShortClassName.php
Expand Up @@ -45,8 +45,7 @@ class ShortClassName extends AbstractRule implements ClassAware, InterfaceAware
public function apply(AbstractNode $node)
{
$threshold = $this->getIntProperty('minimum');

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

Expand Down
4 changes: 2 additions & 2 deletions src/test/php/PHPMD/Rule/Naming/LongVariableTest.php
Expand Up @@ -34,7 +34,7 @@ class LongVariableTest extends AbstractTest
public function testRuleAppliesToLocalVariableInFunctionWithNameLongerThanThreshold()
{
$rule = new LongVariable();
$rule->addProperty('maximum', 17);
$rule->addProperty('maximum', 21);
$rule->setReport($this->getReportWithOneViolation());
$rule->apply($this->getFunction());
}
Expand All @@ -47,7 +47,7 @@ public function testRuleAppliesToLocalVariableInFunctionWithNameLongerThanThresh
public function testRuleNotAppliesToLocalVariableInFunctionWithNameSmallerThanThreshold()
{
$rule = new LongVariable();
$rule->addProperty('maximum', 17);
$rule->addProperty('maximum', 6);
$rule->setReport($this->getReportWithNoViolation());
$rule->apply($this->getFunction());
}
Expand Down

0 comments on commit 0e56d78

Please sign in to comment.