Total Complexity | 47 |
Total Lines | 181 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like StaticLoader 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 StaticLoader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class StaticLoader extends Module { |
||
13 | public $name = 'StaticLoader'; |
||
14 | public $mimes = []; |
||
15 | |||
16 | public function init() { |
||
17 | $this->mimes = $this->config['mimes']; |
||
18 | } |
||
19 | |||
20 | public function parsePath($path) { |
||
21 | $path = Tools::parsePath($path); |
||
22 | |||
23 | if (strpos($path, '/') === 0) { |
||
24 | $path = substr($path, 1); |
||
25 | } |
||
26 | $app = substr($path, 0, strpos($path, '/')); |
||
27 | |||
28 | if ($app && file_exists(INJI_SYSTEM_DIR . '/program/' . $app)) { |
||
29 | $path = substr($path, strpos($path, '/') + 1); |
||
30 | if (App::$cur->name != $app) { |
||
31 | $scriptApp = new App(); |
||
32 | $scriptApp->name = $app; |
||
33 | $scriptApp->system = true; |
||
34 | $scriptApp->staticPath = "/" . $scriptApp->name . "/static"; |
||
35 | $scriptApp->templatesPath = "/" . $scriptApp->name . "/static/templates"; |
||
36 | $scriptApp->path = INJI_SYSTEM_DIR . '/program/' . $scriptApp->name; |
||
37 | $scriptApp->type = 'app' . ucfirst(strtolower($scriptApp->name)); |
||
38 | $scriptApp->installed = true; |
||
39 | $scriptApp->params = []; |
||
40 | $scriptApp->config = Config::app($scriptApp); |
||
41 | } else { |
||
42 | $scriptApp = App::$cur; |
||
43 | } |
||
44 | } else { |
||
45 | $scriptApp = App::$cur->system ? App::$primary : App::$cur; |
||
46 | } |
||
47 | |||
48 | if (strpos($path, 'static/') !== false && strpos($path, 'static/') <= 1) { |
||
49 | $path = substr($path, strpos($path, 'static') + 7); |
||
50 | } |
||
51 | |||
52 | $type = substr($path, 0, strpos($path, '/')); |
||
53 | switch ($type) { |
||
54 | case 'cache': |
||
55 | return INJI_BASE_DIR . $path; |
||
56 | case 'libs': |
||
57 | return App::$cur->Libs->getPath(array_slice(explode('/', $path), 2)); |
||
58 | case 'templates': |
||
59 | $path = substr($path, strpos($path, '/') + 1); |
||
60 | return $scriptApp->view->templatesPath . '/' . $path; |
||
61 | case 'system': |
||
62 | $path = substr($path, strpos($path, '/') + 1); |
||
63 | return INJI_SYSTEM_DIR . '/static/' . $path; |
||
64 | case 'moduleAsset': |
||
65 | $path = substr($path, strpos($path, '/') + 1); |
||
66 | if (!strpos($path, '/')) { |
||
67 | return false; |
||
68 | } |
||
69 | $module = substr($path, 0, strpos($path, '/')); |
||
70 | |||
71 | if (!$scriptApp->$module) { |
||
72 | return false; |
||
73 | } |
||
74 | $path = substr($path, strpos($path, '/') + 1); |
||
75 | if (is_callable([$module, 'staticCalled'])) { |
||
76 | return $scriptApp->$module->staticCalled($path, $scriptApp->$module->path . '/static/'); |
||
77 | } |
||
78 | return $scriptApp->$module->path . '/static/' . $path; |
||
79 | default: |
||
80 | return $scriptApp->path . '/static/' . $path; |
||
81 | } |
||
82 | } |
||
83 | |||
84 | public function giveFile($file) { |
||
193 | } |
||
194 | } |