Complex classes like Index 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 Index, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Index { |
||
| 27 | use |
||
| 28 | Singleton, |
||
| 29 | Router; |
||
| 30 | |||
| 31 | protected $working_directory; |
||
| 32 | /** |
||
| 33 | * Executes plugins processing, blocks and module page generation |
||
| 34 | * |
||
| 35 | * @todo make public |
||
| 36 | * |
||
| 37 | * @throws ExitException |
||
| 38 | */ |
||
| 39 | protected function process_request () { |
||
| 40 | $Config = Config::instance(); |
||
| 41 | $Request = Request::instance(); |
||
| 42 | $this->working_directory = ''; |
||
| 43 | $this->controller_path = ['index']; |
||
| 44 | $this->handle_closed_site(!$Config->core['site_mode'], $Request); |
||
| 45 | $this->working_directory = MODULES."/$Request->current_module"; |
||
| 46 | if ($Request->admin_path) { |
||
| 47 | $this->working_directory .= '/admin'; |
||
| 48 | } elseif ($Request->api_path) { |
||
| 49 | $this->working_directory .= '/api'; |
||
| 50 | } |
||
| 51 | if (!is_dir($this->working_directory)) { |
||
| 52 | throw new ExitException(404); |
||
| 53 | } |
||
| 54 | if (!$this->check_permission('index')) { |
||
| 55 | throw new ExitException(403); |
||
| 56 | } |
||
| 57 | Event::instance()->fire('System/Index/construct'); |
||
| 58 | /** |
||
| 59 | * Plugins processing |
||
| 60 | */ |
||
| 61 | foreach ($Config->components['plugins'] as $plugin) { |
||
| 62 | _include(PLUGINS."/$plugin/index.php", false, false); |
||
| 63 | } |
||
| 64 | _include("$this->working_directory/prepare.php", false, false); |
||
| 65 | if (!preg_match('/^[a-z_]+$/', strtolower($Request->method))) { |
||
| 66 | throw new ExitException(400); |
||
| 67 | } |
||
| 68 | Event::instance()->fire('System/Index/load/before'); |
||
| 69 | /** |
||
| 70 | * Title only for non-API calls |
||
| 71 | */ |
||
| 72 | $Request->api_path || $this->render_title(); |
||
| 73 | $this->render_content(); |
||
| 74 | /** |
||
| 75 | * Blocks only for non-API calls |
||
| 76 | */ |
||
| 77 | $Request->api_path || $this->render_blocks(); |
||
| 78 | Event::instance()->fire('System/Index/load/after'); |
||
| 79 | } |
||
| 80 | /** |
||
| 81 | * @param bool $closed_site |
||
| 82 | * @param Request $Request |
||
| 83 | * |
||
| 84 | * @throws ExitException |
||
| 85 | */ |
||
| 86 | protected function handle_closed_site ($closed_site, $Request) { |
||
| 87 | if (!$closed_site) { |
||
| 88 | return; |
||
| 89 | } |
||
| 90 | /** |
||
| 91 | * If site is closed |
||
| 92 | */ |
||
| 93 | if (!$this->allow_closed_site_request($Request)) { |
||
| 94 | throw new ExitException( |
||
| 95 | [ |
||
| 96 | get_core_ml_text('closed_title'), |
||
| 97 | get_core_ml_text('closed_text') |
||
| 98 | ], |
||
| 99 | 503 |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | /** |
||
| 103 | * Warning about closed site for administrator |
||
| 104 | */ |
||
| 105 | Page::instance()->warning(get_core_ml_text('closed_title')); |
||
| 106 | } |
||
| 107 | /** |
||
| 108 | * Check if visitor is allowed to make current request to closed site |
||
| 109 | * |
||
| 110 | * @param Request $Request |
||
| 111 | * |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | protected function allow_closed_site_request ($Request) { |
||
| 123 | /** |
||
| 124 | * Check whether user allowed to access to specified label |
||
| 125 | * |
||
| 126 | * @param string $label |
||
| 127 | * |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | protected function check_permission ($label) { |
||
| 140 | /** |
||
| 141 | * Render page title |
||
| 142 | */ |
||
| 143 | protected function render_title () { |
||
| 159 | /** |
||
| 160 | * Render page content (without blocks, just module content) |
||
| 161 | * |
||
| 162 | * @throws ExitException |
||
| 163 | */ |
||
| 164 | protected function render_content () { |
||
| 177 | /** |
||
| 178 | * Blocks rendering |
||
| 179 | */ |
||
| 180 | protected function render_blocks () { |
||
| 263 | /** |
||
| 264 | * Check whether to render block or not based on its properties (active state, when start to show, when it expires and permissions) |
||
| 265 | * |
||
| 266 | * @param array $block |
||
| 267 | * |
||
| 268 | * @return bool |
||
| 269 | */ |
||
| 270 | protected function should_block_be_rendered ($block) { |
||
| 280 | /** |
||
| 281 | * @param string $text |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | protected function ml_process ($text) { |
||
| 288 | /** |
||
| 289 | * Getter for `controller_path` property (no other properties supported currently) |
||
| 290 | * |
||
| 291 | * @param string $property |
||
| 292 | * |
||
| 293 | * @return false|string[] |
||
| 294 | */ |
||
| 295 | function __get ($property) { |
||
| 302 | /** |
||
| 303 | * Executes plugins processing, blocks and module page generation |
||
| 304 | * |
||
| 305 | * @todo deprecate in favor of `::process_request()` |
||
| 306 | * |
||
| 307 | * @throws ExitException |
||
| 308 | */ |
||
| 309 | function __finish () { |
||
| 312 | } |
||
| 313 |