Total Complexity | 60 |
Total Lines | 422 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like AbstractPosition often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AbstractPosition, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | abstract class AbstractPosition |
||
28 | { |
||
29 | /** |
||
30 | * AbstractPosition::__get |
||
31 | * |
||
32 | * @param string $property |
||
33 | * |
||
34 | * @return \O2System\Framework\Http\Presenter\Assets\Collections\Css|\O2System\Framework\Http\Presenter\Assets\Collections\Font|\O2System\Framework\Http\Presenter\Assets\Collections\Javascript|null |
||
|
|||
35 | */ |
||
36 | public function __get($property) |
||
37 | { |
||
38 | return isset($this->{$property}) ? $this->{$property} : null; |
||
39 | } |
||
40 | |||
41 | // ------------------------------------------------------------------------ |
||
42 | |||
43 | /** |
||
44 | * AbstractPosition::getUrl |
||
45 | * |
||
46 | * @param string $realPath |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | public function getUrl($realPath) |
||
51 | { |
||
52 | if (strpos($realPath, 'http') !== false) { |
||
53 | return $realPath; |
||
54 | } |
||
55 | |||
56 | return (new Uri()) |
||
57 | ->withQuery(null) |
||
58 | ->withSegments( |
||
59 | new Uri\Segments( |
||
60 | str_replace( |
||
61 | [PATH_PUBLIC, DIRECTORY_SEPARATOR], |
||
62 | ['', '/'], |
||
63 | $realPath |
||
64 | ) |
||
65 | ) |
||
66 | ) |
||
67 | ->__toString(); |
||
68 | } |
||
69 | |||
70 | // ------------------------------------------------------------------------ |
||
71 | |||
72 | /** |
||
73 | * AbstractPosition::loadCollections |
||
74 | * |
||
75 | * @param array $collections |
||
76 | */ |
||
77 | public function loadCollections(array $collections) |
||
78 | { |
||
79 | foreach ($collections as $subDir => $files) { |
||
80 | if (is_array($files) and count($files)) { |
||
81 | |||
82 | // normalize the subDirectory with a trailing separator |
||
83 | $subDir = str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $subDir); |
||
84 | $subDir = rtrim($subDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
85 | |||
86 | foreach ($files as $file) { |
||
87 | if (strpos($file, 'http') !== false) { |
||
88 | $this->loadUrl($file, $subDir); |
||
89 | } else { |
||
90 | $this->loadFile($file, $subDir); |
||
91 | } |
||
92 | } |
||
93 | } elseif (is_string($files)) { |
||
94 | $this->loadFile($files); |
||
95 | } |
||
96 | } |
||
97 | } |
||
98 | |||
99 | // ------------------------------------------------------------------------ |
||
100 | |||
101 | /** |
||
102 | * AbstractPosition::loadUrl |
||
103 | * |
||
104 | * @param string $url |
||
105 | * @param string|null $subDir |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function loadUrl($url, $subDir = null) |
||
110 | { |
||
111 | $property = is_null($subDir) ? 'css' : null; |
||
112 | |||
113 | if (is_null($property)) { |
||
114 | switch ($subDir) { |
||
115 | default: |
||
116 | case 'css/': |
||
117 | $property = 'css'; |
||
118 | break; |
||
119 | case 'font/': |
||
120 | case 'fonts/': |
||
121 | $property = 'font'; |
||
122 | break; |
||
123 | case 'js/': |
||
124 | $property = 'javascript'; |
||
125 | break; |
||
126 | } |
||
127 | } |
||
128 | |||
129 | if (property_exists($this, $property)) { |
||
130 | if ( ! call_user_func_array([$this->{$property}, 'has'], [$url])) { |
||
131 | $this->{$property}->append($url); |
||
132 | |||
133 | return true; |
||
134 | } |
||
135 | } |
||
136 | |||
137 | return false; |
||
138 | } |
||
139 | |||
140 | // ------------------------------------------------------------------------ |
||
141 | |||
142 | /** |
||
143 | * AbstractPosition::loadFile |
||
144 | * |
||
145 | * @param string $filename |
||
146 | * @param string|null $subDir |
||
147 | * |
||
148 | * @return void |
||
149 | */ |
||
150 | abstract public function loadFile($filename, $subDir = null); |
||
151 | |||
152 | // ------------------------------------------------------------------------ |
||
153 | |||
154 | /** |
||
155 | * AbstractPosition::publishFile |
||
156 | * |
||
157 | * @param $filePath |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | protected function publishFile($filePath) |
||
247 | ]; |
||
248 | } |
||
249 | |||
250 | // ------------------------------------------------------------------------ |
||
251 | |||
252 | /** |
||
253 | * AbstractPosition::bundleFile |
||
254 | * |
||
255 | * @param string $filename |
||
256 | * @param array $sources |
||
257 | * |
||
258 | * @return array |
||
259 | */ |
||
260 | protected function bundleFile($filename, array $sources) |
||
261 | { |
||
262 | $sourcesContent = []; |
||
263 | foreach ($sources as $key => $source) { |
||
264 | $content = file_get_contents($source); |
||
265 | |||
266 | if ( ! empty($content)) { |
||
267 | $sourcesContent[] = $content; |
||
268 | } else { |
||
269 | unset($sources[ $key ]); |
||
270 | } |
||
271 | } |
||
272 | |||
273 | $fileContent = implode(PHP_EOL, $sourcesContent); |
||
274 | $fileVersion = $this->getVersion($fileContent); |
||
275 | |||
276 | $publicFilePath = PATH_PUBLIC . $filename; |
||
277 | $filename = pathinfo($publicFilePath, PATHINFO_BASENAME); |
||
278 | |||
279 | $publicFileDir = dirname($publicFilePath) . DIRECTORY_SEPARATOR . 'bundled' . DIRECTORY_SEPARATOR; |
||
280 | $publicFilePath = $publicFileDir . $filename; |
||
281 | |||
282 | $extension = pathinfo($publicFilePath, PATHINFO_EXTENSION); |
||
283 | |||
284 | $publicMinifyFilePath = str_replace('.' . $extension, '.min.' . $extension, $publicFilePath); |
||
285 | |||
286 | if ( ! empty($sourcesContent)) { |
||
287 | if (is_file($mapFilePath = $publicFilePath . '.map')) { |
||
288 | $mapMetadata = json_decode(file_get_contents($mapFilePath), true); |
||
289 | // if the file version is changed delete it first |
||
290 | if ( ! hash_equals($fileVersion, $mapMetadata[ 'version' ])) { |
||
291 | unlink($publicFilePath); |
||
292 | unlink($publicMinifyFilePath); |
||
293 | unlink($mapFilePath); |
||
294 | } |
||
295 | } |
||
296 | |||
297 | if ( ! is_file($mapFilePath)) { |
||
298 | if ( ! empty($fileContent)) { |
||
299 | $mapMetadata = [ |
||
300 | 'version' => $fileVersion, |
||
301 | 'sources' => $sources, |
||
302 | 'names' => [], |
||
303 | 'mappings' => [], |
||
304 | 'file' => pathinfo($publicMinifyFilePath, PATHINFO_BASENAME), |
||
305 | 'sourcesContent' => $sourcesContent, |
||
306 | 'sourceRoot' => '', |
||
307 | ]; |
||
308 | |||
309 | if ( ! is_writable($publicFileDir)) { |
||
310 | @mkdir($publicFileDir, 0777, true); |
||
311 | } |
||
312 | |||
313 | if (is_writable($publicFileDir)) { |
||
314 | if ($fileStream = @fopen($publicFilePath, 'ab')) { |
||
315 | flock($fileStream, LOCK_EX); |
||
316 | fwrite($fileStream, $fileContent); |
||
317 | flock($fileStream, LOCK_UN); |
||
318 | fclose($fileStream); |
||
319 | |||
320 | // File Map |
||
321 | if ($fileStream = @fopen($mapFilePath, 'ab')) { |
||
322 | flock($fileStream, LOCK_EX); |
||
323 | |||
324 | fwrite($fileStream, json_encode($mapMetadata)); |
||
325 | |||
326 | flock($fileStream, LOCK_UN); |
||
327 | fclose($fileStream); |
||
328 | } |
||
329 | |||
330 | switch ($extension) { |
||
331 | case 'min.css': |
||
332 | case 'css': |
||
333 | $minifyStyleHandler = new CSS($publicFilePath); |
||
334 | $minifyStyleHandler->minify($publicMinifyFilePath); |
||
335 | break; |
||
336 | |||
337 | case 'min.js': |
||
338 | case 'js': |
||
339 | $minifyJavascriptHandler = new JS($publicFilePath); |
||
340 | $minifyJavascriptHandler->minify($publicMinifyFilePath); |
||
341 | break; |
||
342 | } |
||
343 | |||
344 | } |
||
345 | } |
||
346 | } |
||
347 | } |
||
348 | } |
||
349 | |||
350 | return [ |
||
351 | 'filePath' => $publicFilePath, |
||
352 | 'url' => $this->getUrl($publicFilePath), |
||
353 | 'minify' => [ |
||
354 | 'filePath' => $publicMinifyFilePath, |
||
355 | 'url' => $this->getUrl($publicMinifyFilePath), |
||
356 | ], |
||
357 | 'version' => $fileVersion, |
||
358 | ]; |
||
359 | } |
||
360 | |||
361 | // ------------------------------------------------------------------------ |
||
362 | |||
363 | /** |
||
364 | * AbstractPosition::getFilePath |
||
365 | * |
||
366 | * @param string $filename |
||
367 | * @param string|null $subDir |
||
368 | * |
||
369 | * @return string |
||
370 | */ |
||
371 | protected function getFilePath($filename, $subDir = null) |
||
372 | { |
||
373 | $directories = presenter()->assets->getFilePaths(); |
||
374 | |||
375 | foreach ($directories as $directory) { |
||
376 | |||
377 | /** |
||
378 | * Try with sub directory |
||
379 | * find from public directory first then resource directory |
||
380 | */ |
||
381 | if (isset($subDir)) { |
||
382 | $subDir = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $subDir); |
||
383 | |||
384 | if (is_file($filePath = str_replace(PATH_RESOURCES, PATH_PUBLIC, |
||
385 | $directory) . $subDir . $filename)) { |
||
386 | return $filePath; |
||
387 | break; |
||
388 | } elseif (is_file($filePath = str_replace(PATH_RESOURCES, PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR, |
||
389 | $directory) . $subDir . $filename)) { |
||
390 | return $filePath; |
||
391 | break; |
||
392 | } elseif (is_file($filePath = $directory . $subDir . $filename)) { |
||
393 | return $filePath; |
||
394 | break; |
||
395 | } |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Try without sub directory |
||
400 | * find from public directory first then resource directory |
||
401 | */ |
||
402 | if (is_file($filePath = str_replace(PATH_RESOURCES, PATH_PUBLIC, $directory) . $filename)) { |
||
403 | return $filePath; |
||
404 | break; |
||
405 | } elseif (is_file($filePath = str_replace(PATH_RESOURCES, PATH_PUBLIC . 'assets' . DIRECTORY_SEPARATOR, |
||
406 | $directory) . $filename)) { |
||
407 | return $filePath; |
||
408 | break; |
||
409 | } elseif (is_file($filePath = $directory . $filename)) { |
||
410 | return $filePath; |
||
411 | break; |
||
412 | } |
||
413 | } |
||
414 | |||
415 | return false; |
||
416 | } |
||
417 | |||
418 | // ------------------------------------------------------------------------ |
||
419 | |||
420 | /** |
||
421 | * AbstractPosition::getVersion |
||
422 | * |
||
423 | * @param string $code |
||
424 | * |
||
425 | * @return string |
||
426 | */ |
||
427 | public function getVersion($codeSerialize) |
||
439 | } |
||
440 | |||
441 | // ------------------------------------------------------------------------ |
||
442 | |||
443 | /** |
||
444 | * AbstractPosition::__toString |
||
445 | * |
||
446 | * @return string |
||
447 | */ |
||
448 | abstract public function __toString(); |
||
449 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths