| Conditions | 27 |
| Paths | 1142 |
| Total Lines | 109 |
| 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 |
||
| 59 | public function process(File $phpcsFile, $stackPtr) |
||
| 60 | { |
||
| 61 | $tokens = $phpcsFile->getTokens(); |
||
| 62 | |||
| 63 | if (isset($tokens[$stackPtr]['scope_opener']) === false) { |
||
| 64 | return; |
||
| 65 | } |
||
| 66 | |||
| 67 | if (($tokens[$stackPtr]['code'] === T_FUNCTION |
||
| 68 | && (bool) $this->checkFunctions === false) |
||
| 69 | || ($tokens[$stackPtr]['code'] === T_CLOSURE |
||
| 70 | && (bool) $this->checkClosures === false) |
||
| 71 | ) { |
||
| 72 | return; |
||
| 73 | } |
||
| 74 | |||
| 75 | $openingBrace = $tokens[$stackPtr]['scope_opener']; |
||
| 76 | $closeBracket = $tokens[$stackPtr]['parenthesis_closer']; |
||
| 77 | if ($tokens[$stackPtr]['code'] === T_CLOSURE) { |
||
| 78 | $use = $phpcsFile->findNext(T_USE, ($closeBracket + 1), $tokens[$stackPtr]['scope_opener']); |
||
| 79 | if ($use !== false) { |
||
| 80 | $openBracket = $phpcsFile->findNext(T_OPEN_PARENTHESIS, ($use + 1)); |
||
| 81 | $closeBracket = $tokens[$openBracket]['parenthesis_closer']; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | $functionLine = $tokens[$closeBracket]['line']; |
||
| 86 | $braceLine = $tokens[$openingBrace]['line']; |
||
| 87 | |||
| 88 | $lineDifference = ($braceLine - $functionLine); |
||
| 89 | |||
| 90 | if ($lineDifference > 0) { |
||
| 91 | $phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'new line'); |
||
| 92 | $error = 'Opening brace should be on the same line as the declaration'; |
||
| 93 | $fix = $phpcsFile->addFixableError($error, $openingBrace, 'BraceOnNewLine'); |
||
| 94 | if ($fix === true) { |
||
| 95 | $prev = $phpcsFile->findPrevious(Tokens::$emptyTokens, ($openingBrace - 1), $closeBracket, true); |
||
| 96 | $phpcsFile->fixer->beginChangeset(); |
||
| 97 | $phpcsFile->fixer->addContent($prev, ' {'); |
||
| 98 | $phpcsFile->fixer->replaceToken($openingBrace, ''); |
||
| 99 | if ($tokens[($openingBrace + 1)]['code'] === T_WHITESPACE |
||
| 100 | && $tokens[($openingBrace + 2)]['line'] > $tokens[$openingBrace]['line'] |
||
| 101 | ) { |
||
| 102 | // Brace is followed by a new line, so remove it to ensure we don't |
||
| 103 | // leave behind a blank line at the top of the block. |
||
| 104 | $phpcsFile->fixer->replaceToken(($openingBrace + 1), ''); |
||
| 105 | |||
| 106 | if ($tokens[($openingBrace - 1)]['code'] === T_WHITESPACE |
||
| 107 | && $tokens[($openingBrace - 1)]['line'] === $tokens[$openingBrace]['line'] |
||
| 108 | && $tokens[($openingBrace - 2)]['line'] < $tokens[$openingBrace]['line'] |
||
| 109 | ) { |
||
| 110 | // Brace is preceeded by indent, so remove it to ensure we don't |
||
| 111 | // leave behind more indent than is required for the first line. |
||
| 112 | $phpcsFile->fixer->replaceToken(($openingBrace - 1), ''); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $phpcsFile->fixer->endChangeset(); |
||
| 117 | }//end if |
||
| 118 | }//end if |
||
| 119 | |||
| 120 | $phpcsFile->recordMetric($stackPtr, 'Function opening brace placement', 'same line'); |
||
| 121 | |||
| 122 | $next = $phpcsFile->findNext(T_WHITESPACE, ($openingBrace + 1), null, true); |
||
| 123 | if ($tokens[$next]['line'] === $tokens[$openingBrace]['line']) { |
||
| 124 | if ($next === $tokens[$stackPtr]['scope_closer'] |
||
| 125 | || $tokens[$next]['code'] === T_CLOSE_TAG |
||
| 126 | ) { |
||
| 127 | // Ignore empty functions. |
||
| 128 | return; |
||
| 129 | } |
||
| 130 | |||
| 131 | $error = 'Opening brace must be the last content on the line'; |
||
| 132 | $fix = $phpcsFile->addFixableError($error, $openingBrace, 'ContentAfterBrace'); |
||
| 133 | if ($fix === true) { |
||
| 134 | $phpcsFile->fixer->addNewline($openingBrace); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | |||
| 138 | // Only continue checking if the opening brace looks good. |
||
| 139 | if ($lineDifference > 0) { |
||
| 140 | return; |
||
| 141 | } |
||
| 142 | |||
| 143 | // We are looking for tabs, even if they have been replaced, because |
||
| 144 | // we enforce a space here. |
||
| 145 | if (isset($tokens[($openingBrace - 1)]['orig_content']) === true) { |
||
| 146 | $spacing = $tokens[($openingBrace - 1)]['orig_content']; |
||
| 147 | } else { |
||
| 148 | $spacing = $tokens[($openingBrace - 1)]['content']; |
||
| 149 | } |
||
| 150 | |||
| 151 | if ($tokens[($openingBrace - 1)]['code'] !== T_WHITESPACE) { |
||
| 152 | $length = 0; |
||
| 153 | } else if ($spacing === "\t") { |
||
| 154 | $length = '\t'; |
||
| 155 | } else { |
||
| 156 | $length = strlen($spacing); |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($length !== 1) { |
||
| 160 | $error = 'Expected 1 space before opening brace; found %s'; |
||
| 161 | $data = [$length]; |
||
| 162 | $fix = $phpcsFile->addFixableError($error, $closeBracket, 'SpaceBeforeBrace', $data); |
||
| 163 | if ($fix === true) { |
||
| 164 | if ($length === 0 || $length === '\t') { |
||
| 165 | $phpcsFile->fixer->addContentBefore($openingBrace, ' '); |
||
| 166 | } else { |
||
| 167 | $phpcsFile->fixer->replaceToken(($openingBrace - 1), ' '); |
||
| 168 | } |
||
| 176 |