1 | <?php |
||
37 | { |
||
38 | |||
39 | protected $exclusions = array( |
||
40 | // Don't match to the end of class name to allow "SomeEventHandlerFeature" format. |
||
41 | '/EventHandler/' => array( |
||
42 | 'SetCustomQuery', |
||
43 | 'CheckPermission', |
||
44 | 'BaseQuery', |
||
45 | 'ListPrepareQuery', |
||
46 | 'ItemPrepareQuery', |
||
47 | 'SetPagination', |
||
48 | 'SetSorting', |
||
49 | ), |
||
50 | // Don't match to the end of class name to allow "SomeTagProcessorFeature" format. |
||
51 | '/TagProcessor/' => array( |
||
52 | 'PrepareListElementParams', |
||
53 | ), |
||
54 | '/Formatter$/' => array( |
||
55 | 'Format', |
||
56 | 'Parse', |
||
57 | 'PrepareOptions', |
||
58 | ), |
||
59 | '/Validator$/' => array( |
||
60 | 'GetErrorMsg', |
||
61 | 'CustomValidation', |
||
62 | 'SetError', |
||
63 | ), |
||
64 | '/ShippingQuoteEngine$/' => array( |
||
65 | 'GetShippingQuotes', |
||
66 | 'GetAvailableTypes', |
||
67 | 'GetEngineFields', |
||
68 | 'MakeOrder', |
||
69 | ), |
||
70 | '/Helper$/' => array( |
||
71 | 'Init', |
||
72 | 'InitHelper', |
||
73 | ), |
||
74 | '/Item$/' => array('*'), |
||
75 | '/List$/' => array('*'), |
||
76 | '/DBConnection/' => array('*'), |
||
77 | '/DBLoadBalancer$/' => array('*'), |
||
78 | '/Application$/' => array('*'), |
||
79 | ); |
||
80 | |||
81 | /** |
||
82 | * Processes the tokens within the scope. |
||
83 | * |
||
84 | * @param File $phpcsFile The file being processed. |
||
85 | * @param int $stackPtr The position where this token was |
||
86 | * found. |
||
87 | * @param int $currScope The position of the current scope. |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | 1 | protected function processTokenWithinScope(File $phpcsFile, $stackPtr, $currScope) |
|
92 | { |
||
93 | 1 | $methodName = $phpcsFile->getDeclarationName($stackPtr); |
|
94 | 1 | if ($methodName === null) { |
|
95 | // Ignore closures. |
||
96 | return; |
||
97 | } |
||
98 | |||
99 | 1 | $className = $phpcsFile->getDeclarationName($currScope); |
|
100 | 1 | $errorData = array($className.'::'.$methodName); |
|
101 | |||
102 | // Is this a magic method. i.e., is prefixed with "__" ? |
||
103 | 1 | if (strpos($methodName, '__') === 0) { |
|
104 | 1 | $magicPart = strtolower(substr($methodName, 2)); |
|
105 | |||
106 | 1 | if (isset($this->magicMethods[$magicPart]) === false) { |
|
107 | 1 | $error = 'Method name "%s" is invalid; only PHP magic methods should be prefixed with a double underscore'; |
|
108 | 1 | $phpcsFile->addError($error, $stackPtr, 'MethodDoubleUnderscore', $errorData); |
|
109 | 1 | } |
|
110 | |||
111 | 1 | return; |
|
112 | } |
||
113 | |||
114 | // PHP4 constructors are allowed to break our rules. |
||
115 | 1 | if ($methodName === $className) { |
|
116 | 1 | return; |
|
117 | } |
||
118 | |||
119 | // PHP4 destructors are allowed to break our rules. |
||
120 | 1 | if ($methodName === '_'.$className) { |
|
121 | 1 | return; |
|
122 | } |
||
123 | |||
124 | 1 | $methodProps = $phpcsFile->getMethodProperties($stackPtr); |
|
125 | 1 | $isPublic = ($methodProps['scope'] === 'private') ? false : true; |
|
126 | 1 | $scope = $methodProps['scope']; |
|
127 | 1 | $scopeSpecified = $methodProps['scope_specified']; |
|
128 | |||
129 | // If it's a private method, it must have an underscore on the front. |
||
130 | 1 | if ($isPublic === false) { |
|
131 | 1 | if ($methodName{0} !== '_') { |
|
132 | 1 | $error = 'Private method name "%s" must be prefixed with an underscore'; |
|
133 | 1 | $phpcsFile->addError($error, $stackPtr, 'PrivateNoUnderscore', $errorData); |
|
134 | |||
135 | 1 | if (isset($phpcsFile->fixer) === true) { |
|
136 | 1 | $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'no'); |
|
137 | 1 | } |
|
138 | |||
139 | 1 | return; |
|
140 | } else { |
||
141 | 1 | if (isset($phpcsFile->fixer) === true) { |
|
142 | 1 | $phpcsFile->recordMetric($stackPtr, 'Private method prefixed with underscore', 'yes'); |
|
143 | 1 | } |
|
144 | } |
||
145 | 1 | } |
|
146 | |||
147 | // If it's not a private method, it must not have an underscore on the front. |
||
148 | 1 | if ($isPublic === true && $scopeSpecified === true && $methodName{0} === '_') { |
|
149 | 1 | $error = '%s method name "%s" must not be prefixed with an underscore'; |
|
150 | $data = array( |
||
151 | 1 | ucfirst($scope), |
|
152 | 1 | $errorData[0], |
|
153 | 1 | ); |
|
154 | 1 | $phpcsFile->addError($error, $stackPtr, 'PublicUnderscore', $data); |
|
155 | 1 | return; |
|
156 | } |
||
157 | |||
158 | // If the scope was specified on the method, then the method must be |
||
159 | // camel caps and an underscore should be checked for. If it wasn't |
||
160 | // specified, treat it like a public method and remove the underscore |
||
161 | // prefix if there is one because we cant determine if it is private or |
||
162 | // public. |
||
163 | 1 | $testMethodName = $methodName; |
|
164 | 1 | if ($scopeSpecified === false && $methodName{0} === '_') { |
|
165 | 1 | $testMethodName = substr($methodName, 1); |
|
166 | 1 | } |
|
167 | |||
168 | 1 | $methodParams = $phpcsFile->getMethodParameters($stackPtr); |
|
169 | |||
170 | 1 | if ($this->isExclusion($className, $methodName, $isPublic) === true |
|
171 | 1 | || $this->isEventHandlerExclusion($className, $methodName, $methodParams) === true |
|
172 | 1 | || $this->isTagProcessorExclusion($className, $methodName, $methodParams) === true |
|
173 | 1 | ) { |
|
174 | 1 | return; |
|
175 | } |
||
176 | |||
177 | 1 | if (Common::isCamelCaps($testMethodName, false, $isPublic, false) === false) { |
|
178 | 1 | if ($scopeSpecified === true) { |
|
179 | 1 | $error = '%s method name "%s" is not in camel caps format'; |
|
180 | $data = array( |
||
181 | 1 | ucfirst($scope), |
|
182 | 1 | $errorData[0], |
|
183 | 1 | ); |
|
184 | 1 | $phpcsFile->addError($error, $stackPtr, 'ScopeNotCamelCaps', $data); |
|
185 | 1 | } else { |
|
186 | 1 | $error = 'Method name "%s" is not in camel caps format'; |
|
187 | 1 | $phpcsFile->addError($error, $stackPtr, 'NotCamelCaps', $errorData); |
|
188 | } |
||
189 | |||
190 | 1 | return; |
|
191 | } |
||
192 | 1 | }//end processTokenWithinScope() |
|
193 | |||
194 | /** |
||
195 | * Determines if a method name shouldn't be checked for camelCaps format. |
||
196 | * |
||
197 | * @param string $className Class name. |
||
198 | * @param string $methodName Method name. |
||
199 | * @param bool $isPublic Public. |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 1 | protected function isExclusion($className, $methodName, $isPublic) |
|
204 | { |
||
205 | 1 | foreach ($this->exclusions as $classRegExp => $excludedMethods) { |
|
206 | 1 | if (preg_match($classRegExp, $className)) { |
|
207 | 1 | return ($excludedMethods[0] == '*' && $isPublic) || in_array($methodName, $excludedMethods); |
|
208 | } |
||
209 | 1 | } |
|
210 | |||
211 | 1 | return false; |
|
212 | }//end isExclusion() |
||
213 | |||
214 | /** |
||
215 | * Determines if a method is an event in the event handler class. |
||
216 | * |
||
217 | * @param string $className Class name. |
||
218 | * @param string $methodName Method name. |
||
219 | * @param array $methodParams Method parameters. |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | 1 | protected function isEventHandlerExclusion($className, $methodName, array $methodParams) |
|
232 | |||
233 | |||
234 | /** |
||
235 | * Determines if a method is an tag in the tag processor class. |
||
236 | * |
||
237 | * @param string $className Class name. |
||
238 | * @param string $methodName Method name. |
||
239 | * @param array $methodParams Method parameters. |
||
240 | * |
||
241 | * @return bool |
||
242 | */ |
||
243 | 1 | protected function isTagProcessorExclusion($className, $methodName, array $methodParams) |
|
252 | }//end class |
||
253 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.