| Conditions | 34 |
| Paths | 35 |
| Total Lines | 131 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 200 | public function processNonString(\PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens) |
||
| 201 | { |
||
| 202 | $nextNonEmpty = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr + 1), null, true); |
||
| 203 | if ($nextNonEmpty === false) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | /* |
||
| 208 | * Deal with anonymous classes - `class` before a reserved keyword is sometimes |
||
| 209 | * misidentified as `T_ANON_CLASS`. |
||
| 210 | * In PHPCS < 2.3.4 these were tokenized as T_CLASS no matter what. |
||
| 211 | */ |
||
| 212 | if ($tokens[$stackPtr]['type'] === 'T_ANON_CLASS' || $tokens[$stackPtr]['type'] === 'T_CLASS') { |
||
| 213 | $prevNonEmpty = $phpcsFile->findPrevious(\PHP_CodeSniffer_Tokens::$emptyTokens, ($stackPtr - 1), null, true); |
||
| 214 | if ($prevNonEmpty !== false && $tokens[$prevNonEmpty]['type'] === 'T_NEW') { |
||
| 215 | return; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /* |
||
| 220 | * PHP 5.6 allows for use const and use function, but only if followed by the function/constant name. |
||
| 221 | * - `use function HelloWorld` => move to the next token (HelloWorld) to verify. |
||
| 222 | * - `use const HelloWorld` => move to the next token (HelloWorld) to verify. |
||
| 223 | */ |
||
| 224 | elseif ($tokens[$stackPtr]['type'] === 'T_USE' |
||
| 225 | && isset($this->validUseNames[strtolower($tokens[$nextNonEmpty]['content'])]) === true |
||
| 226 | ) { |
||
| 227 | $maybeUseNext = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
| 228 | if ($maybeUseNext !== false && $this->isEndOfUseStatement($tokens[$maybeUseNext]) === false) { |
||
| 229 | // Prevent duplicate messages: `const` is T_CONST in PHPCS 1.x and T_STRING in PHPCS 2.x. |
||
| 230 | if ($this->isLowPHPCS === true) { |
||
| 231 | return; |
||
| 232 | } |
||
| 233 | $nextNonEmpty = $maybeUseNext; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /* |
||
| 238 | * Deal with visibility modifiers. |
||
| 239 | * - `use HelloWorld { sayHello as protected; }` => valid, bow out. |
||
| 240 | * - `use HelloWorld { sayHello as private myPrivateHello; }` => move to the next token to verify. |
||
| 241 | */ |
||
| 242 | elseif ($tokens[$stackPtr]['type'] === 'T_AS' |
||
| 243 | && isset($this->allowedModifiers[$tokens[$nextNonEmpty]['code']]) === true |
||
| 244 | && $this->inUseScope($phpcsFile, $stackPtr) === true |
||
| 245 | ) { |
||
| 246 | $maybeUseNext = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
| 247 | if ($maybeUseNext === false || $this->isEndOfUseStatement($tokens[$maybeUseNext]) === true) { |
||
| 248 | return; |
||
| 249 | } |
||
| 250 | |||
| 251 | $nextNonEmpty = $maybeUseNext; |
||
| 252 | } |
||
| 253 | |||
| 254 | /* |
||
| 255 | * Deal with functions declared to return by reference. |
||
| 256 | */ |
||
| 257 | elseif ($tokens[$stackPtr]['type'] === 'T_FUNCTION' |
||
| 258 | && $tokens[$nextNonEmpty]['type'] === 'T_BITWISE_AND' |
||
| 259 | ) { |
||
| 260 | $maybeUseNext = $phpcsFile->findNext(\PHP_CodeSniffer_Tokens::$emptyTokens, ($nextNonEmpty + 1), null, true, null, true); |
||
| 261 | if ($maybeUseNext === false) { |
||
| 262 | // Live coding. |
||
| 263 | return; |
||
| 264 | } |
||
| 265 | |||
| 266 | $nextNonEmpty = $maybeUseNext; |
||
| 267 | } |
||
| 268 | |||
| 269 | /* |
||
| 270 | * Deal with nested namespaces. |
||
| 271 | */ |
||
| 272 | elseif ($tokens[$stackPtr]['type'] === 'T_NAMESPACE') { |
||
| 273 | if ($tokens[$stackPtr + 1]['code'] === T_NS_SEPARATOR) { |
||
| 274 | // Not a namespace declaration, but use of, i.e. namespace\someFunction(); |
||
| 275 | return; |
||
| 276 | } |
||
| 277 | |||
| 278 | $endToken = $phpcsFile->findNext(array(T_SEMICOLON, T_OPEN_CURLY_BRACKET), ($stackPtr + 1), null, false, null, true); |
||
| 279 | $namespaceName = trim($phpcsFile->getTokensAsString(($stackPtr + 1), ($endToken - $stackPtr - 1))); |
||
| 280 | if (empty($namespaceName) === true) { |
||
| 281 | return; |
||
| 282 | } |
||
| 283 | |||
| 284 | $namespaceParts = explode('\\', $namespaceName); |
||
| 285 | foreach ($namespaceParts as $namespacePart) { |
||
| 286 | $partLc = strtolower($namespacePart); |
||
| 287 | if (isset($this->invalidNames[$partLc]) === false) { |
||
| 288 | continue; |
||
| 289 | } |
||
| 290 | |||
| 291 | // Find the token position of the part which matched. |
||
| 292 | for ($i = ($stackPtr + 1); $i < $endToken; $i++) { |
||
| 293 | if ($tokens[$i]['content'] === $namespacePart) { |
||
| 294 | $nextNonEmpty = $i; |
||
| 295 | break; |
||
| 296 | } |
||
| 297 | } |
||
| 298 | } |
||
| 299 | unset($i, $namespacePart, $partLc); |
||
| 300 | } |
||
| 301 | |||
| 302 | $nextContentLc = strtolower($tokens[$nextNonEmpty]['content']); |
||
| 303 | if (isset($this->invalidNames[$nextContentLc]) === false) { |
||
| 304 | return; |
||
| 305 | } |
||
| 306 | |||
| 307 | /* |
||
| 308 | * Deal with PHP 7 relaxing the rules. |
||
| 309 | * "As of PHP 7.0.0 these keywords are allowed as property, constant, and method names |
||
| 310 | * of classes, interfaces and traits, except that class may not be used as constant name." |
||
| 311 | */ |
||
| 312 | if ((($tokens[$stackPtr]['type'] === 'T_FUNCTION' |
||
| 313 | && $this->inClassScope($phpcsFile, $stackPtr, false) === true) |
||
| 314 | || ($tokens[$stackPtr]['type'] === 'T_CONST' |
||
| 315 | && $this->isClassConstant($phpcsFile, $stackPtr) === true |
||
| 316 | && $nextContentLc !== 'class')) |
||
| 317 | && $this->supportsBelow('5.6') === false |
||
| 318 | ) { |
||
| 319 | return; |
||
| 320 | } |
||
| 321 | |||
| 322 | if ($this->supportsAbove($this->invalidNames[$nextContentLc])) { |
||
| 323 | $data = array( |
||
| 324 | $tokens[$nextNonEmpty]['content'], |
||
| 325 | $this->invalidNames[$nextContentLc], |
||
| 326 | ); |
||
| 327 | $this->addError($phpcsFile, $stackPtr, $tokens[$nextNonEmpty]['content'], $data); |
||
| 328 | } |
||
| 329 | |||
| 330 | }//end processNonString() |
||
| 331 | |||
| 414 |