| Conditions | 20 |
| Paths | 2436 |
| Total Lines | 108 |
| 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 |
||
| 40 | public function process(File $phpcsFile, $stackPtr) |
||
| 41 | { |
||
| 42 | $tokens = $phpcsFile->getTokens(); |
||
| 43 | |||
| 44 | $openBracket = null; |
||
| 45 | $closeBracket = null; |
||
| 46 | if (isset($tokens[$stackPtr]['nested_parenthesis']) === true) { |
||
| 47 | $parens = $tokens[$stackPtr]['nested_parenthesis']; |
||
| 48 | $openBracket = array_pop($parens); |
||
| 49 | $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
||
| 50 | } |
||
| 51 | |||
| 52 | // Find the beginning of the statement. If we don't find a |
||
| 53 | // semicolon (end of statement) or comma (end of array value) |
||
| 54 | // then assume the content before the closing parenthesis is the end. |
||
| 55 | $else = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1)); |
||
| 56 | $statementEnd = $phpcsFile->findNext([T_SEMICOLON, T_COMMA], ($else + 1), $closeBracket); |
||
| 57 | if ($statementEnd === false) { |
||
| 58 | $statementEnd = $phpcsFile->findPrevious(T_WHITESPACE, ($closeBracket - 1), null, true); |
||
| 59 | } |
||
| 60 | |||
| 61 | // Make sure it's all on the same line. |
||
| 62 | if ($tokens[$statementEnd]['line'] !== $tokens[$stackPtr]['line']) { |
||
| 63 | $error = 'Inline shorthand IF statement must be declared on a single line'; |
||
| 64 | $phpcsFile->addError($error, $stackPtr, 'NotSingleLine'); |
||
| 65 | return; |
||
| 66 | } |
||
| 67 | |||
| 68 | // Make sure there are spaces around the question mark. |
||
| 69 | $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
||
| 70 | $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
||
| 71 | if ($tokens[$contentBefore]['code'] !== T_CLOSE_PARENTHESIS) { |
||
| 72 | $error = 'Inline shorthand IF statement requires brackets around comparison'; |
||
| 73 | $phpcsFile->addError($error, $stackPtr, 'NoBrackets'); |
||
| 74 | } |
||
| 75 | |||
| 76 | $spaceBefore = ($tokens[$stackPtr]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
||
| 77 | if ($spaceBefore !== 1) { |
||
| 78 | $error = 'Inline shorthand IF statement requires 1 space before THEN; %s found'; |
||
| 79 | $data = [$spaceBefore]; |
||
| 80 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingBeforeThen', $data); |
||
| 81 | if ($fix === true) { |
||
| 82 | if ($spaceBefore === 0) { |
||
| 83 | $phpcsFile->fixer->addContentBefore($stackPtr, ' '); |
||
| 84 | } else { |
||
| 85 | $phpcsFile->fixer->replaceToken(($stackPtr - 1), ' '); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | // If there is no content between the ? and the : operators, then they are |
||
| 91 | // trying to replicate an elvis operator, even though PHP doesn't have one. |
||
| 92 | // In this case, we want no spaces between the two operators so ?: looks like |
||
| 93 | // an operator itself. |
||
| 94 | $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
||
| 95 | if ($tokens[$next]['code'] === T_INLINE_ELSE) { |
||
| 96 | $inlineElse = $next; |
||
| 97 | if ($inlineElse !== ($stackPtr + 1)) { |
||
| 98 | $error = 'Inline shorthand IF statement without THEN statement requires 0 spaces between THEN and ELSE'; |
||
| 99 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'ElvisSpacing'); |
||
| 100 | if ($fix === true) { |
||
| 101 | $phpcsFile->fixer->replaceToken(($stackPtr + 1), ''); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$stackPtr]['column'] + 1)); |
||
| 106 | if ($spaceAfter !== 1) { |
||
| 107 | $error = 'Inline shorthand IF statement requires 1 space after THEN; %s found'; |
||
| 108 | $data = [$spaceAfter]; |
||
| 109 | $fix = $phpcsFile->addFixableError($error, $stackPtr, 'SpacingAfterThen', $data); |
||
| 110 | if ($fix === true) { |
||
| 111 | if ($spaceAfter === 0) { |
||
| 112 | $phpcsFile->fixer->addContent($stackPtr, ' '); |
||
| 113 | } else { |
||
| 114 | $phpcsFile->fixer->replaceToken(($stackPtr + 1), ' '); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | // Make sure the ELSE has the correct spacing. |
||
| 120 | $inlineElse = $phpcsFile->findNext(T_INLINE_ELSE, ($stackPtr + 1), $statementEnd, false); |
||
| 121 | $contentBefore = $phpcsFile->findPrevious(T_WHITESPACE, ($inlineElse - 1), null, true); |
||
| 122 | $spaceBefore = ($tokens[$inlineElse]['column'] - ($tokens[$contentBefore]['column'] + $tokens[$contentBefore]['length'])); |
||
| 123 | if ($spaceBefore !== 1) { |
||
| 124 | $error = 'Inline shorthand IF statement requires 1 space before ELSE; %s found'; |
||
| 125 | $data = [$spaceBefore]; |
||
| 126 | $fix = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingBeforeElse', $data); |
||
| 127 | if ($fix === true) { |
||
| 128 | if ($spaceBefore === 0) { |
||
| 129 | $phpcsFile->fixer->addContentBefore($inlineElse, ' '); |
||
| 130 | } else { |
||
| 131 | $phpcsFile->fixer->replaceToken(($inlineElse - 1), ' '); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | }//end if |
||
| 136 | |||
| 137 | $contentAfter = $phpcsFile->findNext(T_WHITESPACE, ($inlineElse + 1), null, true); |
||
| 138 | $spaceAfter = (($tokens[$contentAfter]['column']) - ($tokens[$inlineElse]['column'] + 1)); |
||
| 139 | if ($spaceAfter !== 1) { |
||
| 140 | $error = 'Inline shorthand IF statement requires 1 space after ELSE; %s found'; |
||
| 141 | $data = [$spaceAfter]; |
||
| 142 | $fix = $phpcsFile->addFixableError($error, $inlineElse, 'SpacingAfterElse', $data); |
||
| 143 | if ($fix === true) { |
||
| 144 | if ($spaceAfter === 0) { |
||
| 145 | $phpcsFile->fixer->addContent($inlineElse, ' '); |
||
| 146 | } else { |
||
| 147 | $phpcsFile->fixer->replaceToken(($inlineElse + 1), ' '); |
||
| 148 | } |
||
| 156 |