Total Complexity | 54 |
Total Lines | 499 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Module 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 Module, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class Module extends SplDirectoryInfo |
||
27 | { |
||
28 | /** |
||
29 | * Module::$type |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $type = 'MODULE'; |
||
34 | |||
35 | /** |
||
36 | * Module::$namespace |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $namespace; |
||
41 | |||
42 | /** |
||
43 | * Module::$segments |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $segments; |
||
48 | |||
49 | /** |
||
50 | * Module::$parentSegments |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $parentSegments; |
||
55 | |||
56 | /** |
||
57 | * Module::$properties |
||
58 | * |
||
59 | * @var array |
||
60 | */ |
||
61 | protected $properties = []; |
||
62 | |||
63 | /** |
||
64 | * Module::$presets |
||
65 | * |
||
66 | * @var array |
||
67 | */ |
||
68 | protected $presets = []; |
||
69 | |||
70 | // ------------------------------------------------------------------------ |
||
71 | |||
72 | /** |
||
73 | * Module::__construct |
||
74 | * |
||
75 | * @param string $dir |
||
76 | */ |
||
77 | public function __construct($dir) |
||
78 | { |
||
79 | parent::__construct($dir); |
||
80 | $this->namespace = prepare_namespace(str_replace(PATH_ROOT, '', $dir), false); |
||
|
|||
81 | } |
||
82 | |||
83 | // ------------------------------------------------------------------------ |
||
84 | |||
85 | /** |
||
86 | * Module::getSegments |
||
87 | * |
||
88 | * @param bool $returnArray |
||
89 | * |
||
90 | * @return array|string |
||
91 | */ |
||
92 | public function getSegments($returnArray = true) |
||
93 | { |
||
94 | if ($returnArray) { |
||
95 | return explode('/', $this->segments); |
||
96 | } |
||
97 | |||
98 | return $this->segments; |
||
99 | } |
||
100 | |||
101 | // ------------------------------------------------------------------------ |
||
102 | |||
103 | /** |
||
104 | * Module::setSegments |
||
105 | * |
||
106 | * @param array|string $segments |
||
107 | * |
||
108 | * @return static |
||
109 | */ |
||
110 | public function setSegments($segments) |
||
115 | } |
||
116 | |||
117 | // ------------------------------------------------------------------------ |
||
118 | |||
119 | /** |
||
120 | * Module::getParentSegments |
||
121 | * |
||
122 | * @param bool $returnArray |
||
123 | * |
||
124 | * @return array|string |
||
125 | */ |
||
126 | public function getParentSegments($returnArray = true) |
||
133 | } |
||
134 | |||
135 | // ------------------------------------------------------------------------ |
||
136 | |||
137 | /** |
||
138 | * Module::setParentSegments |
||
139 | * |
||
140 | * @param array|string $parentSegments |
||
141 | * |
||
142 | * @return static |
||
143 | */ |
||
144 | public function setParentSegments($parentSegments) |
||
145 | { |
||
146 | $this->parentSegments = is_array($parentSegments) ? implode('/', $parentSegments) : $parentSegments; |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | // ------------------------------------------------------------------------ |
||
152 | |||
153 | /** |
||
154 | * Module::getCode |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getCode() |
||
159 | { |
||
160 | return strtoupper(substr(md5(spl_object_hash($this)), 2, 7)); |
||
161 | } |
||
162 | |||
163 | // ------------------------------------------------------------------------ |
||
164 | |||
165 | /** |
||
166 | * Module::getChecksum |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function getChecksum() |
||
171 | { |
||
172 | return md5($this->getMTime()); |
||
173 | } |
||
174 | |||
175 | // ------------------------------------------------------------------------ |
||
176 | |||
177 | /** |
||
178 | * Module::getProperties |
||
179 | * |
||
180 | * @return \O2System\Spl\DataStructures\SplArrayObject |
||
181 | */ |
||
182 | public function getProperties() |
||
185 | } |
||
186 | |||
187 | // ------------------------------------------------------------------------ |
||
188 | |||
189 | /** |
||
190 | * Module::setProperties |
||
191 | * |
||
192 | * @param array $properties |
||
193 | * |
||
194 | * @return static |
||
195 | */ |
||
196 | public function setProperties(array $properties) |
||
197 | { |
||
198 | if (isset($properties[ 'presets' ])) { |
||
199 | $this->setPresets($properties[ 'presets' ]); |
||
200 | |||
201 | unset($properties[ 'presets' ]); |
||
202 | } |
||
203 | |||
204 | $this->properties = $properties; |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | // ------------------------------------------------------------------------ |
||
210 | |||
211 | /** |
||
212 | * Module::getPresets |
||
213 | * |
||
214 | * @return \O2System\Spl\DataStructures\SplArrayObject |
||
215 | */ |
||
216 | public function getPresets() |
||
217 | { |
||
218 | return new SplArrayObject($this->presets); |
||
219 | } |
||
220 | |||
221 | // ------------------------------------------------------------------------ |
||
222 | |||
223 | /** |
||
224 | * Module::setPresets |
||
225 | * |
||
226 | * @param array $presets |
||
227 | * |
||
228 | * @return static |
||
229 | */ |
||
230 | public function setPresets(array $presets) |
||
231 | { |
||
232 | $this->presets = $presets; |
||
233 | |||
234 | return $this; |
||
235 | } |
||
236 | |||
237 | // ------------------------------------------------------------------------ |
||
238 | |||
239 | /** |
||
240 | * Module::getDefaultControllerClassName |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | public function getDefaultControllerClassName() |
||
245 | { |
||
246 | return $this->getNamespace() . 'Controllers\\' . $this->getDirName(); |
||
247 | } |
||
248 | |||
249 | // ------------------------------------------------------------------------ |
||
250 | |||
251 | /** |
||
252 | * Module::getNamespace |
||
253 | * |
||
254 | * @return string |
||
255 | */ |
||
256 | public function getNamespace() |
||
257 | { |
||
258 | return $this->namespace; |
||
259 | } |
||
260 | |||
261 | // ------------------------------------------------------------------------ |
||
262 | |||
263 | /** |
||
264 | * Module::setNamespace |
||
265 | * |
||
266 | * @param string $namespace |
||
267 | * |
||
268 | * @return static |
||
269 | */ |
||
270 | public function setNamespace($namespace) |
||
271 | { |
||
272 | $this->namespace = trim($namespace, '\\') . '\\'; |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | // ------------------------------------------------------------------------ |
||
278 | |||
279 | /** |
||
280 | * Module::getThemes |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | public function getThemes() |
||
296 | } |
||
297 | |||
298 | // ------------------------------------------------------------------------ |
||
299 | |||
300 | /** |
||
301 | * Module::getResourcesDir |
||
302 | * |
||
303 | * @param string|null $subDir |
||
304 | * |
||
305 | * @return bool|string |
||
306 | */ |
||
307 | public function getResourcesDir($subDir = null) |
||
308 | { |
||
309 | $dirResources = PATH_RESOURCES; |
||
310 | |||
311 | $dirPath = str_replace(PATH_APP, '', $this->getRealPath()); |
||
312 | $dirPathParts = explode(DIRECTORY_SEPARATOR, $dirPath); |
||
313 | |||
314 | if (count($dirPathParts)) { |
||
315 | $dirPathParts = array_map('dash', $dirPathParts); |
||
316 | $dirResources .= implode(DIRECTORY_SEPARATOR, $dirPathParts); |
||
317 | } |
||
318 | |||
319 | if (is_null($subDir)) { |
||
320 | return $dirResources; |
||
321 | } elseif (is_dir($dirResources . $subDir . DIRECTORY_SEPARATOR)) { |
||
322 | return $dirResources . $subDir . DIRECTORY_SEPARATOR; |
||
323 | } |
||
324 | |||
325 | return false; |
||
326 | } |
||
327 | |||
328 | // ------------------------------------------------------------------------ |
||
329 | |||
330 | /** |
||
331 | * Module::getTheme |
||
332 | * |
||
333 | * @param string $theme |
||
334 | * @param bool $failover |
||
335 | * |
||
336 | * @return bool|Module\Theme |
||
337 | */ |
||
338 | public function getTheme($theme, $failover = true) |
||
339 | { |
||
340 | $theme = dash($theme); |
||
341 | |||
342 | if ($failover === false) { |
||
343 | if (is_dir($themePath = $this->getResourcesDir('themes') . $theme . DIRECTORY_SEPARATOR)) { |
||
344 | $themeObject = new Module\Theme($themePath); |
||
345 | |||
346 | if ($themeObject->isValid()) { |
||
347 | return $themeObject; |
||
348 | } |
||
349 | } |
||
350 | } else { |
||
351 | foreach (modules() as $module) { |
||
352 | if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
||
353 | continue; |
||
354 | } elseif ($themeObject = $module->getTheme($theme, false)) { |
||
355 | return $themeObject; |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | |||
360 | return false; |
||
361 | } |
||
362 | |||
363 | // ------------------------------------------------------------------------ |
||
364 | |||
365 | /** |
||
366 | * Module::getPublicDir |
||
367 | * |
||
368 | * @return string |
||
369 | */ |
||
370 | public function getPublicDir() |
||
371 | { |
||
372 | return PATH_PUBLIC . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
||
373 | } |
||
374 | |||
375 | // ------------------------------------------------------------------------ |
||
376 | |||
377 | /** |
||
378 | * Module::getType |
||
379 | * |
||
380 | * @return string |
||
381 | */ |
||
382 | public function getType() |
||
383 | { |
||
384 | return $this->type; |
||
385 | } |
||
386 | |||
387 | // ------------------------------------------------------------------------ |
||
388 | |||
389 | /** |
||
390 | * Module::setType |
||
391 | * |
||
392 | * @param string $type |
||
393 | * |
||
394 | * @return static |
||
395 | */ |
||
396 | public function setType($type) |
||
397 | { |
||
398 | $this->type = strtoupper($type); |
||
399 | |||
400 | return $this; |
||
401 | } |
||
402 | |||
403 | // ------------------------------------------------------------------------ |
||
404 | |||
405 | /** |
||
406 | * Module::getDir |
||
407 | * |
||
408 | * @param string $subDir |
||
409 | * @param bool $psrDir |
||
410 | * |
||
411 | * @return bool|string |
||
412 | */ |
||
413 | public function getDir($subDir, $psrDir = false) |
||
423 | } |
||
424 | |||
425 | // ------------------------------------------------------------------------ |
||
426 | |||
427 | /** |
||
428 | * Module::hasTheme |
||
429 | * |
||
430 | * @param string $theme |
||
431 | * |
||
432 | * @return bool |
||
433 | */ |
||
434 | public function hasTheme($theme) |
||
449 | } |
||
450 | |||
451 | // ------------------------------------------------------------------------ |
||
452 | |||
453 | /** |
||
454 | * Module::getThemesDir |
||
455 | * |
||
456 | * @return string |
||
457 | */ |
||
458 | public function getThemesDir() |
||
459 | { |
||
460 | return $this->getResourcesDir('themes'); |
||
461 | } |
||
462 | |||
463 | // ------------------------------------------------------------------------ |
||
464 | |||
465 | /** |
||
466 | * Module::getParameter |
||
467 | * |
||
468 | * @return string |
||
469 | */ |
||
470 | public function getParameter() |
||
471 | { |
||
472 | return snakecase($this->getDirName()); |
||
473 | } |
||
474 | |||
475 | // ------------------------------------------------------------------------ |
||
476 | |||
477 | /** |
||
478 | * Module::loadModel |
||
479 | */ |
||
480 | public function loadModel() |
||
486 | } |
||
487 | } |
||
488 | |||
489 | public function getControllers() |
||
490 | { |
||
491 | $controllers = []; |
||
492 | |||
525 | } |
||
526 | } |