Skip to content

Commit

Permalink
Merge pull request #53 from palbertini/master
Browse files Browse the repository at this point in the history
Added getStringProperty and rule-setting to change TooManyMethods ignore Regexp
  • Loading branch information
manuelpichler committed Sep 7, 2012
2 parents 22b523c + f0a6ee2 commit bc795b6
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/main/php/PHP/PMD/AbstractRule.php
Expand Up @@ -410,6 +410,23 @@ public function getStringProperty($name)

}

/**
* Returns the value of a configured property as a string or throws an
* exception when no property with <b>$name</b> exists.
*
* @param string $name The property identifier.
*
* @return string
* @throws OutOfBoundsException When no property for <b>$name</b> exists.
*/
public function getStringProperty($name)
{
if (isset($this->_properties[$name])) {
return (string) $this->_properties[$name];
}
throw new OutOfBoundsException('Property $' . $name . ' does not exist.');
}

/**
* This method adds a violation to all reports for this violation type and
* for the given <b>$node</b> instance.
Expand Down
23 changes: 18 additions & 5 deletions src/main/php/PHP/PMD/Rule/Design/TooManyMethods.php
Expand Up @@ -65,12 +65,19 @@ class PHP_PMD_Rule_Design_TooManyMethods
extends PHP_PMD_AbstractRule
implements PHP_PMD_Rule_IClassAware
{
/**
* the default regex pattern for ignore method names. override with "ignorepattern" rule setting.
*
* @var string
*/
const DEFAULT_IGNORE_REGEXP = '(^(set|get))i';

/**
* Regular expression that filter all methods that are ignored by this rule.
*
* @var string
*/
private $_ignoreRegexp = '(^(set|get))i';
private $_ignoreRegexp;

/**
* This method checks the number of methods with in a given class and checks
Expand All @@ -82,6 +89,12 @@ class PHP_PMD_Rule_Design_TooManyMethods
*/
public function apply(PHP_PMD_AbstractNode $node)
{
try {
$this->_ignoreRegexp = $this->getStringProperty('ignorepattern');
} catch (OutOfBoundsException $e) {
$this->_ignoreRegexp = self::DEFAULT_IGNORE_REGEXP;
}

$threshold = $this->getIntProperty('maxmethods');
if ($node->getMetric('nom') <= $threshold) {
return;
Expand All @@ -91,11 +104,11 @@ public function apply(PHP_PMD_AbstractNode $node)
return;
}
$this->addViolation(
$node,
$node,
array(
$node->getType(),
$node->getName(),
$nom,
$node->getType(),
$node->getName(),
$nom,
$threshold
)
);
Expand Down
14 changes: 14 additions & 0 deletions src/test/php/PHP/PMD/Rule/Design/TooManyMethodsTest.php
Expand Up @@ -135,6 +135,20 @@ public function testRuleIgnoresSetterMethodsInTest()
$rule->apply($this->_createClassMock(2, array('invoke', 'setClass')));
}

/**
* testRuleIgnoresCustomMethodsWhenRegexPropertyIsGiven
*
* @return void
*/
public function testRuleIgnoresCustomMethodsWhenRegexPropertyIsGiven()
{
$rule = new PHP_PMD_Rule_Design_TooManyMethods();
$rule->setReport($this->getReportMock(0));
$rule->addProperty('maxmethods', '1');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->_createClassMock(2, array('invoke', 'injectClass')));
}

/**
* testRuleIgnoresGetterAndSetterMethodsInTest
*
Expand Down

1 comment on commit bc795b6

@palbertini
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot, very useful @work

Please sign in to comment.