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