| Conditions | 36 |
| Paths | 355 |
| Total Lines | 158 |
| Code Lines | 101 |
| Lines | 27 |
| Ratio | 17.09 % |
| 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 |
||
| 275 | public static function getMethodParameters(\PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 276 | { |
||
| 277 | if (version_compare(self::getVersion(), '2.7.1', '>') === true) { |
||
| 278 | return $phpcsFile->getMethodParameters($stackPtr); |
||
| 279 | } |
||
| 280 | |||
| 281 | $tokens = $phpcsFile->getTokens(); |
||
| 282 | |||
| 283 | // Check for the existence of the token. |
||
| 284 | if (isset($tokens[$stackPtr]) === false) { |
||
| 285 | return false; |
||
| 286 | } |
||
| 287 | |||
| 288 | View Code Duplication | if ($tokens[$stackPtr]['code'] !== T_FUNCTION && $tokens[$stackPtr]['code'] !== T_CLOSURE) { |
|
| 289 | throw new \PHP_CodeSniffer_Exception('$stackPtr must be of type T_FUNCTION or T_CLOSURE'); |
||
| 290 | } |
||
| 291 | |||
| 292 | $opener = $tokens[$stackPtr]['parenthesis_opener']; |
||
| 293 | $closer = $tokens[$stackPtr]['parenthesis_closer']; |
||
| 294 | |||
| 295 | $vars = array(); |
||
| 296 | $currVar = null; |
||
| 297 | $paramStart = ($opener + 1); |
||
| 298 | $defaultStart = null; |
||
| 299 | $paramCount = 0; |
||
| 300 | $passByReference = false; |
||
| 301 | $variableLength = false; |
||
| 302 | $typeHint = ''; |
||
| 303 | $nullableType = false; |
||
| 304 | |||
| 305 | for ($i = $paramStart; $i <= $closer; $i++) { |
||
| 306 | // Check to see if this token has a parenthesis or bracket opener. If it does |
||
| 307 | // it's likely to be an array which might have arguments in it. This |
||
| 308 | // could cause problems in our parsing below, so lets just skip to the |
||
| 309 | // end of it. |
||
| 310 | View Code Duplication | if (isset($tokens[$i]['parenthesis_opener']) === true) { |
|
| 311 | // Don't do this if it's the close parenthesis for the method. |
||
| 312 | if ($i !== $tokens[$i]['parenthesis_closer']) { |
||
| 313 | $i = ($tokens[$i]['parenthesis_closer'] + 1); |
||
| 314 | } |
||
| 315 | } |
||
| 316 | |||
| 317 | View Code Duplication | if (isset($tokens[$i]['bracket_opener']) === true) { |
|
| 318 | // Don't do this if it's the close parenthesis for the method. |
||
| 319 | if ($i !== $tokens[$i]['bracket_closer']) { |
||
| 320 | $i = ($tokens[$i]['bracket_closer'] + 1); |
||
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | switch ($tokens[$i]['type']) { |
||
| 325 | case 'T_BITWISE_AND': |
||
| 326 | $passByReference = true; |
||
| 327 | break; |
||
| 328 | case 'T_VARIABLE': |
||
| 329 | $currVar = $i; |
||
| 330 | break; |
||
| 331 | case 'T_ELLIPSIS': |
||
| 332 | $variableLength = true; |
||
| 333 | break; |
||
| 334 | case 'T_ARRAY_HINT': |
||
| 335 | case 'T_CALLABLE': |
||
| 336 | $typeHint .= $tokens[$i]['content']; |
||
| 337 | break; |
||
| 338 | case 'T_SELF': |
||
| 339 | case 'T_PARENT': |
||
| 340 | case 'T_STATIC': |
||
| 341 | // Self and parent are valid, static invalid, but was probably intended as type hint. |
||
| 342 | if (isset($defaultStart) === false) { |
||
| 343 | $typeHint .= $tokens[$i]['content']; |
||
| 344 | } |
||
| 345 | break; |
||
| 346 | case 'T_STRING': |
||
| 347 | // This is a string, so it may be a type hint, but it could |
||
| 348 | // also be a constant used as a default value. |
||
| 349 | $prevComma = false; |
||
| 350 | View Code Duplication | for ($t = $i; $t >= $opener; $t--) { |
|
| 351 | if ($tokens[$t]['code'] === T_COMMA) { |
||
| 352 | $prevComma = $t; |
||
| 353 | break; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | if ($prevComma !== false) { |
||
| 358 | $nextEquals = false; |
||
| 359 | View Code Duplication | for ($t = $prevComma; $t < $i; $t++) { |
|
| 360 | if ($tokens[$t]['code'] === T_EQUAL) { |
||
| 361 | $nextEquals = $t; |
||
| 362 | break; |
||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | if ($nextEquals !== false) { |
||
| 367 | break; |
||
| 368 | } |
||
| 369 | } |
||
| 370 | |||
| 371 | if ($defaultStart === null) { |
||
| 372 | $typeHint .= $tokens[$i]['content']; |
||
| 373 | } |
||
| 374 | break; |
||
| 375 | case 'T_NS_SEPARATOR': |
||
| 376 | // Part of a type hint or default value. |
||
| 377 | if ($defaultStart === null) { |
||
| 378 | $typeHint .= $tokens[$i]['content']; |
||
| 379 | } |
||
| 380 | break; |
||
| 381 | case 'T_INLINE_THEN': |
||
| 382 | if ($defaultStart === null) { |
||
| 383 | $nullableType = true; |
||
| 384 | $typeHint .= $tokens[$i]['content']; |
||
| 385 | } |
||
| 386 | break; |
||
| 387 | case 'T_CLOSE_PARENTHESIS': |
||
| 388 | case 'T_COMMA': |
||
| 389 | // If it's null, then there must be no parameters for this |
||
| 390 | // method. |
||
| 391 | if ($currVar === null) { |
||
| 392 | continue; |
||
| 393 | } |
||
| 394 | |||
| 395 | $vars[$paramCount] = array(); |
||
| 396 | $vars[$paramCount]['token'] = $currVar; |
||
| 397 | $vars[$paramCount]['name'] = $tokens[$currVar]['content']; |
||
| 398 | $vars[$paramCount]['content'] = trim($phpcsFile->getTokensAsString($paramStart, ($i - $paramStart))); |
||
| 399 | |||
| 400 | if ($defaultStart !== null) { |
||
| 401 | $vars[$paramCount]['default'] = trim( |
||
| 402 | $phpcsFile->getTokensAsString( |
||
| 403 | $defaultStart, |
||
| 404 | ($i - $defaultStart) |
||
| 405 | ) |
||
| 406 | ); |
||
| 407 | } |
||
| 408 | |||
| 409 | $vars[$paramCount]['pass_by_reference'] = $passByReference; |
||
| 410 | $vars[$paramCount]['variable_length'] = $variableLength; |
||
| 411 | $vars[$paramCount]['type_hint'] = $typeHint; |
||
| 412 | $vars[$paramCount]['nullable_type'] = $nullableType; |
||
| 413 | |||
| 414 | // Reset the vars, as we are about to process the next parameter. |
||
| 415 | $defaultStart = null; |
||
| 416 | $paramStart = ($i + 1); |
||
| 417 | $passByReference = false; |
||
| 418 | $variableLength = false; |
||
| 419 | $typeHint = ''; |
||
| 420 | $nullableType = false; |
||
| 421 | |||
| 422 | $paramCount++; |
||
| 423 | break; |
||
| 424 | case 'T_EQUAL': |
||
| 425 | $defaultStart = ($i + 1); |
||
| 426 | break; |
||
| 427 | }//end switch |
||
| 428 | }//end for |
||
| 429 | |||
| 430 | return $vars; |
||
| 431 | |||
| 432 | }//end getMethodParameters() |
||
| 433 | } |
||
| 434 |