|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category PHP |
|
8
|
|
|
* @package PHP_CodeSniffer |
|
9
|
|
|
* @author Thomas Ernest <[email protected]> |
|
10
|
|
|
* @copyright 2010 Thomas Ernest |
|
11
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
|
12
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
|
13
|
|
|
*/ |
|
14
|
|
|
/** |
|
15
|
|
|
* CodeIgniter_Sniffs_NamingConventions_ValidMethodNameSniff. |
|
16
|
|
|
* |
|
17
|
|
|
* Ensures that class methods and functions areentirely lowercased and that |
|
18
|
|
|
* words are separated with an underscore, and not CamelCased. |
|
19
|
|
|
* Ensures that private class methods are prefixed with an underscore and that |
|
20
|
|
|
* all other methods are not prefixed with an underscored. |
|
21
|
|
|
* Ensures that names longer than 50 chars are prohibited. Likewise names longer |
|
22
|
|
|
* than 35 chars raise a warning. |
|
23
|
|
|
* |
|
24
|
|
|
* @todo Use a rule property or a configuration variable to allow users to set |
|
25
|
|
|
* their own maximum lengths for function and method names. Have a look at |
|
26
|
|
|
* CodeIgniter_Sniffs_Files_ClosingLocationCommentSniff and application root. |
|
27
|
|
|
* |
|
28
|
|
|
* @category PHP |
|
29
|
|
|
* @package PHP_CodeSniffer |
|
30
|
|
|
* @author Thomas Ernest <[email protected]> |
|
31
|
|
|
* @copyright 2010 Thomas Ernest |
|
32
|
|
|
* @license http://thomas.ernest.fr/developement/php_cs/licence GNU General Public License |
|
33
|
|
|
* @link http://pear.php.net/package/PHP_CodeSniffer |
|
34
|
|
|
*/ |
|
35
|
|
|
|
|
36
|
|
|
namespace CodeIgniter\Sniffs\NamingConventions; |
|
37
|
|
|
|
|
38
|
|
|
use PHP_CodeSniffer\Sniffs\AbstactScopeSniff; |
|
39
|
|
|
use PHP_CodeSniffer\Files\File; |
|
40
|
|
|
|
|
41
|
|
|
class ValidMethodNameSniff extends AbstractScopeSniff |
|
42
|
|
|
{ |
|
43
|
|
|
/** |
|
44
|
|
|
* A list of all PHP magic methods. |
|
45
|
|
|
* |
|
46
|
|
|
* @var array |
|
47
|
|
|
*/ |
|
48
|
|
|
protected static $magicMethods = array( |
|
49
|
|
|
'construct', |
|
50
|
|
|
'destruct', |
|
51
|
|
|
'call', |
|
52
|
|
|
'callStatic', |
|
53
|
|
|
'get', |
|
54
|
|
|
'set', |
|
55
|
|
|
'isset', |
|
56
|
|
|
'unset', |
|
57
|
|
|
'sleep', |
|
58
|
|
|
'wakeup', |
|
59
|
|
|
'toString', |
|
60
|
|
|
'set_state', |
|
61
|
|
|
'clone', |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Defines which token(s) in which scope(s) will be proceed. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function __construct() |
|
68
|
|
|
{ |
|
69
|
|
|
parent::__construct(array(T_CLASS, T_INTERFACE), array(T_FUNCTION), true); |
|
70
|
|
|
|
|
71
|
|
|
}//end __construct() |
|
72
|
|
|
|
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Processes the tokens within the scope. |
|
76
|
|
|
* |
|
77
|
|
|
* @param File $phpcsFile The file being processed. |
|
78
|
|
|
* @param int $stackPtr The position where this token was |
|
79
|
|
|
* found. |
|
80
|
|
|
* @param int $currScope The position of the current scope. |
|
81
|
|
|
* |
|
82
|
|
|
* @return void |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
|
85
|
|
|
{ |
|
86
|
|
|
$methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
87
|
|
|
if ($methodName === null) { |
|
88
|
|
|
// Ignore closures. |
|
89
|
|
|
return; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$className = $phpcsFile->getDeclarationName($currScope); |
|
93
|
|
|
|
|
94
|
|
|
// Is this a magic method i.e. is prefixed with "__". |
|
95
|
|
|
if (0 === strcmp(substr($methodName, 0, 2), '__')) { |
|
96
|
|
|
$magicPart = substr($methodName, 2); |
|
97
|
|
|
if (in_array($magicPart, self::$magicMethods) === false) { |
|
98
|
|
|
$error = "Method name \"$className::$methodName\" is invalid; only PHP magic methods should be prefixed with a double underscore"; |
|
|
|
|
|
|
99
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
// PHP4 constructors are allowed to break our rules. |
|
106
|
|
|
if ($methodName === $className) { |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// PHP4 destructors are allowed to break our rules. |
|
111
|
|
|
if ($methodName === '_'.$className) { |
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
if (0 !== strcmp($methodName, strtolower($methodName))) { |
|
116
|
|
|
$uscrdMethodName = preg_replace('/([A-Z])/', '_${1}', $methodName); |
|
117
|
|
|
$expectedMethodName = strtolower($uscrdMethodName); |
|
118
|
|
|
$error = "Class methods should be entirely lowercased. Please consider \"$expectedMethodName\" instead of \"$methodName\"."; |
|
|
|
|
|
|
119
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
120
|
|
|
return; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
124
|
|
|
$scope = $methodProps['scope']; |
|
125
|
|
|
$scopeSpecified = $methodProps['scope_specified']; |
|
126
|
|
|
|
|
127
|
|
|
// If it's a private method, it must have an underscore on the front. |
|
128
|
|
|
if ($scope === 'private' && $methodName{0} !== '_') { |
|
129
|
|
|
$error = "Private method name \"$className::$methodName\" must be prefixed with an underscore"; |
|
130
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
131
|
|
|
return; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
// If it's not a private method, it must not have an underscore on the front. |
|
135
|
|
View Code Duplication |
if ($scope !== 'private' && $methodName{0} === '_') { |
|
|
|
|
|
|
136
|
|
|
if (true === $scopeSpecified) { |
|
137
|
|
|
$error = "Public method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
138
|
|
|
} else { |
|
139
|
|
|
$error = ucfirst($scope)." method name \"$className::$methodName\" must not be prefixed with an underscore"; |
|
|
|
|
|
|
140
|
|
|
} |
|
141
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
142
|
|
|
return; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
// If name is too verbose, |
|
146
|
|
|
// then either an error or a warning is displayed. |
|
147
|
|
|
$error_limit = 50; |
|
148
|
|
|
$warning_limit = 35; |
|
149
|
|
|
if (strlen($methodName) > $error_limit) { |
|
150
|
|
|
$error = "Overly long and verbose names are prohibited. Please find a name shorter than $error_limit chars."; |
|
|
|
|
|
|
151
|
|
|
$phpcsFile->addError($error, $stackPtr); |
|
|
|
|
|
|
152
|
|
|
return; |
|
153
|
|
|
} else if (strlen($methodName) > $warning_limit) { |
|
154
|
|
|
$warning = "Try to avoid overly long and verbose names in finding a name shorter than $warning_limit chars."; |
|
|
|
|
|
|
155
|
|
|
$phpcsFile->addWarning($warning, $stackPtr); |
|
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
}//end processTokenWithinScope() |
|
158
|
|
|
|
|
159
|
|
|
}//end class |
|
160
|
|
|
|
|
161
|
|
|
?> |
|
|
|
|
|
|
162
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.