| Total Complexity | 41 |
| Total Lines | 429 |
| 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 | private $type = 'MODULE'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Module::$namespace |
||
| 37 | * |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $namespace; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Module::$segments |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $segments; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Module::$parentSegments |
||
| 51 | * |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $parentSegments; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Module::$properties |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $properties = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Module::$config |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $config = []; |
||
| 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 | $this->properties = $properties; |
||
| 200 | |||
| 201 | return $this; |
||
| 202 | } |
||
| 203 | |||
| 204 | // ------------------------------------------------------------------------ |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Module::getConfig |
||
| 208 | * |
||
| 209 | * @return \O2System\Spl\DataStructures\SplArrayObject |
||
| 210 | */ |
||
| 211 | public function getConfig() |
||
| 212 | { |
||
| 213 | return new SplArrayObject($this->config); |
||
| 214 | } |
||
| 215 | |||
| 216 | // ------------------------------------------------------------------------ |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Module::setConfig |
||
| 220 | * |
||
| 221 | * @param array $config |
||
| 222 | * |
||
| 223 | * @return static |
||
| 224 | */ |
||
| 225 | public function setConfig(array $config) |
||
| 226 | { |
||
| 227 | $this->config = $config; |
||
| 228 | |||
| 229 | return $this; |
||
| 230 | } |
||
| 231 | |||
| 232 | // ------------------------------------------------------------------------ |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Module::getDefaultControllerClassName |
||
| 236 | * |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getDefaultControllerClassName() |
||
| 240 | { |
||
| 241 | return $this->getNamespace() . 'Controllers\\' . $this->getDirName(); |
||
| 242 | } |
||
| 243 | |||
| 244 | // ------------------------------------------------------------------------ |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Module::getNamespace |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getNamespace() |
||
| 252 | { |
||
| 253 | return $this->namespace; |
||
| 254 | } |
||
| 255 | |||
| 256 | // ------------------------------------------------------------------------ |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Module::setNamespace |
||
| 260 | * |
||
| 261 | * @param string $namespace |
||
| 262 | * |
||
| 263 | * @return static |
||
| 264 | */ |
||
| 265 | public function setNamespace($namespace) |
||
| 266 | { |
||
| 267 | $this->namespace = trim($namespace, '\\') . '\\'; |
||
| 268 | |||
| 269 | return $this; |
||
| 270 | } |
||
| 271 | |||
| 272 | // ------------------------------------------------------------------------ |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Module::getThemes |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function getThemes() |
||
| 291 | } |
||
| 292 | |||
| 293 | // ------------------------------------------------------------------------ |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Module::getResourcesDir |
||
| 297 | * |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | public function getResourcesDir() |
||
| 301 | { |
||
| 302 | return PATH_RESOURCES . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
||
| 303 | } |
||
| 304 | |||
| 305 | // ------------------------------------------------------------------------ |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Module::getTheme |
||
| 309 | * |
||
| 310 | * @param string $theme |
||
| 311 | * @param bool $failover |
||
| 312 | * |
||
| 313 | * @return bool|Module\Theme |
||
| 314 | */ |
||
| 315 | public function getTheme($theme, $failover = true) |
||
| 316 | { |
||
| 317 | $theme = dash($theme); |
||
| 318 | |||
| 319 | if ($failover === false) { |
||
| 320 | if (is_dir($themePath = $this->getResourcesDir() . 'themes' . DIRECTORY_SEPARATOR . $theme . DIRECTORY_SEPARATOR)) { |
||
| 321 | $themeObject = new Module\Theme($themePath); |
||
| 322 | |||
| 323 | if ($themeObject->isValid()) { |
||
| 324 | return $themeObject; |
||
| 325 | } |
||
| 326 | } |
||
| 327 | } else { |
||
| 328 | foreach (modules() as $module) { |
||
| 329 | if (in_array($module->getType(), ['KERNEL', 'FRAMEWORK'])) { |
||
| 330 | continue; |
||
| 331 | } elseif ($themeObject = $module->getTheme($theme, false)) { |
||
| 332 | return $themeObject; |
||
| 333 | } |
||
| 334 | } |
||
| 335 | } |
||
| 336 | |||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | // ------------------------------------------------------------------------ |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Module::getPublicDir |
||
| 344 | * |
||
| 345 | * @return string |
||
| 346 | */ |
||
| 347 | public function getPublicDir() |
||
| 348 | { |
||
| 349 | return PATH_PUBLIC . strtolower(str_replace(PATH_APP, '', $this->getRealPath())); |
||
| 350 | } |
||
| 351 | |||
| 352 | // ------------------------------------------------------------------------ |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Module::getType |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getType() |
||
| 360 | { |
||
| 361 | return $this->type; |
||
| 362 | } |
||
| 363 | |||
| 364 | // ------------------------------------------------------------------------ |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Module::setType |
||
| 368 | * |
||
| 369 | * @param string $type |
||
| 370 | * |
||
| 371 | * @return static |
||
| 372 | */ |
||
| 373 | public function setType($type) |
||
| 374 | { |
||
| 375 | $this->type = strtoupper($type); |
||
| 376 | |||
| 377 | return $this; |
||
| 378 | } |
||
| 379 | |||
| 380 | // ------------------------------------------------------------------------ |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Module::getDir |
||
| 384 | * |
||
| 385 | * @param string $dirName |
||
| 386 | * @param bool $psrDir |
||
| 387 | * |
||
| 388 | * @return bool|string |
||
| 389 | */ |
||
| 390 | public function getDir($dirName, $psrDir = false) |
||
| 400 | } |
||
| 401 | |||
| 402 | // ------------------------------------------------------------------------ |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Module::hasTheme |
||
| 406 | * |
||
| 407 | * @param string $theme |
||
| 408 | * |
||
| 409 | * @return bool |
||
| 410 | */ |
||
| 411 | public function hasTheme($theme) |
||
| 412 | { |
||
| 413 | if (is_dir($this->getThemesPath() . $theme)) { |
||
| 414 | return true; |
||
| 415 | } |
||
| 416 | |||
| 417 | return false; |
||
| 418 | } |
||
| 419 | |||
| 420 | // ------------------------------------------------------------------------ |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Module::getThemesPath |
||
| 424 | * |
||
| 425 | * @return string |
||
| 426 | */ |
||
| 427 | public function getThemesPath() |
||
| 430 | } |
||
| 431 | |||
| 432 | // ------------------------------------------------------------------------ |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Module::getParameter |
||
| 436 | * |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function getParameter() |
||
| 440 | { |
||
| 441 | return snakecase($this->getDirName()); |
||
| 442 | } |
||
| 443 | |||
| 444 | // ------------------------------------------------------------------------ |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Module::loadModel |
||
| 448 | */ |
||
| 449 | public function loadModel() |
||
| 455 | } |
||
| 456 | } |
||
| 457 | } |