| Conditions | 23 |
| Paths | 1754 |
| Total Lines | 129 |
| Code Lines | 76 |
| Lines | 45 |
| Ratio | 34.88 % |
| 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 |
||
| 62 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 63 | { |
||
| 64 | $tokens = $phpcsFile->getTokens(); |
||
| 65 | |||
| 66 | // If this is an inline condition (ie. there is no scope opener), then |
||
| 67 | // return, as this is not a new scope. |
||
| 68 | if (isset($tokens[$stackPtr]['scope_closer']) === false) { |
||
| 69 | return; |
||
| 70 | } |
||
| 71 | |||
| 72 | $scopeStart = $tokens[$stackPtr]['scope_opener']; |
||
| 73 | $scopeEnd = $tokens[$stackPtr]['scope_closer']; |
||
| 74 | |||
| 75 | // If the scope closer doesn't think it belongs to this scope opener |
||
| 76 | // then the opener is sharing its closer with other tokens. We only |
||
| 77 | // want to process the closer once, so skip this one. |
||
| 78 | if (isset($tokens[$scopeEnd]['scope_condition']) === false |
||
| 79 | || $tokens[$scopeEnd]['scope_condition'] !== $stackPtr |
||
| 80 | ) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | // We need to actually find the first piece of content on this line, |
||
| 85 | // because if this is a method with tokens before it (public, static etc) |
||
| 86 | // or an if with an else before it, then we need to start the scope |
||
| 87 | // checking from there, rather than the current token. |
||
| 88 | $lineStart = ($stackPtr - 1); |
||
| 89 | for ($lineStart; $lineStart > 0; $lineStart--) { |
||
| 90 | if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) { |
||
| 91 | break; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | $lineStart++; |
||
| 96 | |||
| 97 | $startColumn = 1; |
||
| 98 | if ($tokens[$lineStart]['code'] === T_WHITESPACE) { |
||
| 99 | $startColumn = $tokens[($lineStart + 1)]['column']; |
||
| 100 | } else if ($tokens[$lineStart]['code'] === T_INLINE_HTML) { |
||
| 101 | $trimmed = ltrim($tokens[$lineStart]['content']); |
||
| 102 | if ($trimmed === '') { |
||
| 103 | $startColumn = $tokens[($lineStart + 1)]['column']; |
||
| 104 | } else { |
||
| 105 | $startColumn = (strlen($tokens[$lineStart]['content']) - strlen($trimmed)); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | // Check that the closing brace is on it's own line. |
||
| 110 | $lastContent = $phpcsFile->findPrevious( |
||
| 111 | array( |
||
| 112 | T_WHITESPACE, |
||
| 113 | T_INLINE_HTML, |
||
| 114 | T_OPEN_TAG, |
||
| 115 | ), |
||
| 116 | ($scopeEnd - 1), |
||
| 117 | $scopeStart, |
||
| 118 | true |
||
| 119 | ); |
||
| 120 | |||
| 121 | if ($tokens[$lastContent]['line'] === $tokens[$scopeEnd]['line']) { |
||
| 122 | $error = 'Closing brace must be on a line by itself'; |
||
| 123 | $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Line'); |
||
| 124 | if ($fix === true) { |
||
| 125 | $phpcsFile->fixer->addNewlineBefore($scopeEnd); |
||
| 126 | } |
||
| 127 | |||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | // Check now that the closing brace is lined up correctly. |
||
| 132 | $lineStart = ($scopeEnd - 1); |
||
| 133 | for ($lineStart; $lineStart > 0; $lineStart--) { |
||
| 134 | if (strpos($tokens[$lineStart]['content'], $phpcsFile->eolChar) !== false) { |
||
| 135 | break; |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $lineStart++; |
||
| 140 | |||
| 141 | $braceIndent = 0; |
||
| 142 | if ($tokens[$lineStart]['code'] === T_WHITESPACE) { |
||
| 143 | $braceIndent = ($tokens[($lineStart + 1)]['column'] - 1); |
||
| 144 | } else if ($tokens[$lineStart]['code'] === T_INLINE_HTML) { |
||
| 145 | $trimmed = ltrim($tokens[$lineStart]['content']); |
||
| 146 | if ($trimmed === '') { |
||
| 147 | $braceIndent = ($tokens[($lineStart + 1)]['column'] - 1); |
||
| 148 | } else { |
||
| 149 | $braceIndent = (strlen($tokens[$lineStart]['content']) - strlen($trimmed) - 1); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | $fix = false; |
||
| 154 | if ($tokens[$stackPtr]['code'] === T_CASE |
||
| 155 | || $tokens[$stackPtr]['code'] === T_DEFAULT |
||
| 156 | ) { |
||
| 157 | // BREAK statements should be indented n spaces from the |
||
| 158 | // CASE or DEFAULT statement. |
||
| 159 | $expectedIndent = ($startColumn + $this->indent - 1); |
||
| 160 | if ($braceIndent !== $expectedIndent) { |
||
| 161 | $error = 'Case breaking statement indented incorrectly; expected %s spaces, found %s'; |
||
| 162 | $data = array( |
||
| 163 | $expectedIndent, |
||
| 164 | $braceIndent, |
||
| 165 | ); |
||
| 166 | $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'BreakIndent', $data); |
||
| 167 | } |
||
| 168 | } else { |
||
| 169 | $expectedIndent = ($startColumn - 1); |
||
| 170 | if ($braceIndent !== $expectedIndent) { |
||
| 171 | $error = 'Closing brace indented incorrectly; expected %s spaces, found %s'; |
||
| 172 | $data = array( |
||
| 173 | $expectedIndent, |
||
| 174 | $braceIndent, |
||
| 175 | ); |
||
| 176 | $fix = $phpcsFile->addFixableError($error, $scopeEnd, 'Indent', $data); |
||
| 177 | } |
||
| 178 | }//end if |
||
| 179 | |||
| 180 | if ($fix === true && $phpcsFile->fixer->enabled === true) { |
||
| 181 | $spaces = str_repeat(' ', $expectedIndent); |
||
| 182 | if ($braceIndent === 0) { |
||
| 183 | $phpcsFile->fixer->addContentBefore($lineStart, $spaces); |
||
| 184 | } else { |
||
| 185 | $phpcsFile->fixer->replaceToken($lineStart, ltrim($tokens[$lineStart]['content'])); |
||
| 186 | $phpcsFile->fixer->addContentBefore($lineStart, $spaces); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | }//end process() |
||
| 191 | |||
| 194 |