From 19c4da8a00d9dfb09cbf2be29a324f5a63f288ae Mon Sep 17 00:00:00 2001 From: Adam Brett Date: Tue, 10 Dec 2013 15:04:11 +0000 Subject: [PATCH] Add: New parameter 'ignore-whitespace' to LongClass and LongMethod rules Quite often you don't care about Comment Lines of Code nor whitespace when counting the lines of code in a method. The ignore-whitespace option allows you to set these rules to ignore whitespace and only count the executable lines of code. This parameter defaults to `false` to avoid changing default behavior. --- .../php/PHP/PMD/Rule/Design/LongClass.php | 9 ++++++++- .../php/PHP/PMD/Rule/Design/LongMethod.php | 9 ++++++++- src/main/resources/rulesets/codesize.xml | 4 +++- .../php/PHP/PMD/Rule/Design/LongClassTest.php | 20 +++++++++++++++++++ .../PHP/PMD/Rule/Design/LongMethodTest.php | 20 +++++++++++++++++++ 5 files changed, 59 insertions(+), 3 deletions(-) diff --git a/src/main/php/PHP/PMD/Rule/Design/LongClass.php b/src/main/php/PHP/PMD/Rule/Design/LongClass.php index e507546d5..1ad658161 100644 --- a/src/main/php/PHP/PMD/Rule/Design/LongClass.php +++ b/src/main/php/PHP/PMD/Rule/Design/LongClass.php @@ -76,7 +76,14 @@ class PHP_PMD_Rule_Design_LongClass public function apply(PHP_PMD_AbstractNode $node) { $threshold = $this->getIntProperty('minimum'); - $loc = $node->getMetric('loc'); + $ignoreWhitespace = $this->getBooleanProperty('ignore-whitespace'); + + if ($ignoreWhitespace) { + $loc = $node->getMetric('eloc'); + } else { + $loc = $node->getMetric('loc'); + } + if ($loc < $threshold) { return; } diff --git a/src/main/php/PHP/PMD/Rule/Design/LongMethod.php b/src/main/php/PHP/PMD/Rule/Design/LongMethod.php index 7c73a33f0..db7919441 100644 --- a/src/main/php/PHP/PMD/Rule/Design/LongMethod.php +++ b/src/main/php/PHP/PMD/Rule/Design/LongMethod.php @@ -79,7 +79,14 @@ class PHP_PMD_Rule_Design_LongMethod public function apply(PHP_PMD_AbstractNode $node) { $threshold = $this->getIntProperty('minimum'); - $loc = $node->getMetric('loc'); + $ignoreWhitespace = $this->getBooleanProperty('ignore-whitespace'); + + if ($ignoreWhitespace) { + $loc = $node->getMetric('eloc'); + } else { + $loc = $node->getMetric('loc'); + } + if ($loc < $threshold) { return; } diff --git a/src/main/resources/rulesets/codesize.xml b/src/main/resources/rulesets/codesize.xml index 7bf3ce91f..22225e31b 100644 --- a/src/main/resources/rulesets/codesize.xml +++ b/src/main/resources/rulesets/codesize.xml @@ -9,7 +9,7 @@ The Code Size Ruleset contains a collection of rules that find code size related problems. - + 3 + 3 + setReport($report); $rule->addProperty('minimum', '41'); + $rule->addProperty('ignore-whitespace', false); $rule->apply($class); } @@ -101,6 +102,7 @@ public function testRuleAppliesForValueEqualToThreshold() $rule = new PHP_PMD_Rule_Design_LongClass(); $rule->setReport($report); $rule->addProperty('minimum', '42'); + $rule->addProperty('ignore-whitespace', false); $rule->apply($class); } @@ -118,6 +120,24 @@ public function testRuleDoesNotApplyForValueLowerThanThreshold() $rule = new PHP_PMD_Rule_Design_LongClass(); $rule->setReport($report); $rule->addProperty('minimum', '23'); + $rule->addProperty('ignore-whitespace', false); + $rule->apply($class); + } + + /** + * Tests that the rule uses eloc when ignore whitespace is set + * + * @return void + */ + public function testRuleUsesElocWhenIgnoreWhitespaceSet() + { + $class = $this->getClassMock('eloc', 22); + $report = $this->getReportMock(0); + + $rule = new PHP_PMD_Rule_Design_LongClass(); + $rule->setReport($report); + $rule->addProperty('minimum', '23'); + $rule->addProperty('ignore-whitespace', true); $rule->apply($class); } } diff --git a/src/test/php/PHP/PMD/Rule/Design/LongMethodTest.php b/src/test/php/PHP/PMD/Rule/Design/LongMethodTest.php index 867b75176..d3797e5fd 100644 --- a/src/test/php/PHP/PMD/Rule/Design/LongMethodTest.php +++ b/src/test/php/PHP/PMD/Rule/Design/LongMethodTest.php @@ -84,6 +84,7 @@ public function testRuleAppliesForValueGreaterThanThreshold() $rule = new PHP_PMD_Rule_Design_LongMethod(); $rule->setReport($report); $rule->addProperty('minimum', '41'); + $rule->addProperty('ignore-whitespace', false); $rule->apply($method); } @@ -101,6 +102,7 @@ public function testRuleAppliesForValueEqualToThreshold() $rule = new PHP_PMD_Rule_Design_LongMethod(); $rule->setReport($report); $rule->addProperty('minimum', '42'); + $rule->addProperty('ignore-whitespace', false); $rule->apply($method); } @@ -118,6 +120,24 @@ public function testRuleDoesNotApplyForValueLowerThanThreshold() $rule = new PHP_PMD_Rule_Design_LongMethod(); $rule->setReport($report); $rule->addProperty('minimum', '23'); + $rule->addProperty('ignore-whitespace', false); $rule->apply($method); } + + /** + * Tests that the rule uses eloc when ignore whitespace is set + * + * @return void + */ + public function testRuleUsesElocWhenIgnoreWhitespaceSet() + { + $class = $this->getClassMock('eloc', 22); + $report = $this->getReportMock(0); + + $rule = new PHP_PMD_Rule_Design_LongMethod(); + $rule->setReport($report); + $rule->addProperty('minimum', '23'); + $rule->addProperty('ignore-whitespace', true); + $rule->apply($class); + } }