5 |
aurelien |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* Checks the nesting level for methods.
|
|
|
4 |
*
|
|
|
5 |
* PHP version 5
|
|
|
6 |
*
|
|
|
7 |
* @category PHP
|
|
|
8 |
* @package PHP_CodeSniffer
|
|
|
9 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
10 |
* @author Marc McIntyre <mmcintyre@squiz.net>
|
|
|
11 |
* @copyright 2006 Squiz Pty Ltd (ABN 77 084 670 600)
|
|
|
12 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
13 |
* @version CVS: $Id: NestingLevelSniff.php,v 1.1 2007/07/30 04:55:35 squiz Exp $
|
|
|
14 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/**
|
|
|
18 |
* Checks the nesting level for methods.
|
|
|
19 |
*
|
|
|
20 |
* @category PHP
|
|
|
21 |
* @package PHP_CodeSniffer
|
|
|
22 |
* @author Johann-Peter Hartmann <hartmann@mayflower.de>
|
|
|
23 |
* @author Greg Sherwood <gsherwood@squiz.net>
|
|
|
24 |
* @copyright 2007 Mayflower GmbH
|
|
|
25 |
* @license http://matrix.squiz.net/developer/tools/php_cs/licence BSD Licence
|
|
|
26 |
* @version Release: 1.2.0RC1
|
|
|
27 |
* @link http://pear.php.net/package/PHP_CodeSniffer
|
|
|
28 |
*/
|
|
|
29 |
class Generic_Sniffs_Metrics_NestingLevelSniff implements PHP_CodeSniffer_Sniff
|
|
|
30 |
{
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* A nesting level than this value will throw a warning.
|
|
|
34 |
*
|
|
|
35 |
* @var int
|
|
|
36 |
*/
|
|
|
37 |
protected $nestingLevel = 5;
|
|
|
38 |
|
|
|
39 |
/**
|
|
|
40 |
* A nesting level than this value will throw an error.
|
|
|
41 |
*
|
|
|
42 |
* @var int
|
|
|
43 |
*/
|
|
|
44 |
protected $absoluteNestingLevel = 10;
|
|
|
45 |
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Returns an array of tokens this test wants to listen for.
|
|
|
49 |
*
|
|
|
50 |
* @return array
|
|
|
51 |
*/
|
|
|
52 |
public function register()
|
|
|
53 |
{
|
|
|
54 |
return array(T_FUNCTION);
|
|
|
55 |
|
|
|
56 |
}//end register()
|
|
|
57 |
|
|
|
58 |
|
|
|
59 |
/**
|
|
|
60 |
* Processes this test, when one of its tokens is encountered.
|
|
|
61 |
*
|
|
|
62 |
* @param PHP_CodeSniffer_File $phpcsFile The file being scanned.
|
|
|
63 |
* @param int $stackPtr The position of the current token
|
|
|
64 |
* in the stack passed in $tokens.
|
|
|
65 |
*
|
|
|
66 |
* @return void
|
|
|
67 |
*/
|
|
|
68 |
public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr)
|
|
|
69 |
{
|
|
|
70 |
$tokens = $phpcsFile->getTokens();
|
|
|
71 |
|
|
|
72 |
// Ignore abstract methods.
|
|
|
73 |
if (isset($tokens[$stackPtr]['scope_opener']) === false) {
|
|
|
74 |
return;
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
// Detect start and end of this function definition.
|
|
|
78 |
$start = $tokens[$stackPtr]['scope_opener'];
|
|
|
79 |
$end = $tokens[$stackPtr]['scope_closer'];
|
|
|
80 |
|
|
|
81 |
$nestingLevel = 0;
|
|
|
82 |
|
|
|
83 |
// Find the maximum nesting level of any token in the function.
|
|
|
84 |
for ($i = ($start + 1); $i < $end; $i++) {
|
|
|
85 |
$level = $tokens[$i]['level'];
|
|
|
86 |
if ($nestingLevel < $level) {
|
|
|
87 |
$nestingLevel = $level;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
|
|
91 |
// We subtract the nesting level of the function itself.
|
|
|
92 |
$nestingLevel = ($nestingLevel - $tokens[$stackPtr]['level'] - 1);
|
|
|
93 |
|
|
|
94 |
if ($nestingLevel > $this->absoluteNestingLevel) {
|
|
|
95 |
$error = "Function's nesting level ($nestingLevel) exceeds allowed maximum of ".$this->absoluteNestingLevel;
|
|
|
96 |
$phpcsFile->addError($error, $stackPtr);
|
|
|
97 |
} else if ($nestingLevel > $this->nestingLevel) {
|
|
|
98 |
$warning = "Function's nesting level ($nestingLevel) exceeds ".$this->nestingLevel.'; consider refactoring the function';
|
|
|
99 |
$phpcsFile->addWarning($warning, $stackPtr);
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
}//end process()
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
}//end class
|
|
|
106 |
|
|
|
107 |
?>
|