Complex classes like App 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 App, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class App { |
||
| 34 | use |
||
| 35 | Singleton, |
||
| 36 | Router, |
||
| 37 | Static_files; |
||
| 38 | const INIT_STATE_METHOD = 'init'; |
||
| 39 | 12 | protected function init () { |
|
| 42 | /** |
||
| 43 | * Executes blocks and module page generation |
||
| 44 | * |
||
| 45 | * @throws ExitException |
||
| 46 | */ |
||
| 47 | 10 | function execute () { |
|
| 48 | 10 | $Config = Config::instance(); |
|
| 49 | 10 | $Request = Request::instance(); |
|
| 50 | 10 | if (!preg_match('/^[0-9a-z_]+$/i', $Request->method)) { |
|
| 51 | throw new ExitException(400); |
||
| 52 | } |
||
| 53 | if ( |
||
| 54 | 10 | $Request->method == 'GET' && |
|
| 55 | 10 | in_array(PHP_SAPI, ['cli', 'cli-server']) |
|
| 56 | ) { |
||
| 57 | 6 | $this->serve_static_files($Request); |
|
| 58 | } |
||
| 59 | 10 | $this->handle_closed_site(!$Config->core['site_mode'], $Request); |
|
| 60 | 8 | if (!$this->check_permission($Request, 'index')) { |
|
| 61 | 2 | throw new ExitException(403); |
|
| 62 | } |
||
| 63 | 6 | Event::instance()->fire('System/App/render/before'); |
|
| 64 | 6 | $this->render($Request); |
|
| 65 | 6 | Event::instance()->fire('System/App/render/after'); |
|
| 66 | 6 | Page::instance()->render(); |
|
| 67 | 6 | } |
|
| 68 | /** |
||
| 69 | * @param bool $closed_site |
||
| 70 | * @param Request $Request |
||
| 71 | * |
||
| 72 | * @throws ExitException |
||
| 73 | */ |
||
| 74 | 10 | protected function handle_closed_site ($closed_site, $Request) { |
|
| 75 | 10 | if (!$closed_site) { |
|
| 76 | 6 | return; |
|
| 77 | } |
||
| 78 | /** |
||
| 79 | * If site is closed |
||
| 80 | */ |
||
| 81 | 4 | if (!$this->allow_closed_site_request($Request)) { |
|
| 82 | 2 | throw new ExitException( |
|
| 83 | [ |
||
| 84 | 2 | get_core_ml_text('closed_title'), |
|
| 85 | 2 | get_core_ml_text('closed_text') |
|
| 86 | ], |
||
| 87 | 2 | 503 |
|
| 88 | ); |
||
| 89 | } |
||
| 90 | /** |
||
| 91 | * Warning about closed site for administrator |
||
| 92 | */ |
||
| 93 | 2 | Page::instance()->warning(get_core_ml_text('closed_title')); |
|
| 94 | 2 | } |
|
| 95 | /** |
||
| 96 | * Check if visitor is allowed to make current request to closed site |
||
| 97 | * |
||
| 98 | * @param Request $Request |
||
| 99 | * |
||
| 100 | * @return bool |
||
| 101 | */ |
||
| 102 | 4 | protected function allow_closed_site_request ($Request) { |
|
| 103 | return |
||
| 104 | 4 | User::instance()->admin() || |
|
| 105 | ( |
||
| 106 | 4 | $Request->api_path && |
|
| 107 | 4 | $Request->current_module == 'System' && |
|
| 108 | 4 | $Request->route == ['profile'] && |
|
| 109 | 4 | $Request->method == 'SIGN_IN' |
|
| 110 | ); |
||
| 111 | } |
||
| 112 | /** |
||
| 113 | * Check whether user allowed to access to specified label |
||
| 114 | * |
||
| 115 | * @param Request $Request |
||
| 116 | * @param string $label |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | 8 | protected function check_permission ($Request, $label) { |
|
| 132 | /** |
||
| 133 | * @param Request $Request |
||
| 134 | * |
||
| 135 | * @throws ExitException |
||
| 136 | */ |
||
| 137 | 6 | protected function render ($Request) { |
|
| 147 | /** |
||
| 148 | * Render page title |
||
| 149 | * |
||
| 150 | * @param Request $Request |
||
| 151 | * @param Page $Page |
||
| 152 | */ |
||
| 153 | 2 | protected function render_title ($Request, $Page) { |
|
| 165 | /** |
||
| 166 | * Blocks rendering |
||
| 167 | * |
||
| 168 | * @param Page $Page |
||
| 169 | */ |
||
| 170 | 2 | protected function render_blocks ($Page) { |
|
| 238 | /** |
||
| 239 | * Check whether to render block or not based on its properties (active state, when start to show, when it expires and permissions) |
||
| 240 | * |
||
| 241 | * @param array $block |
||
| 242 | * |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | protected function should_block_be_rendered ($block) { |
||
| 255 | /** |
||
| 256 | * @param string $text |
||
| 257 | * |
||
| 258 | * @return string |
||
| 259 | */ |
||
| 260 | protected function ml_process ($text) { |
||
| 263 | /** |
||
| 264 | * Getter for `controller_path` property (no other properties supported currently) |
||
| 265 | * |
||
| 266 | * @param string $property |
||
| 267 | * |
||
| 268 | * @return false|string[] |
||
| 269 | */ |
||
| 270 | 4 | function __get ($property) { |
|
| 276 | } |
||
| 277 |