| Conditions | 32 |
| Paths | 1326 |
| Total Lines | 125 |
| 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 |
||
| 58 | public function process(File $phpcsFile, $stackPtr) |
||
| 59 | { |
||
| 60 | if ($this->tabWidth === null) { |
||
| 61 | if (isset($phpcsFile->config->tabWidth) === false || $phpcsFile->config->tabWidth === 0) { |
||
| 62 | // We have no idea how wide tabs are, so assume 4 spaces for metrics. |
||
| 63 | $this->tabWidth = 4; |
||
| 64 | } else { |
||
| 65 | $this->tabWidth = $phpcsFile->config->tabWidth; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | $tokens = $phpcsFile->getTokens(); |
||
| 70 | $checkTokens = [ |
||
| 71 | T_WHITESPACE => true, |
||
| 72 | T_INLINE_HTML => true, |
||
| 73 | T_DOC_COMMENT_WHITESPACE => true, |
||
| 74 | T_DOC_COMMENT_STRING => true, |
||
| 75 | T_COMMENT => true, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | for ($i = 0; $i < $phpcsFile->numTokens; $i++) { |
||
| 79 | if (isset($checkTokens[$tokens[$i]['code']]) === false) { |
||
| 80 | continue; |
||
| 81 | } |
||
| 82 | |||
| 83 | // If tabs are being converted to spaces by the tokeniser, the |
||
| 84 | // original content should be checked instead of the converted content. |
||
| 85 | if (isset($tokens[$i]['orig_content']) === true) { |
||
| 86 | $content = $tokens[$i]['orig_content']; |
||
| 87 | } else { |
||
| 88 | $content = $tokens[$i]['content']; |
||
| 89 | } |
||
| 90 | |||
| 91 | if ($content === '') { |
||
| 92 | continue; |
||
| 93 | } |
||
| 94 | |||
| 95 | // If this is an inline HTML token or a subsequent line of a multi-line comment, |
||
| 96 | // split off the indentation as that is the only part to take into account for the metrics. |
||
| 97 | $indentation = $content; |
||
| 98 | if (($tokens[$i]['code'] === T_INLINE_HTML |
||
| 99 | || $tokens[$i]['code'] === T_COMMENT) |
||
| 100 | && preg_match('`^(\s*)\S.*`s', $content, $matches) > 0 |
||
| 101 | ) { |
||
| 102 | if (isset($matches[1]) === true) { |
||
| 103 | $indentation = $matches[1]; |
||
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | if (($tokens[$i]['code'] === T_DOC_COMMENT_WHITESPACE |
||
| 108 | || $tokens[$i]['code'] === T_COMMENT) |
||
| 109 | && $indentation === ' ' |
||
| 110 | ) { |
||
| 111 | // Ignore all non-indented comments, especially for recording metrics. |
||
| 112 | continue; |
||
| 113 | } |
||
| 114 | |||
| 115 | $recordMetrics = true; |
||
| 116 | if ($content === $indentation |
||
| 117 | && isset($tokens[($i + 1)]) === true |
||
| 118 | && $tokens[$i]['line'] < $tokens[($i + 1)]['line'] |
||
| 119 | ) { |
||
| 120 | // Don't record metrics for empty lines. |
||
| 121 | $recordMetrics = false; |
||
| 122 | } |
||
| 123 | |||
| 124 | $foundTabs = substr_count($content, "\t"); |
||
| 125 | |||
| 126 | $error = 'Spaces must be used to indent lines; tabs are not allowed'; |
||
| 127 | $errorCode = 'TabsUsed'; |
||
| 128 | if ($tokens[$i]['column'] === 1) { |
||
| 129 | if ($recordMetrics === true) { |
||
| 130 | $foundIndentSpaces = substr_count($indentation, ' '); |
||
| 131 | $foundIndentTabs = substr_count($indentation, "\t"); |
||
| 132 | |||
| 133 | if ($foundIndentTabs > 0 && $foundIndentSpaces === 0) { |
||
| 134 | $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); |
||
| 135 | } else if ($foundIndentTabs === 0 && $foundIndentSpaces > 0) { |
||
| 136 | $phpcsFile->recordMetric($i, 'Line indent', 'spaces'); |
||
| 137 | } else if ($foundIndentTabs > 0 && $foundIndentSpaces > 0) { |
||
| 138 | $spacePosition = strpos($indentation, ' '); |
||
| 139 | $tabAfterSpaces = strpos($indentation, "\t", $spacePosition); |
||
| 140 | if ($tabAfterSpaces !== false) { |
||
| 141 | $phpcsFile->recordMetric($i, 'Line indent', 'mixed'); |
||
| 142 | } else { |
||
| 143 | // Check for use of precision spaces. |
||
| 144 | $numTabs = (int) floor($foundIndentSpaces / $this->tabWidth); |
||
| 145 | if ($numTabs === 0) { |
||
| 146 | $phpcsFile->recordMetric($i, 'Line indent', 'tabs'); |
||
| 147 | } else { |
||
| 148 | $phpcsFile->recordMetric($i, 'Line indent', 'mixed'); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | }//end if |
||
| 153 | } else { |
||
| 154 | // Look for tabs so we can report and replace, but don't |
||
| 155 | // record any metrics about them because they aren't |
||
| 156 | // line indent tokens. |
||
| 157 | if ($foundTabs > 0) { |
||
| 158 | $error = 'Spaces must be used for alignment; tabs are not allowed'; |
||
| 159 | $errorCode = 'NonIndentTabsUsed'; |
||
| 160 | } |
||
| 161 | }//end if |
||
| 162 | |||
| 163 | if ($foundTabs === 0) { |
||
| 164 | continue; |
||
| 165 | } |
||
| 166 | |||
| 167 | $fix = $phpcsFile->addFixableError($error, $i, $errorCode); |
||
| 168 | if ($fix === true) { |
||
| 169 | if (isset($tokens[$i]['orig_content']) === true) { |
||
| 170 | // Use the replacement that PHPCS has already done. |
||
| 171 | $phpcsFile->fixer->replaceToken($i, $tokens[$i]['content']); |
||
| 172 | } else { |
||
| 173 | // Replace tabs with spaces, using an indent of tabWidth spaces. |
||
| 174 | // Other sniffs can then correct the indent if they need to. |
||
| 175 | $newContent = str_replace("\t", str_repeat(' ', $this->tabWidth), $tokens[$i]['content']); |
||
| 176 | $phpcsFile->fixer->replaceToken($i, $newContent); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | }//end for |
||
| 180 | |||
| 181 | // Ignore the rest of the file. |
||
| 182 | return ($phpcsFile->numTokens + 1); |
||
| 183 | |||
| 188 |