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 |
||
32 | class App { |
||
33 | use |
||
34 | Singleton, |
||
35 | Router; |
||
36 | const INIT_STATE_METHOD = 'init'; |
||
37 | 12 | protected function init () { |
|
38 | 12 | $this->init_router(); |
|
39 | 12 | } |
|
40 | /** |
||
41 | * Executes blocks and module page generation |
||
42 | * |
||
43 | * @throws ExitException |
||
44 | */ |
||
45 | 10 | function execute () { |
|
46 | 10 | $Config = Config::instance(); |
|
47 | 10 | $Request = Request::instance(); |
|
48 | 10 | if (!preg_match('/^[0-9a-z_]+$/i', $Request->method)) { |
|
49 | throw new ExitException(400); |
||
50 | } |
||
51 | 10 | $this->handle_closed_site(!$Config->core['site_mode'], $Request); |
|
52 | 8 | if (!$this->check_permission($Request, 'index')) { |
|
53 | 2 | throw new ExitException(403); |
|
54 | } |
||
55 | 6 | Event::instance()->fire('System/App/render/before'); |
|
56 | 6 | $this->render($Request); |
|
57 | 6 | Event::instance()->fire('System/App/render/after'); |
|
58 | 6 | Page::instance()->render(); |
|
59 | 6 | } |
|
60 | /** |
||
61 | * @param bool $closed_site |
||
62 | * @param Request $Request |
||
63 | * |
||
64 | * @throws ExitException |
||
65 | */ |
||
66 | 10 | protected function handle_closed_site ($closed_site, $Request) { |
|
87 | /** |
||
88 | * Check if visitor is allowed to make current request to closed site |
||
89 | * |
||
90 | * @param Request $Request |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | protected function allow_closed_site_request ($Request) { |
||
95 | return |
||
96 | User::instance()->admin() || |
||
97 | ( |
||
98 | $Request->api_path && |
||
99 | $Request->current_module == 'System' && |
||
100 | $Request->route == ['profile'] && |
||
101 | $Request->method == 'SIGN_IN' |
||
102 | ); |
||
103 | } |
||
104 | /** |
||
105 | * Check whether user allowed to access to specified label |
||
106 | * |
||
107 | * @param Request $Request |
||
108 | * @param string $label |
||
109 | * |
||
110 | * @return bool |
||
111 | */ |
||
112 | 8 | protected function check_permission ($Request, $label) { |
|
124 | /** |
||
125 | * @param Request $Request |
||
126 | * |
||
127 | * @throws ExitException |
||
128 | */ |
||
129 | protected function render ($Request) { |
||
130 | $Page = Page::instance(); |
||
131 | if ($Request->cli_path || $Request->api_path || !$Page->interface) { |
||
132 | $this->execute_router($Request); |
||
133 | } else { |
||
134 | $this->render_title($Request, $Page); |
||
135 | $this->execute_router($Request); |
||
136 | $this->render_blocks($Page); |
||
137 | } |
||
138 | } |
||
139 | /** |
||
140 | * Render page title |
||
141 | * |
||
142 | * @param Request $Request |
||
143 | * @param Page $Page |
||
144 | */ |
||
145 | protected function render_title ($Request, $Page) { |
||
146 | /** |
||
147 | * Add generic Home or Module name title |
||
148 | */ |
||
149 | $L = Language::instance(); |
||
150 | if ($Request->admin_path) { |
||
151 | $Page->title($L->system_admin_administration); |
||
152 | } |
||
153 | $Page->title( |
||
154 | $L->{$Request->home_page ? 'system_home' : $Request->current_module} |
||
155 | ); |
||
156 | } |
||
157 | /** |
||
158 | * Blocks rendering |
||
159 | * |
||
160 | * @param Page $Page |
||
161 | */ |
||
162 | protected function render_blocks ($Page) { |
||
163 | $blocks = Config::instance()->components['blocks']; |
||
164 | /** |
||
165 | * It is frequent that there is no blocks - so, no need to to anything here |
||
166 | */ |
||
167 | if (!$blocks) { |
||
168 | return; |
||
169 | } |
||
170 | $blocks_array = [ |
||
171 | 'top' => '', |
||
172 | 'left' => '', |
||
173 | 'right' => '', |
||
174 | 'bottom' => '' |
||
175 | ]; |
||
176 | foreach ($blocks as $block) { |
||
177 | /** |
||
178 | * If there is no need to show block or it was rendered by even handler - skip further processing |
||
179 | */ |
||
180 | if ( |
||
181 | !$this->should_block_be_rendered($block) || |
||
182 | !Event::instance()->fire( |
||
183 | 'System/Index/block_render', |
||
184 | [ |
||
185 | 'index' => $block['index'], |
||
186 | 'blocks_array' => &$blocks_array |
||
187 | ] |
||
188 | ) || |
||
189 | !Event::instance()->fire( |
||
190 | 'System/App/block_render', |
||
191 | [ |
||
192 | 'index' => $block['index'], |
||
193 | 'blocks_array' => &$blocks_array |
||
194 | ] |
||
195 | ) |
||
196 | ) { |
||
197 | /** |
||
198 | * Block was rendered by event handler |
||
199 | */ |
||
200 | continue; |
||
201 | } |
||
202 | $block['title'] = $this->ml_process($block['title']); |
||
203 | switch ($block['type']) { |
||
204 | default: |
||
205 | $block['content'] = ob_wrapper( |
||
206 | function () use ($block) { |
||
207 | include BLOCKS."/block.$block[type].php"; |
||
208 | } |
||
209 | ); |
||
210 | break; |
||
211 | case 'html': |
||
212 | case 'raw_html': |
||
213 | $block['content'] = $this->ml_process($block['content']); |
||
214 | break; |
||
215 | } |
||
216 | if ($block['position'] == 'floating') { |
||
217 | $Page->replace( |
||
218 | "<!--block#$block[index]-->", |
||
219 | $block['content'] |
||
220 | ); |
||
221 | } else { |
||
222 | $blocks_array[$block['position']] .= $block['content']; |
||
223 | } |
||
224 | } |
||
225 | $Page->Top .= $blocks_array['top']; |
||
226 | $Page->Left .= $blocks_array['left']; |
||
227 | $Page->Right .= $blocks_array['right']; |
||
228 | $Page->Bottom .= $blocks_array['bottom']; |
||
229 | } |
||
230 | /** |
||
231 | * Check whether to render block or not based on its properties (active state, when start to show, when it expires and permissions) |
||
232 | * |
||
233 | * @param array $block |
||
234 | * |
||
235 | * @return bool |
||
236 | */ |
||
237 | protected function should_block_be_rendered ($block) { |
||
247 | /** |
||
248 | * @param string $text |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | protected function ml_process ($text) { |
||
255 | /** |
||
256 | * Getter for `controller_path` property (no other properties supported currently) |
||
257 | * |
||
258 | * @param string $property |
||
259 | * |
||
260 | * @return false|string[] |
||
261 | */ |
||
262 | function __get ($property) { |
||
268 | } |
||
269 |