| Conditions | 34 |
| Paths | 108 |
| Total Lines | 133 |
| Code Lines | 93 |
| 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 |
||
| 149 | private function readFile(array &$contents, string $file, string $ext): void |
||
| 150 | { |
||
| 151 | if (!file_exists($file)) { |
||
| 152 | return; |
||
| 153 | } |
||
| 154 | $source = fopen($file, 'r'); |
||
| 155 | if (false === $source) { |
||
| 156 | return; |
||
| 157 | } |
||
| 158 | |||
| 159 | // avoid exposure of absolute server path |
||
| 160 | $pathParts = explode($this->rootDir, $file); |
||
| 161 | $relativePath = end($pathParts); |
||
| 162 | $contents[] = "/* --- Source file: {$relativePath} */\n\n"; |
||
| 163 | $inMultilineComment = false; |
||
| 164 | $importsAllowed = true; |
||
| 165 | $wasCommentHack = false; |
||
| 166 | while (!feof($source)) { |
||
| 167 | if ('css' === $ext) { |
||
| 168 | $line = fgets($source, 4096); |
||
| 169 | $lineParse = s(false !== $line ? trim($line) : '')->toString(); |
||
| 170 | $lineParseLength = mb_strlen($lineParse, 'UTF-8'); |
||
| 171 | $newLine = ''; |
||
| 172 | // parse line char by char |
||
| 173 | for ($i = 0; $i < $lineParseLength; $i++) { |
||
| 174 | $char = $lineParse[$i]; |
||
| 175 | $nextchar = $i < ($lineParseLength - 1) ? $lineParse[$i + 1] : ''; |
||
| 176 | if (!$inMultilineComment && '/' === $char && '*' === $nextchar) { |
||
| 177 | // a multiline comment starts here |
||
| 178 | $inMultilineComment = true; |
||
| 179 | $wasCommentHack = false; |
||
| 180 | $newLine .= $char . $nextchar; |
||
| 181 | $i++; |
||
| 182 | } elseif ($inMultilineComment && '*' === $char && '/' === $nextchar) { |
||
| 183 | // a multiline comment stops here |
||
| 184 | $inMultilineComment = false; |
||
| 185 | $newLine .= $char . $nextchar; |
||
| 186 | if ('/*\*//*/' === s($lineParse)->slice($i - 3, 8)) { |
||
| 187 | $wasCommentHack = true; |
||
| 188 | $i += 3; // move to end of hack process hack as it where |
||
| 189 | $newLine .= '/*/'; // fix hack comment because we lost some chars with $i += 3 |
||
| 190 | } |
||
| 191 | $i++; |
||
| 192 | } elseif ($importsAllowed && '@' === $char && '@import' === s($lineParse)->slice($i, 7)) { |
||
| 193 | // an @import starts here |
||
| 194 | $lineParseRest = s($lineParse)->slice($i + 7)->trim(); |
||
| 195 | if ($lineParseRest->ignoreCase()->startsWith('url')) { |
||
| 196 | // the @import uses url to specify the path |
||
| 197 | $posEnd = s($lineParse)->indexOf(';', $i); |
||
| 198 | $charsEnd = s($lineParse)->slice($posEnd - 1, 2); |
||
| 199 | if (');' === $charsEnd) { |
||
| 200 | // used url() without media |
||
| 201 | $start = $lineParseRest->indexOf('(') + 1; |
||
| 202 | $end = $lineParseRest->indexOf(')'); |
||
| 203 | $url = $lineParseRest->slice($start, $end - $start); |
||
| 204 | $url = $url->trimStart('"')->trimStart('"'); |
||
| 205 | // fix url |
||
| 206 | if ($url->startsWith('http')) { |
||
| 207 | $newLine .= '@import url("' . $url . '");'; |
||
| 208 | } else { |
||
| 209 | $url = dirname($file) . '/' . $url; |
||
| 210 | if (!$wasCommentHack) { |
||
| 211 | // clear buffer |
||
| 212 | $contents[] = $newLine; |
||
| 213 | $newLine = ''; |
||
| 214 | // process include |
||
| 215 | $this->readFile($contents, $url, $ext); |
||
| 216 | } else { |
||
| 217 | $newLine .= '@import url("' . $url . '");'; |
||
| 218 | } |
||
| 219 | } |
||
| 220 | // skip @import statement |
||
| 221 | $i += $posEnd - $i; |
||
| 222 | } else { |
||
| 223 | // @import contains media type so we can't include its contents. |
||
| 224 | // We need to fix the url instead. |
||
| 225 | $start = $lineParseRest->indexOf('(') + 1; |
||
| 226 | $end = $lineParseRest->indexOf(')'); |
||
| 227 | $url = $lineParseRest->slice($start, $end - $start); |
||
| 228 | $url = $url->trimStart('"')->trimStart('"'); |
||
| 229 | // fix url |
||
| 230 | $url = dirname($file) . '/' . $url; |
||
| 231 | // readd @import with fixed url |
||
| 232 | $newLine .= '@import url("' . $url . '")' . $lineParseRest->slice($end + 1, $lineParseRest->indexOf(';') - $end - 1) . ';'; |
||
| 233 | // skip @import statement |
||
| 234 | $i += $posEnd - $i; |
||
| 235 | } |
||
| 236 | } elseif ($lineParseRest->startsWith('"') || $lineParseRest->startsWith('\'')) { |
||
| 237 | // the @import uses an normal string to specify the path |
||
| 238 | $posEnd = $lineParseRest->indexOf(';'); |
||
| 239 | $url = $lineParseRest->slice(1, $posEnd - 2); |
||
| 240 | $posEnd = s($lineParse)->indexOf(';', $i); |
||
| 241 | // fix url |
||
| 242 | $url = dirname($file) . '/' . $url; |
||
| 243 | if (!$wasCommentHack) { |
||
| 244 | // clear buffer |
||
| 245 | $contents[] = $newLine; |
||
| 246 | $newLine = ''; |
||
| 247 | // process include |
||
| 248 | self::readFile($contents, $url, $ext); |
||
|
|
|||
| 249 | } else { |
||
| 250 | $newLine .= '@import url("' . $url . '");'; |
||
| 251 | } |
||
| 252 | // skip @import statement |
||
| 253 | $i += $posEnd - $i; |
||
| 254 | } |
||
| 255 | } elseif (!$inMultilineComment && ' ' !== $char && "\n" !== $char && "\r\n" !== $char && "\r" !== $char) { |
||
| 256 | // css rule found -> stop processing of @import statements |
||
| 257 | $importsAllowed = false; |
||
| 258 | $newLine .= $char; |
||
| 259 | } else { |
||
| 260 | $newLine .= $char; |
||
| 261 | } |
||
| 262 | } |
||
| 263 | // fix other paths after @import processing |
||
| 264 | if (!$importsAllowed) { |
||
| 265 | $relativePath = str_replace(realpath($this->rootDir), '', $file); |
||
| 266 | $newLine = $this->cssFixPath($newLine, explode('/', dirname($relativePath))); |
||
| 267 | } |
||
| 268 | $contents[] = $newLine; |
||
| 269 | } else { |
||
| 270 | $line = fgets($source, 4096); |
||
| 271 | if (false === $line || 0 === mb_strpos($line, '//# sourceMappingURL=')) { |
||
| 272 | continue; |
||
| 273 | } |
||
| 274 | $contents[] = $line; |
||
| 275 | } |
||
| 276 | } |
||
| 277 | fclose($source); |
||
| 278 | if ('js' === $ext) { |
||
| 279 | $contents[] = "\n;\n"; |
||
| 280 | } else { |
||
| 281 | $contents[] = "\n\n"; |
||
| 282 | } |
||
| 323 |