| 1 |
1
|
mapi |
<?php |
|
| 2 |
↓
|
mapi |
/** |
|
| 3 |
↓
|
mapi |
* This file is part of PHP_PMD. |
|
| 4 |
↓
|
mapi |
* |
|
| 5 |
↓
|
mapi |
* PHP Version 5 |
|
| 6 |
↓
|
mapi |
* |
|
| 7 |
174
|
mapi |
* Copyright (c) 2009-2010, Manuel Pichler <mapi@phpmd.org>. |
|
| 8 |
1
|
mapi |
* All rights reserved. |
|
| 9 |
↓
|
mapi |
* |
|
| 10 |
↓
|
mapi |
* Redistribution and use in source and binary forms, with or without |
|
| 11 |
↓
|
mapi |
* modification, are permitted provided that the following conditions |
|
| 12 |
↓
|
mapi |
* are met: |
|
| 13 |
↓
|
mapi |
* |
|
| 14 |
↓
|
mapi |
* * Redistributions of source code must retain the above copyright |
|
| 15 |
↓
|
mapi |
* notice, this list of conditions and the following disclaimer. |
|
| 16 |
↓
|
mapi |
* |
|
| 17 |
↓
|
mapi |
* * Redistributions in binary form must reproduce the above copyright |
|
| 18 |
↓
|
mapi |
* notice, this list of conditions and the following disclaimer in |
|
| 19 |
↓
|
mapi |
* the documentation and/or other materials provided with the |
|
| 20 |
↓
|
mapi |
* distribution. |
|
| 21 |
↓
|
mapi |
* |
|
| 22 |
↓
|
mapi |
* * Neither the name of Manuel Pichler nor the names of his |
|
| 23 |
↓
|
mapi |
* contributors may be used to endorse or promote products derived |
|
| 24 |
↓
|
mapi |
* from this software without specific prior written permission. |
|
| 25 |
↓
|
mapi |
* |
|
| 26 |
↓
|
mapi |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
| 27 |
↓
|
mapi |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
| 28 |
↓
|
mapi |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
|
| 29 |
↓
|
mapi |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
|
| 30 |
↓
|
mapi |
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
|
| 31 |
↓
|
mapi |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
|
| 32 |
↓
|
mapi |
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|
| 33 |
↓
|
mapi |
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
| 34 |
↓
|
mapi |
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
| 35 |
↓
|
mapi |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
|
| 36 |
↓
|
mapi |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
| 37 |
↓
|
mapi |
* POSSIBILITY OF SUCH DAMAGE. |
|
| 38 |
↓
|
mapi |
* |
|
| 39 |
↓
|
mapi |
* @category PHP |
|
| 40 |
↓
|
mapi |
* @package PHP_PMD |
|
| 41 |
98
|
mapi |
* @subpackage Rule_Design |
|
| 42 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 43 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 44 |
1
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 45 |
↓
|
mapi |
* @version SVN: $Id$ |
|
| 46 |
174
|
mapi |
* @link http://phpmd.org |
|
| 47 |
1
|
mapi |
*/ |
|
| 48 |
↓
|
mapi |
|
|
| 49 |
98
|
mapi |
require_once 'PHP/PMD/AbstractRule.php'; |
|
| 50 |
↓
|
mapi |
require_once 'PHP/PMD/Rule/IClassAware.php'; |
|
| 51 |
1
|
mapi |
|
|
| 52 |
↓
|
mapi |
/** |
|
| 53 |
98
|
mapi |
* This rule class will detect all classes with too much methods. |
|
| 54 |
1
|
mapi |
* |
|
| 55 |
↓
|
mapi |
* @category PHP |
|
| 56 |
↓
|
mapi |
* @package PHP_PMD |
|
| 57 |
98
|
mapi |
* @subpackage Rule_Design |
|
| 58 |
174
|
mapi |
* @author Manuel Pichler <mapi@phpmd.org> |
|
| 59 |
150
|
mapi |
* @copyright 2009-2010 Manuel Pichler. All rights reserved. |
|
| 60 |
1
|
mapi |
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
|
| 61 |
↓
|
mapi |
* @version Release: @package_version@ |
|
| 62 |
174
|
mapi |
* @link http://phpmd.org |
|
| 63 |
1
|
mapi |
*/ |
|
| 64 |
98
|
mapi |
class PHP_PMD_Rule_Design_TooManyMethods |
|
| 65 |
↓
|
mapi |
extends PHP_PMD_AbstractRule |
|
| 66 |
↓
|
mapi |
implements PHP_PMD_Rule_IClassAware |
|
| 67 |
1
|
mapi |
{ |
|
| 68 |
14
|
mapi |
/** |
|
| 69 |
98
|
mapi |
* Regular expression that filter all methods that are ignored by this rule. |
|
| 70 |
36
|
mapi |
* |
|
| 71 |
98
|
mapi |
* @var string |
|
| 72 |
36
|
mapi |
*/ |
|
| 73 |
98
|
mapi |
private $_ignoreRegexp = '(^(set|get))i'; |
|
| 74 |
1
|
mapi |
|
|
| 75 |
14
|
mapi |
/** |
|
| 76 |
98
|
mapi |
* This method checks the number of methods with in a given class and checks |
|
| 77 |
↓
|
mapi |
* this number against a configured threshold. |
|
| 78 |
14
|
mapi |
* |
|
| 79 |
98
|
mapi |
* @param PHP_PMD_AbstractNode $node The context source code node. |
|
| 80 |
14
|
mapi |
* |
|
| 81 |
98
|
mapi |
* @return void |
|
| 82 |
14
|
mapi |
*/ |
|
| 83 |
98
|
mapi |
public function apply(PHP_PMD_AbstractNode $node) |
|
| 84 |
14
|
mapi |
{ |
|
| 85 |
100
|
mapi |
if ($node->getMetric('nom') <= $this->getIntProperty('maxmethods')) { |
|
| 86 |
98
|
mapi |
return; |
|
| 87 |
↓
|
mapi |
} |
|
| 88 |
100
|
mapi |
if ($this->_countMethods($node) <= $this->getIntProperty('maxmethods')) { |
|
| 89 |
98
|
mapi |
return; |
|
| 90 |
↓
|
mapi |
} |
|
| 91 |
↓
|
mapi |
$this->addViolation($node); |
|
| 92 |
14
|
mapi |
} |
|
| 93 |
↓
|
mapi |
|
|
| 94 |
↓
|
mapi |
/** |
|
| 95 |
98
|
mapi |
* Counts all methods within the given class/interface node. |
|
| 96 |
14
|
mapi |
* |
|
| 97 |
200
|
mapi |
* @param PHP_PMD_Node_AbstractType $node The context class node. |
|
| 98 |
17
|
mapi |
* |
|
| 99 |
98
|
mapi |
* @return integer |
|
| 100 |
17
|
mapi |
*/ |
|
| 101 |
200
|
mapi |
private function _countMethods(PHP_PMD_Node_AbstractType $node) |
|
| 102 |
17
|
mapi |
{ |
|
| 103 |
98
|
mapi |
$count = 0; |
|
| 104 |
↓
|
mapi |
foreach ($node->getMethodNames() as $name) { |
|
| 105 |
↓
|
mapi |
if (preg_match($this->_ignoreRegexp, $name) === 0) { |
|
| 106 |
↓
|
mapi |
++$count; |
|
| 107 |
↓
|
mapi |
} |
|
| 108 |
↓
|
mapi |
} |
|
| 109 |
↓
|
mapi |
return $count; |
|
| 110 |
17
|
mapi |
} |
|
| 111 |
73
|
mapi |
} |
|