Conditions | 33 |
Paths | 238 |
Total Lines | 129 |
Code Lines | 89 |
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 |
||
126 | private function readFile(array &$contents, string $file, string $ext): void |
||
127 | { |
||
128 | if (!file_exists($file)) { |
||
129 | return; |
||
130 | } |
||
131 | $source = fopen($file, 'rb'); |
||
132 | if (!$source) { |
||
133 | return; |
||
134 | } |
||
135 | |||
136 | // avoid exposure of absolute server path |
||
137 | $pathParts = explode($this->rootDir, $file); |
||
138 | $relativePath = end($pathParts); |
||
139 | $contents[] = "/* --- Source file: {$relativePath} */\n\n"; |
||
140 | $inMultilineComment = false; |
||
141 | $importsAllowd = true; |
||
142 | $wasCommentHack = false; |
||
143 | while (!feof($source)) { |
||
144 | if ('css' === $ext) { |
||
145 | $line = fgets($source, 4096); |
||
146 | $lineParse = false !== $line ? trim($line) : ''; |
||
147 | $lineParse_length = mb_strlen($lineParse, 'UTF-8'); |
||
148 | $newLine = ''; |
||
149 | // parse line char by char |
||
150 | for ($i = 0; $i < $lineParse_length; $i++) { |
||
151 | $char = $lineParse[$i]; |
||
152 | $nextchar = $i < ($lineParse_length - 1) ? $lineParse[$i + 1] : ''; |
||
153 | if (!$inMultilineComment && '/' === $char && '*' === $nextchar) { |
||
154 | // a multiline comment starts here |
||
155 | $inMultilineComment = true; |
||
156 | $wasCommentHack = false; |
||
157 | $newLine .= $char . $nextchar; |
||
158 | $i++; |
||
159 | } elseif ($inMultilineComment && '*' === $char && '/' === $nextchar) { |
||
160 | // a multiline comment stops here |
||
161 | $inMultilineComment = false; |
||
162 | $newLine .= $char . $nextchar; |
||
163 | if ('/*\*//*/' === mb_substr($lineParse, $i - 3, 8)) { |
||
164 | $wasCommentHack = true; |
||
165 | $i += 3; // move to end of hack process hack as it where |
||
166 | $newLine .= '/*/'; // fix hack comment because we lost some chars with $i += 3 |
||
167 | } |
||
168 | $i++; |
||
169 | } elseif ($importsAllowd && '@' === $char && '@import' === mb_substr($lineParse, $i, 7)) { |
||
170 | // an @import starts here |
||
171 | $lineParseRest = trim(mb_substr($lineParse, $i + 7)); |
||
172 | if (0 === mb_stripos($lineParseRest, 'url')) { |
||
173 | // the @import uses url to specify the path |
||
174 | $posEnd = mb_strpos($lineParse, ';', $i); |
||
175 | $charsEnd = mb_substr($lineParse, $posEnd - 1, 2); |
||
176 | if (');' === $charsEnd) { |
||
177 | // used url() without media |
||
178 | $start = mb_strpos($lineParseRest, '(') + 1; |
||
179 | $end = mb_strpos($lineParseRest, ')'); |
||
180 | $url = mb_substr($lineParseRest, $start, $end - $start); |
||
181 | if (0 === mb_strpos($url, '"') | 0 === mb_strpos($url, "'")) { |
||
182 | $url = mb_substr($url, 1, -1); |
||
183 | } |
||
184 | // fix url |
||
185 | $url = dirname($file) . '/' . $url; |
||
186 | if (!$wasCommentHack) { |
||
187 | // clear buffer |
||
188 | $contents[] = $newLine; |
||
189 | $newLine = ''; |
||
190 | // process include |
||
191 | $this->readFile($contents, $url, $ext); |
||
192 | } else { |
||
193 | $newLine .= '@import url("' . $url . '");'; |
||
194 | } |
||
195 | // skip @import statement |
||
196 | $i += $posEnd - $i; |
||
197 | } else { |
||
198 | // @import contains media type so we can't include its contents. |
||
199 | // We need to fix the url instead. |
||
200 | $start = mb_strpos($lineParseRest, '(') + 1; |
||
201 | $end = mb_strpos($lineParseRest, ')'); |
||
202 | $url = mb_substr($lineParseRest, $start, $end - $start); |
||
203 | if (0 === mb_strpos($url, '"') | 0 === mb_strpos($url, "'")) { |
||
204 | $url = mb_substr($url, 1, -1); |
||
205 | } |
||
206 | // fix url |
||
207 | $url = dirname($file) . '/' . $url; |
||
208 | // readd @import with fixed url |
||
209 | $newLine .= '@import url("' . $url . '")' . mb_substr($lineParseRest, $end + 1, mb_strpos($lineParseRest, ';') - $end - 1) . ';'; |
||
210 | // skip @import statement |
||
211 | $i += $posEnd - $i; |
||
212 | } |
||
213 | } elseif (0 === mb_strpos($lineParseRest, '"') || 0 === mb_strpos($lineParseRest, '\'')) { |
||
214 | // the @import uses an normal string to specify the path |
||
215 | $posEnd = mb_strpos($lineParseRest, ';'); |
||
216 | $url = mb_substr($lineParseRest, 1, $posEnd - 2); |
||
217 | $posEnd = mb_strpos($lineParse, ';', $i); |
||
218 | // fix url |
||
219 | $url = dirname($file) . '/' . $url; |
||
220 | if (!$wasCommentHack) { |
||
221 | // clear buffer |
||
222 | $contents[] = $newLine; |
||
223 | $newLine = ''; |
||
224 | // process include |
||
225 | self::readFile($contents, $url, $ext); |
||
226 | } else { |
||
227 | $newLine .= '@import url("' . $url . '");'; |
||
228 | } |
||
229 | // skip @import statement |
||
230 | $i += $posEnd - $i; |
||
231 | } |
||
232 | } elseif (!$inMultilineComment && ' ' !== $char && "\n" !== $char && "\r\n" !== $char && "\r" !== $char) { |
||
233 | // css rule found -> stop processing of @import statements |
||
234 | $importsAllowd = false; |
||
235 | $newLine .= $char; |
||
236 | } else { |
||
237 | $newLine .= $char; |
||
238 | } |
||
239 | } |
||
240 | // fix other paths after @import processing |
||
241 | if (!$importsAllowd) { |
||
242 | $relativePath = str_replace(realpath($this->rootDir), '', $file); |
||
243 | $newLine = $this->cssFixPath($newLine, explode('/', dirname($relativePath))); |
||
244 | } |
||
245 | $contents[] = $newLine; |
||
246 | } else { |
||
247 | $contents[] = fgets($source, 4096); |
||
248 | } |
||
249 | } |
||
250 | fclose($source); |
||
251 | if ('js' === $ext) { |
||
252 | $contents[] = "\n;\n"; |
||
253 | } else { |
||
254 | $contents[] = "\n\n"; |
||
255 | } |
||
296 |