Skip to content

Commit

Permalink
Add TooManyPublicMethods rule
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenDeDauw committed Jun 2, 2015
1 parent 5eeb5a4 commit b862f1f
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 18 deletions.
16 changes: 2 additions & 14 deletions src/main/php/PHPMD/Rule/Design/TooManyMethods.php
Expand Up @@ -56,15 +56,7 @@
class TooManyMethods extends AbstractRule implements ClassAware
{
/**
* 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.
* Regular expression that filters all methods that are ignored by this rule.
*
* @var string
*/
Expand All @@ -79,11 +71,7 @@ class TooManyMethods extends AbstractRule implements ClassAware
*/
public function apply(AbstractNode $node)
{
try {
$this->ignoreRegexp = $this->getStringProperty('ignorepattern');
} catch (\OutOfBoundsException $e) {
$this->ignoreRegexp = self::DEFAULT_IGNORE_REGEXP;
}
$this->ignoreRegexp = $this->getStringProperty('ignorepattern');

$threshold = $this->getIntProperty('maxmethods');
if ($node->getMetric('nom') <= $threshold) {
Expand Down
111 changes: 111 additions & 0 deletions src/main/php/PHPMD/Rule/Design/TooManyPublicMethods.php
@@ -0,0 +1,111 @@
<?php
/**
* This file is part of PHP Mess Detector.
*
* Copyright (c) 2008-2012, Manuel Pichler <mapi@phpmd.org>.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Manuel Pichler nor the names of his
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright 2008-2014 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/

namespace PHPMD\Rule\Design;

use PHPMD\AbstractNode;
use PHPMD\AbstractRule;
use PHPMD\Node\AbstractTypeNode;
use PHPMD\Rule\ClassAware;

/**
* This rule class will detect all classes with too much public methods.
*
* @author Manuel Pichler <mapi@phpmd.org>
* @copyright 2008-2014 Manuel Pichler. All rights reserved.
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
*/
class TooManyPublicMethods extends AbstractRule implements ClassAware
{
/**
* Regular expression that filters all methods that are ignored by this rule.
*
* @var string
*/
private $ignoreRegexp;

/**
* This method checks the number of public methods with in a given class and checks
* this number against a configured threshold.
*
* @param \PHPMD\AbstractNode $node
* @return void
*/
public function apply(AbstractNode $node)
{
$this->ignoreRegexp = $this->getStringProperty('ignorepattern');

$threshold = $this->getIntProperty('maxmethods');
if ($node->getMetric('npm') <= $threshold) {
return;
}
$nom = $this->countMethods($node);
if ($nom <= $threshold) {
return;
}
$this->addViolation(
$node,
array(
$node->getType(),
$node->getName(),
$nom,
$threshold
)
);
}

/**
* Counts public methods within the given class/interface node.
*
* @param \PHPMD\Node\AbstractTypeNode $node
* @return integer
*/
private function countMethods(AbstractTypeNode $node)
{
$count = 0;
foreach ($node->getMethods() as $method) {
if ($method->getNode()->isPublic() && preg_match($this->ignoreRegexp, $method->getName()) === 0) {
++$count;
}
}
return $count;
}
}
27 changes: 26 additions & 1 deletion src/main/resources/rulesets/codesize.xml
Expand Up @@ -342,11 +342,36 @@ public class Foo extends Bar {
<![CDATA[
A class with too many methods is probably a good suspect for refactoring, in
order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
The default was changed from 10 to 25 in PHPMD 2.3.
]]>
</description>
<priority>3</priority>
<properties>
<property name="maxmethods" description="The method count reporting threshold" value="25"/>
<property name="ignorepattern" description="Ignore methods matching this regex" value="(^(set|get))i"/>
</properties>
</rule>

<rule name="TooManyPublicMethods"
since="0.1"
class="PHPMD\Rule\Design\TooManyPublicMethods"
message="The {0} {1} has {2} public methods. Consider refactoring {1} to keep number of public methods under {3}."
externalInfoUrl="http://phpmd.org/rules/codesize.html#toomanypublicmethods">
<description>
<![CDATA[
A class with too many public methods is probably a good suspect for refactoring, in
order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.
]]>
</description>
<priority>3</priority>
<properties>
<property name="maxmethods" description="The method count reporting threshold " value="10"/>
<property name="maxmethods" description="The method count reporting threshold" value="10"/>
<property name="ignorepattern" description="Ignore methods matching this regex" value="(^(set|get))i"/>
</properties>
</rule>

Expand Down
29 changes: 26 additions & 3 deletions src/site/rst/rules/codesize.rst
Expand Up @@ -223,14 +223,37 @@ TooManyMethods

Since: PHPMD 0.1

A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to have more fine grained objects.
A class with too many methods is probably a good suspect for refactoring,
in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.

The default was changed from 10 to 25 in PHPMD 2.3.

This rule has the following properties:

=================================== =============== =======================================
Name Default Value Description
=================================== =============== =======================================
maxmethods 25 The method count reporting threshold
ignorepattern (^(set|get))i Ignore methods matching this regex
=================================== =============== =======================================

TooManyPublicMethods
====================

Since: PHPMD 2.3

A class with too many public methods is probably a good suspect for refactoring,
in order to reduce its complexity and find a way to have more fine grained objects.
By default it ignores methods starting with 'get' or 'set'.

This rule has the following properties:

=================================== =============== =======================================
Name Default Value Description
Name Default Value Description
=================================== =============== =======================================
maxmethods 10 The method count reporting threshold
maxmethods 10 The method count reporting threshold
ignorepattern (^(set|get))i Ignore methods matching this regex
=================================== =============== =======================================

ExcessiveClassComplexity
Expand Down
6 changes: 6 additions & 0 deletions src/test/php/PHPMD/Rule/Design/TooManyMethodsTest.php
Expand Up @@ -68,6 +68,7 @@ public function testRuleDoesNotApplyToClassesWithLessMethodsThanThreshold()
$rule = new TooManyMethods();
$rule->setReport($this->getReportMock(0));
$rule->addProperty('maxmethods', '42');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->createClassMock(23));
}

Expand All @@ -81,6 +82,7 @@ public function testRuleDoesNotApplyToClassesWithSameNumberOfMethodsAsThreshold(
$rule = new TooManyMethods();
$rule->setReport($this->getReportMock(0));
$rule->addProperty('maxmethods', '42');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->createClassMock(42));
}

Expand All @@ -94,6 +96,7 @@ public function testRuleAppliesToClassesWithMoreMethodsThanThreshold()
$rule = new TooManyMethods();
$rule->setReport($this->getReportMock(1));
$rule->addProperty('maxmethods', '23');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->createClassMock(42, array_fill(0, 42, __FUNCTION__)));
}

Expand All @@ -107,6 +110,7 @@ public function testRuleIgnoresGetterMethodsInTest()
$rule = new TooManyMethods();
$rule->setReport($this->getReportMock(0));
$rule->addProperty('maxmethods', '1');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->createClassMock(2, array('invoke', 'getClass')));
}

Expand All @@ -120,6 +124,7 @@ public function testRuleIgnoresSetterMethodsInTest()
$rule = new TooManyMethods();
$rule->setReport($this->getReportMock(0));
$rule->addProperty('maxmethods', '1');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->createClassMock(2, array('invoke', 'setClass')));
}

Expand Down Expand Up @@ -147,6 +152,7 @@ public function testRuleIgnoresGetterAndSetterMethodsInTest()
$rule = new TooManyMethods();
$rule->setReport($this->getReportMock(0));
$rule->addProperty('maxmethods', '2');
$rule->addProperty('ignorepattern', '(^(set|get|inject))i');
$rule->apply($this->createClassMock(3, array('invoke', 'getClass', 'setClass')));
}

Expand Down

0 comments on commit b862f1f

Please sign in to comment.