| Conditions | 62 |
| Paths | > 20000 |
| Total Lines | 228 |
| Code Lines | 129 |
| Lines | 35 |
| Ratio | 15.35 % |
| 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 |
||
| 81 | public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr) |
||
| 82 | { |
||
| 83 | $tokens = $phpcsFile->getTokens(); |
||
| 84 | |||
| 85 | if (isset($tokens[$stackPtr]['scope_opener']) === true) { |
||
| 86 | $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'no'); |
||
| 87 | return; |
||
| 88 | } |
||
| 89 | |||
| 90 | // Ignore the ELSE in ELSE IF. We'll process the IF part later. |
||
| 91 | View Code Duplication | if ($tokens[$stackPtr]['code'] === T_ELSE) { |
|
| 92 | $next = $phpcsFile->findNext(T_WHITESPACE, ($stackPtr + 1), null, true); |
||
| 93 | if ($tokens[$next]['code'] === T_IF) { |
||
| 94 | return; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($tokens[$stackPtr]['code'] === T_WHILE) { |
||
| 99 | // This could be from a DO WHILE, which doesn't have an opening brace. |
||
| 100 | $lastContent = $phpcsFile->findPrevious(T_WHITESPACE, ($stackPtr - 1), null, true); |
||
| 101 | View Code Duplication | if ($tokens[$lastContent]['code'] === T_CLOSE_CURLY_BRACKET) { |
|
| 102 | $brace = $tokens[$lastContent]; |
||
| 103 | if (isset($brace['scope_condition']) === true) { |
||
| 104 | $condition = $tokens[$brace['scope_condition']]; |
||
| 105 | if ($condition['code'] === T_DO) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | // In Javascript DO WHILE loops without curly braces are legal. This |
||
| 112 | // is only valid if a single statement is present between the DO and |
||
| 113 | // the WHILE. We can detect this by checking only a single semicolon |
||
| 114 | // is present between them. |
||
| 115 | if ($phpcsFile->tokenizerType === 'JS') { |
||
| 116 | $lastDo = $phpcsFile->findPrevious(T_DO, ($stackPtr - 1)); |
||
| 117 | $lastSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, ($stackPtr - 1)); |
||
| 118 | if ($lastDo !== false && $lastSemicolon !== false && $lastDo < $lastSemicolon) { |
||
| 119 | $precedingSemicolon = $phpcsFile->findPrevious(T_SEMICOLON, ($lastSemicolon - 1)); |
||
| 120 | if ($precedingSemicolon === false || $precedingSemicolon < $lastDo) { |
||
| 121 | return; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | }//end if |
||
| 126 | |||
| 127 | // This is a control structure without an opening brace, |
||
| 128 | // so it is an inline statement. |
||
| 129 | if ($this->error === true) { |
||
| 130 | $fix = $phpcsFile->addFixableError('Inline control structures are not allowed', $stackPtr, 'NotAllowed'); |
||
| 131 | } else { |
||
| 132 | $fix = $phpcsFile->addFixableWarning('Inline control structures are discouraged', $stackPtr, 'Discouraged'); |
||
| 133 | } |
||
| 134 | |||
| 135 | $phpcsFile->recordMetric($stackPtr, 'Control structure defined inline', 'yes'); |
||
| 136 | |||
| 137 | // Stop here if we are not fixing the error. |
||
| 138 | if ($fix !== true) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | $phpcsFile->fixer->beginChangeset(); |
||
| 143 | View Code Duplication | if (isset($tokens[$stackPtr]['parenthesis_closer']) === true) { |
|
| 144 | $closer = $tokens[$stackPtr]['parenthesis_closer']; |
||
| 145 | } else { |
||
| 146 | $closer = $stackPtr; |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($tokens[($closer + 1)]['code'] === T_WHITESPACE |
||
| 150 | || $tokens[($closer + 1)]['code'] === T_SEMICOLON |
||
| 151 | ) { |
||
| 152 | $phpcsFile->fixer->addContent($closer, ' {'); |
||
| 153 | } else { |
||
| 154 | $phpcsFile->fixer->addContent($closer, ' { '); |
||
| 155 | } |
||
| 156 | |||
| 157 | $fixableScopeOpeners = $this->register(); |
||
| 158 | |||
| 159 | $lastNonEmpty = $closer; |
||
| 160 | for ($end = ($closer + 1); $end < $phpcsFile->numTokens; $end++) { |
||
| 161 | if ($tokens[$end]['code'] === T_SEMICOLON) { |
||
| 162 | break; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($tokens[$end]['code'] === T_CLOSE_TAG) { |
||
| 166 | $end = $lastNonEmpty; |
||
| 167 | break; |
||
| 168 | } |
||
| 169 | |||
| 170 | if (in_array($tokens[$end]['code'], $fixableScopeOpeners) === true |
||
| 171 | && isset($tokens[$end]['scope_opener']) === false |
||
| 172 | ) { |
||
| 173 | // The best way to fix nested inline scopes is middle-out. |
||
| 174 | // So skip this one. It will be detected and fixed on a future loop. |
||
| 175 | $phpcsFile->fixer->rollbackChangeset(); |
||
| 176 | return; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (isset($tokens[$end]['scope_opener']) === true) { |
||
| 180 | $type = $tokens[$end]['code']; |
||
| 181 | $end = $tokens[$end]['scope_closer']; |
||
| 182 | if ($type === T_DO || $type === T_IF || $type === T_ELSEIF) { |
||
| 183 | $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($end + 1), null, true); |
||
| 184 | if ($next === false) { |
||
| 185 | break; |
||
| 186 | } |
||
| 187 | |||
| 188 | $nextType = $tokens[$next]['code']; |
||
| 189 | |||
| 190 | // Let additional conditions loop and find their ending. |
||
| 191 | if (($type === T_IF |
||
| 192 | || $type === T_ELSEIF) |
||
| 193 | && ($nextType === T_ELSEIF |
||
| 194 | || $nextType === T_ELSE) |
||
| 195 | ) { |
||
| 196 | continue; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Account for DO... WHILE conditions. |
||
| 200 | if ($type === T_DO && $nextType === T_WHILE) { |
||
| 201 | $end = $phpcsFile->findNext(T_SEMICOLON, ($next + 1)); |
||
| 202 | } |
||
| 203 | }//end if |
||
| 204 | |||
| 205 | if ($tokens[$end]['code'] !== T_END_HEREDOC |
||
| 206 | && $tokens[$end]['code'] !== T_END_NOWDOC |
||
| 207 | ) { |
||
| 208 | break; |
||
| 209 | } |
||
| 210 | }//end if |
||
| 211 | |||
| 212 | View Code Duplication | if (isset($tokens[$end]['parenthesis_closer']) === true) { |
|
| 213 | $end = $tokens[$end]['parenthesis_closer']; |
||
| 214 | $lastNonEmpty = $end; |
||
| 215 | continue; |
||
| 216 | } |
||
| 217 | |||
| 218 | if ($tokens[$end]['code'] !== T_WHITESPACE) { |
||
| 219 | $lastNonEmpty = $end; |
||
| 220 | } |
||
| 221 | }//end for |
||
| 222 | |||
| 223 | if ($end === $phpcsFile->numTokens) { |
||
| 224 | $end = $lastNonEmpty; |
||
| 225 | } |
||
| 226 | |||
| 227 | $next = $phpcsFile->findNext(PHP_CodeSniffer_Tokens::$emptyTokens, ($end + 1), null, true); |
||
| 228 | |||
| 229 | if ($next === false || $tokens[$next]['line'] !== $tokens[$end]['line']) { |
||
| 230 | // Looks for completely empty statements. |
||
| 231 | $next = $phpcsFile->findNext(T_WHITESPACE, ($closer + 1), ($end + 1), true); |
||
| 232 | |||
| 233 | // Account for a comment on the end of the line. |
||
| 234 | for ($endLine = $end; $endLine < $phpcsFile->numTokens; $endLine++) { |
||
| 235 | View Code Duplication | if (isset($tokens[($endLine + 1)]) === false |
|
| 236 | || $tokens[$endLine]['line'] !== $tokens[($endLine + 1)]['line'] |
||
| 237 | ) { |
||
| 238 | break; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if ($tokens[$endLine]['code'] !== T_COMMENT) { |
||
| 243 | $endLine = $end; |
||
| 244 | } |
||
| 245 | } else { |
||
| 246 | $next = ($end + 1); |
||
| 247 | $endLine = $end; |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($next !== $end) { |
||
| 251 | if ($endLine !== $end) { |
||
| 252 | $endToken = $endLine; |
||
| 253 | $addedContent = ''; |
||
| 254 | } else { |
||
| 255 | $endToken = $end; |
||
| 256 | $addedContent = $phpcsFile->eolChar; |
||
| 257 | |||
| 258 | View Code Duplication | if ($tokens[$end]['code'] !== T_SEMICOLON |
|
| 259 | && $tokens[$end]['code'] !== T_CLOSE_CURLY_BRACKET |
||
| 260 | ) { |
||
| 261 | $phpcsFile->fixer->addContent($end, '; '); |
||
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | $next = $phpcsFile->findNext(T_WHITESPACE, ($endToken + 1), null, true); |
||
| 266 | if ($next !== false |
||
| 267 | && ($tokens[$next]['code'] === T_ELSE |
||
| 268 | || $tokens[$next]['code'] === T_ELSEIF) |
||
| 269 | ) { |
||
| 270 | $phpcsFile->fixer->addContentBefore($next, '} '); |
||
| 271 | } else { |
||
| 272 | $indent = ''; |
||
| 273 | for ($first = $stackPtr; $first > 0; $first--) { |
||
| 274 | if ($first === 1 |
||
| 275 | || $tokens[($first - 1)]['line'] !== $tokens[$first]['line'] |
||
| 276 | ) { |
||
| 277 | break; |
||
| 278 | } |
||
| 279 | } |
||
| 280 | |||
| 281 | if ($tokens[$first]['code'] === T_WHITESPACE) { |
||
| 282 | $indent = $tokens[$first]['content']; |
||
| 283 | } else if ($tokens[$first]['code'] === T_INLINE_HTML |
||
| 284 | || $tokens[$first]['code'] === T_OPEN_TAG |
||
| 285 | ) { |
||
| 286 | $addedContent = ''; |
||
| 287 | } |
||
| 288 | |||
| 289 | $addedContent .= $indent.'}'; |
||
| 290 | if ($next !== false && $tokens[$endToken]['code'] === T_COMMENT) { |
||
| 291 | $addedContent .= $phpcsFile->eolChar; |
||
| 292 | } |
||
| 293 | |||
| 294 | $phpcsFile->fixer->addContent($endToken, $addedContent); |
||
| 295 | }//end if |
||
| 296 | } else { |
||
| 297 | if ($endLine !== $end) { |
||
| 298 | $phpcsFile->fixer->replaceToken($end, ''); |
||
| 299 | $phpcsFile->fixer->addNewlineBefore($endLine); |
||
| 300 | $phpcsFile->fixer->addContent($endLine, '}'); |
||
| 301 | } else { |
||
| 302 | $phpcsFile->fixer->replaceToken($end, '}'); |
||
| 303 | } |
||
| 304 | }//end if |
||
| 305 | |||
| 306 | $phpcsFile->fixer->endChangeset(); |
||
| 307 | |||
| 308 | }//end process() |
||
| 309 | |||
| 312 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.