| Conditions | 11 |
| Paths | 7 |
| Total Lines | 52 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 44 | public function process(File $phpcsFile, $stackPtr) |
||
| 45 | { |
||
| 46 | $tokens = $phpcsFile->getTokens(); |
||
| 47 | |||
| 48 | // This sniff intentionally doesn't care about whitespace at the end of the file |
||
| 49 | if (!isset($tokens[$stackPtr + 3]) |
||
| 50 | || $tokens[$stackPtr + 2]['line'] === $tokens[$stackPtr + 3]['line'] |
||
| 51 | ) { |
||
| 52 | return $stackPtr + 3; |
||
| 53 | } |
||
| 54 | |||
| 55 | if ($tokens[$stackPtr + 1]['line'] === $tokens[$stackPtr + 2]['line']) { |
||
| 56 | return $stackPtr + 2; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Finally, check the assumption the current token is or ends with a newline |
||
| 60 | if ($tokens[$stackPtr]['line'] === $tokens[$stackPtr + 1]['line']) { |
||
| 61 | return; |
||
| 62 | } |
||
| 63 | |||
| 64 | // Search for the next non-newline token |
||
| 65 | $next = $stackPtr + 1; |
||
| 66 | |||
| 67 | while (isset($tokens[$next + 1]) && |
||
| 68 | $tokens[$next]['code'] === T_WHITESPACE && |
||
| 69 | $tokens[$next]['line'] !== $tokens[$next + 1]['line'] |
||
| 70 | ) { |
||
| 71 | $next++; |
||
| 72 | } |
||
| 73 | |||
| 74 | $count = $next - $stackPtr - 1; |
||
| 75 | |||
| 76 | if ($count > 1 |
||
| 77 | && $phpcsFile->addFixableError( |
||
| 78 | 'Multiple empty lines should not exist in a row; found %s consecutive empty lines', |
||
| 79 | $stackPtr + 1, |
||
| 80 | 'MultipleEmptyLines', |
||
| 81 | [$count] |
||
| 82 | ) |
||
| 83 | ) { |
||
| 84 | $phpcsFile->fixer->beginChangeset(); |
||
| 85 | |||
| 86 | // Remove all newlines except the first two, i.e. keep one empty line |
||
| 87 | for ($i = $stackPtr + 2; $i < $next; $i++) { |
||
| 88 | $phpcsFile->fixer->replaceToken($i, ''); |
||
| 89 | } |
||
| 90 | |||
| 91 | $phpcsFile->fixer->endChangeset(); |
||
| 92 | } |
||
| 93 | |||
| 94 | // Don't check the current sequence a second time |
||
| 95 | return $next; |
||
| 96 | } |
||
| 98 |