| Conditions | 8 |
| Paths | 17 |
| Total Lines | 53 |
| Code Lines | 27 |
| 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 |
||
| 143 | public static function normalizeIndents(string $string, string $tabulationCost = " "): string |
||
| 144 | { |
||
| 145 | $string = self::normalizeEndings($string, false); |
||
| 146 | $lines = explode("\n", $string); |
||
| 147 | |||
| 148 | $minIndent = null; |
||
| 149 | foreach ($lines as $line) { |
||
| 150 | if (!trim($line)) { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | $line = str_replace("\t", $tabulationCost, $line); |
||
| 155 | |||
| 156 | //Getting indent size |
||
| 157 | if (!preg_match("/^( +)/", $line, $matches)) { |
||
| 158 | //Some line has no indent |
||
| 159 | return $string; |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($minIndent === null) { |
||
| 163 | $minIndent = strlen($matches[1]); |
||
| 164 | } |
||
| 165 | |||
| 166 | $minIndent = min($minIndent, strlen($matches[1])); |
||
| 167 | } |
||
| 168 | |||
| 169 | //Fixing indent |
||
| 170 | foreach ($lines as &$line) { |
||
| 171 | if (empty($line)) { |
||
| 172 | continue; |
||
| 173 | } |
||
| 174 | |||
| 175 | //Getting line indent |
||
| 176 | preg_match("/^([ \t]+)/", $line, $matches); |
||
| 177 | $indent = $matches[1]; |
||
| 178 | |||
| 179 | if (!trim($line)) { |
||
| 180 | $line = ''; |
||
| 181 | continue; |
||
| 182 | } |
||
| 183 | |||
| 184 | //Getting new indent |
||
| 185 | $useIndent = str_repeat( |
||
| 186 | " ", |
||
| 187 | strlen(str_replace("\t", $tabulationCost, $indent)) - $minIndent |
||
| 188 | ); |
||
| 189 | |||
| 190 | $line = $useIndent . substr($line, strlen($indent)); |
||
| 191 | unset($line); |
||
| 192 | } |
||
| 193 | |||
| 194 | return join("\n", $lines); |
||
| 195 | } |
||
| 196 | } |