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

Added getStringProperty and rule-setting to change TooManyMethods ignore Regexp #53

Merged
merged 1 commit into from Sep 7, 2012
Merged
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions src/main/php/PHP/PMD/AbstractRule.php
Expand Up @@ -391,6 +391,23 @@ public function getIntProperty($name)
throw new OutOfBoundsException('Property $' . $name . ' does not exist.');
}

/**
* 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