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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 declare(strict_types=1); |
||
| 20 | class Module implements \ArrayAccess, iModule |
||
| 21 | { |
||
| 22 | /** Static module instances collection */ |
||
| 23 | public static $instances = array(); |
||
| 24 | |||
| 25 | /** Uniquer identifier to check pointers */ |
||
| 26 | public $uid; |
||
| 27 | |||
| 28 | /** @var ResourcesInterface Pointer to module resource map */ |
||
| 29 | public $resourceMap; |
||
| 30 | |||
| 31 | /** @var array TODO: WTF? */ |
||
| 32 | public $composerParameters = array(); |
||
| 33 | |||
| 34 | /** Module views collection */ |
||
| 35 | protected $views = array(); |
||
| 36 | |||
| 37 | /** Module location */ |
||
| 38 | protected $path = ''; |
||
| 39 | |||
| 40 | /** Unique module identifier */ |
||
| 41 | protected $id; |
||
| 42 | |||
| 43 | /** Path to view for rendering */ |
||
| 44 | protected $view_path = self::VD_POINTER_DEF; |
||
| 45 | |||
| 46 | /** Pointer to view data entry */ |
||
| 47 | protected $data = array(self::VD_POINTER_DEF => array(self::VD_HTML => '')); |
||
| 48 | |||
| 49 | /** Collection of data for view rendering, filled with default pointer */ |
||
| 50 | protected $view_data = array(self::VD_POINTER_DEF => array(self::VD_HTML => '')); |
||
| 51 | |||
| 52 | /** Name of current view context entry */ |
||
| 53 | protected $view_context = self::VD_POINTER_DEF; |
||
| 54 | |||
| 55 | /** Unique module cache path in local web-application */ |
||
| 56 | protected $cache_path; |
||
| 57 | |||
| 58 | /** @var SystemInterface Instance for interaction with framework */ |
||
| 59 | protected $system; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Constructor |
||
| 63 | * |
||
| 64 | * @param string $id Module unique identifier |
||
| 65 | * @param string $path Module location |
||
| 66 | 7 | * @param ResourcesInterface $resourceMap Pointer to module resource map |
|
| 67 | * @param SystemInterface $system |
||
| 68 | */ |
||
| 69 | 7 | public function __construct($id, $path, ResourcesInterface $resourceMap, SystemInterface $system) |
|
| 70 | { |
||
| 71 | // Inject generic module dependencies |
||
| 72 | 7 | $this->system = $system; |
|
| 73 | |||
| 74 | 7 | // Store pointer to module resource map |
|
| 75 | // TODO: Should be changed or removed |
||
| 76 | $this->resourceMap = $resourceMap = ResourceMap::get($path); |
||
| 77 | 7 | // Save views list |
|
| 78 | $this->views = $resourceMap->views; |
||
| 79 | |||
| 80 | 7 | // Set default view context name |
|
| 81 | $this->view_context = self::VD_POINTER_DEF; |
||
| 82 | |||
| 83 | 7 | // Set up default view data pointer |
|
| 84 | $this->data = &$this->view_data[$this->view_context]; |
||
| 85 | |||
| 86 | 7 | // Set module identifier |
|
| 87 | $this->id = $id; |
||
| 88 | |||
| 89 | 7 | // Set path to module |
|
| 90 | $this->path(realpath($path)); |
||
| 91 | |||
| 92 | 7 | // Generate unique module identifier |
|
| 93 | $this->uid = rand(0, 9999999) . '_' . microtime(true); |
||
| 94 | |||
| 95 | 7 | // Add to module identifier to view data stack |
|
| 96 | $this->data['id'] = $this->id; |
||
| 97 | |||
| 98 | // Generate unique module cache path in local web-application |
||
| 99 | 7 | $this->cache_path = __SAMSON_CWD__ . __SAMSON_CACHE_PATH . $this->id . '/'; |
|
| 100 | |||
| 101 | // Save ONLY ONE copy of this instance in static instances collection, |
||
| 102 | 7 | // avoiding rewriting by cloned modules |
|
| 103 | !isset(self::$instances[$this->id]) ? self::$instances[$this->id] = &$this : ''; |
||
| 104 | |||
| 105 | 7 | // Make view path relative to module - remove module path from view path |
|
| 106 | $this->views = str_replace($this->path, '', $this->views); |
||
| 107 | |||
| 108 | 7 | //elapsed('Registering module: '.$this->id.'('.$path.')' ); |
|
| 109 | } |
||
| 110 | |||
| 111 | 7 | /** @see iModule::path() */ |
|
| 112 | 7 | public function path($value = null) |
|
| 122 | |||
| 123 | /** @see iModule::title() */ |
||
| 124 | public function title($title = null) |
||
| 128 | 1 | ||
| 129 | /** @see iModule::set() */ |
||
| 130 | 1 | public function set($value, $field = null) |
|
| 136 | 1 | ||
| 137 | /** @see iModule::id() */ |
||
| 138 | public function id() |
||
| 142 | |||
| 143 | /** @see iModuleViewable::toView() */ |
||
| 144 | public function toView($prefix = null, array $restricted = array()) |
||
| 154 | 1 | ||
| 155 | /** @see iModule::html() */ |
||
| 156 | 1 | public function html($value) |
|
| 162 | 3 | ||
| 163 | public function view($viewPath) |
||
| 186 | |||
| 187 | /** |
||
| 188 | 3 | * Find view file by its part in module view resources and return full path to it. |
|
| 189 | * |
||
| 190 | * @param string $viewPath Part of path to module view file |
||
| 191 | 3 | * @return string Full path to view file |
|
| 192 | */ |
||
| 193 | 3 | public function findView($viewPath) |
|
| 209 | 4 | ||
| 210 | /** |
||
| 211 | * Perform module view context switching |
||
| 212 | 4 | * @param string $view_path New view context name |
|
| 213 | */ |
||
| 214 | protected function viewContext($view_path) |
||
| 252 | 1 | ||
| 253 | /** |
||
| 254 | * @param null $controller |
||
| 255 | * |
||
| 256 | * @throws ControllerActionNotFound |
||
| 257 | 1 | */ |
|
| 258 | 1 | public function render($controller = null) |
|
| 295 | |||
| 296 | /** @see iModule::output() */ |
||
| 297 | public function output() |
||
| 350 | |||
| 351 | /** Magic method for calling un existing object methods */ |
||
| 352 | public function __call($method, $arguments) |
||
| 369 | 1 | ||
| 370 | 1 | // Магический метод для получения переменных представления модуля |
|
| 371 | |||
| 372 | /** Обработчик сериализации объекта */ |
||
| 373 | public function __sleep() |
||
| 377 | 1 | ||
| 378 | 1 | /** Обработчик десериализации объекта */ |
|
| 379 | public function __wakeup() |
||
| 390 | 3 | ||
| 391 | // TODO: Переделать обработчик в одинаковый вид для объектов и простых |
||
| 392 | |||
| 393 | 3 | /** Группа методов для доступа к аттрибутам в виде массива */ |
|
| 394 | 2 | public function offsetSet($value, $offset) |
|
| 398 | |||
| 399 | public function offsetGet($offset) |
||
| 403 | 3 | ||
| 404 | // Магический метод для установки переменных представления модуля |
||
| 405 | |||
| 406 | 3 | public function __get($field) |
|
| 421 | 3 | ||
| 422 | public function __set($value, $field = null) |
||
| 442 | |||
| 443 | public function offsetUnset($offset) |
||
| 447 | |||
| 448 | public function offsetExists($offset) |
||
| 452 | |||
| 453 | /** Sort array by string length */ |
||
| 454 | 1 | protected function sortStrings($a, $b) |
|
| 458 | |||
| 459 | 1 | /** |
|
| 460 | 1 | * Create unique module cache folder structure in local web-application. |
|
| 461 | 1 | * |
|
| 462 | * @param string $file Path to file relative to module cache location |
||
| 463 | * @param boolean $clear Flag to perform generic cache folder clearence |
||
| 464 | 1 | * @return boolean TRUE if cache file has to be regenerated |
|
| 465 | */ |
||
| 466 | protected function cache_refresh(&$file, $clear = true, $folder = null) |
||
| 494 | |||
| 495 | 1 | /** |
|
| 496 | * |
||
| 497 | * @param unknown $object |
||
| 498 | 1 | * @param string $viewprefix |
|
| 499 | */ |
||
| 500 | private function _setObject($object, $viewprefix = null) |
||
| 514 | |||
| 515 | /** |
||
| 516 | * |
||
| 517 | * @param unknown $array |
||
| 518 | * @param string $viewprefix |
||
| 519 | */ |
||
| 520 | private function _setArray($array, $viewprefix = null) |
||
| 528 | } |
||
| 529 |
Adding braces to control structures avoids accidental mistakes as your code changes: