Complex classes like Router 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 Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class Router extends RouterAbstract |
||
21 | { |
||
22 | /** |
||
23 | * route the header |
||
24 | * |
||
25 | * @since 3.3.0 |
||
26 | * |
||
27 | * @return bool |
||
28 | */ |
||
29 | |||
30 | 2 | public function routeHeader() : bool |
|
31 | { |
||
32 | 2 | Module\Hook::trigger('adminRouteHeader'); |
|
33 | 2 | $adminParameter = $this->getAdmin(); |
|
34 | |||
35 | /* handle break */ |
||
36 | |||
37 | 2 | if ($this->_registry->get('adminRouterBreak')) |
|
38 | { |
||
39 | 1 | Header::responseCode(200); |
|
40 | } |
||
41 | |||
42 | /* handle guard */ |
||
43 | |||
44 | 2 | if ($adminParameter && ($this->_tokenGuard() || $this->_authGuard())) |
|
45 | { |
||
46 | Header::responseCode(403); |
||
47 | } |
||
48 | 2 | return (bool)$this->_registry->get('adminRouterBreak'); |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * route the content |
||
53 | * |
||
54 | * @since 3.3.0 |
||
55 | * |
||
56 | * @return string|null |
||
57 | */ |
||
58 | |||
59 | 10 | public function routeContent() : ?string |
|
60 | { |
||
61 | 10 | Module\Hook::trigger('adminRouteContent'); |
|
62 | 10 | $firstParameter = $this->getFirst(); |
|
63 | 10 | $adminParameter = $this->getAdmin(); |
|
64 | 10 | $tableParameter = $this->getTable(); |
|
65 | |||
66 | /* handle admin */ |
||
67 | |||
68 | 10 | if ($firstParameter === 'admin') |
|
69 | { |
||
70 | /* handle break */ |
||
71 | |||
72 | 9 | if ($this->_registry->get('adminRouterBreak')) |
|
73 | { |
||
74 | return '<!-- adminRouterBreak -->'; |
||
75 | } |
||
76 | |||
77 | /* handle guard */ |
||
78 | |||
79 | 9 | if ($adminParameter) |
|
80 | { |
||
81 | 9 | if ($this->_tokenGuard()) |
|
82 | { |
||
83 | 1 | return $this->_errorToken(); |
|
84 | } |
||
85 | 8 | if ($this->_authGuard()) |
|
86 | { |
||
87 | return $this->_errorAccess(); |
||
88 | } |
||
89 | } |
||
90 | |||
91 | /* handle update */ |
||
92 | |||
93 | 8 | if (!$adminParameter || $adminParameter === 'view' && $tableParameter === 'users' || $this->_registry->get('cronUpdate')) |
|
94 | { |
||
95 | $this->_updateLast(); |
||
96 | } |
||
97 | |||
98 | /* handle post */ |
||
99 | |||
100 | 8 | if ($this->_request->getPost('Redaxscript\Admin\View\CategoryForm')) |
|
101 | { |
||
102 | 1 | return $this->_processCategory(); |
|
103 | } |
||
104 | 7 | if ($this->_request->getPost('Redaxscript\Admin\View\ArticleForm')) |
|
105 | { |
||
106 | 1 | return $this->_processArticle(); |
|
107 | } |
||
108 | 6 | if ($this->_request->getPost('Redaxscript\Admin\View\ExtraForm')) |
|
109 | { |
||
110 | 1 | return $this->_processExtra(); |
|
111 | } |
||
112 | 5 | if ($this->_request->getPost('Redaxscript\Admin\View\CommentForm')) |
|
113 | { |
||
114 | 1 | return $this->_processComment(); |
|
115 | } |
||
116 | 4 | if ($this->_request->getPost('Redaxscript\Admin\View\UserForm')) |
|
117 | { |
||
118 | 1 | return $this->_processUser(); |
|
119 | } |
||
120 | 3 | if ($this->_request->getPost('Redaxscript\Admin\View\GroupForm')) |
|
121 | { |
||
122 | 1 | return $this->_processGroup(); |
|
123 | } |
||
124 | 2 | if ($this->_request->getPost('Redaxscript\Admin\View\ModuleForm')) |
|
125 | { |
||
126 | 1 | return $this->_processModule(); |
|
127 | } |
||
128 | 1 | if ($this->_request->getPost('Redaxscript\Admin\View\SettingForm')) |
|
129 | { |
||
130 | 1 | return $this->_processSetting(); |
|
131 | } |
||
132 | |||
133 | /* handle route */ |
||
134 | |||
135 | if ($adminParameter === 'view') |
||
136 | { |
||
137 | return $this->_renderView(); |
||
138 | } |
||
139 | if ($adminParameter === 'new') |
||
140 | { |
||
141 | return $this->_renderNew(); |
||
142 | } |
||
143 | if ($adminParameter === 'edit') |
||
144 | { |
||
145 | return $this->_renderEdit(); |
||
146 | } |
||
147 | return $this->_processCommon(); |
||
148 | } |
||
149 | 1 | return null; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * token guard |
||
154 | * |
||
155 | * @since 3.3.0 |
||
156 | * |
||
157 | * @return bool |
||
158 | */ |
||
159 | |||
160 | 9 | protected function _tokenGuard() : bool |
|
161 | { |
||
162 | 9 | $adminParameter = $this->getAdmin(); |
|
163 | 9 | $tokenParameter = $this->getToken(); |
|
164 | $tokenArray = |
||
165 | [ |
||
166 | 9 | 'enable', |
|
167 | 'disable', |
||
168 | 'publish', |
||
169 | 'unpublish', |
||
170 | 'install', |
||
171 | 'uninstall', |
||
172 | 'delete' |
||
173 | ]; |
||
174 | 9 | return $this->_request->get('post') && $this->_request->getPost('token') !== $this->_registry->get('token') || in_array($adminParameter, $tokenArray) && !$tokenParameter; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * auth guard |
||
179 | * |
||
180 | * @since 3.3.0 |
||
181 | * |
||
182 | * @return bool |
||
183 | */ |
||
184 | |||
185 | 8 | protected function _authGuard() : bool |
|
186 | { |
||
187 | 8 | $adminParameter = $this->getAdmin(); |
|
188 | 8 | $tableParameter = $this->getTable(); |
|
189 | 8 | $idParameter = $this->getId(); |
|
190 | 8 | $myId = (int)$this->_registry->get('myId'); |
|
191 | $editArray = |
||
192 | [ |
||
193 | 8 | 'edit', |
|
194 | 'view', |
||
195 | 'enable', |
||
196 | 'disable', |
||
197 | 'publish', |
||
198 | 'unpublish' |
||
199 | ]; |
||
200 | 8 | $permissionNew = $adminParameter === 'new' && $this->_registry->get('tableNew'); |
|
201 | 8 | $permissionEdit = in_array($adminParameter, $editArray) && $this->_registry->get('tableEdit'); |
|
202 | 8 | $permissionDelete = $adminParameter === 'delete' && $this->_registry->get('tableDelete'); |
|
203 | 8 | $permissionInstall = $adminParameter === 'install' && $this->_registry->get('tableInstall'); |
|
204 | 8 | $permissionUninstall = $adminParameter === 'uninstall' && $this->_registry->get('tableUninstall'); |
|
205 | 8 | $permissionProfile = $tableParameter === 'users' && $idParameter === $myId; |
|
206 | 8 | return !$permissionNew && !$permissionEdit && !$permissionDelete && !$permissionInstall && !$permissionUninstall && !$permissionProfile; |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * update last |
||
211 | * |
||
212 | * @since 4.00 |
||
213 | */ |
||
214 | |||
215 | protected function _updateLast() : void |
||
223 | |||
224 | /** |
||
225 | * process the category |
||
226 | * |
||
227 | * @since 4.00 |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | |||
232 | 1 | protected function _processCategory() : string |
|
237 | |||
238 | /** |
||
239 | * process the article |
||
240 | * |
||
241 | * @since 4.00 |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | |||
246 | 1 | protected function _processArticle() : string |
|
251 | |||
252 | /** |
||
253 | * process the extra |
||
254 | * |
||
255 | * @since 4.00 |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | |||
260 | 1 | protected function _processExtra() : string |
|
265 | |||
266 | /** |
||
267 | * process the comment |
||
268 | * |
||
269 | * @since 4.00 |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | |||
274 | 1 | protected function _processComment() : string |
|
279 | |||
280 | /** |
||
281 | * process the user |
||
282 | * |
||
283 | * @since 4.00 |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | |||
288 | 1 | protected function _processUser() : string |
|
293 | |||
294 | /** |
||
295 | * process the group |
||
296 | * |
||
297 | * @since 4.00 |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | |||
302 | 1 | protected function _processGroup() : string |
|
307 | |||
308 | /** |
||
309 | * process the module |
||
310 | * |
||
311 | * @since 4.00 |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | |||
316 | 1 | protected function _processModule() : string |
|
321 | |||
322 | /** |
||
323 | * process the setting |
||
324 | * |
||
325 | * @since 4.00 |
||
326 | * |
||
327 | * @return string |
||
328 | */ |
||
329 | |||
330 | 1 | protected function _processSetting() : string |
|
335 | |||
336 | /** |
||
337 | * process the common |
||
338 | * |
||
339 | * @since 4.00 |
||
340 | * |
||
341 | * @return string|null |
||
342 | */ |
||
343 | |||
344 | protected function _processCommon() : ?string |
||
364 | |||
365 | /** |
||
366 | * render the view |
||
367 | * |
||
368 | * @since 3.3.0 |
||
369 | * |
||
370 | * @return string|null |
||
371 | */ |
||
372 | |||
373 | protected function _renderView() : ?string |
||
416 | |||
417 | /** |
||
418 | * render the new |
||
419 | * |
||
420 | * @since 3.3.0 |
||
421 | * |
||
422 | * @return string|null |
||
423 | */ |
||
424 | |||
425 | protected function _renderNew() : ?string |
||
463 | |||
464 | /** |
||
465 | * render the edit |
||
466 | * |
||
467 | * @since 3.3.0 |
||
468 | * |
||
469 | * @return string |
||
470 | */ |
||
471 | |||
472 | protected function _renderEdit() : string |
||
528 | |||
529 | /** |
||
530 | * messenger factory |
||
531 | * |
||
532 | * @since 4.0.0 |
||
533 | * |
||
534 | * @return Admin\View\Helper\Messenger |
||
535 | */ |
||
536 | |||
537 | 1 | protected function _messengerFactory() : Admin\View\Helper\Messenger |
|
541 | |||
542 | /** |
||
543 | * show the token error |
||
544 | * |
||
545 | * @since 3.3.0 |
||
546 | * |
||
547 | * @return string |
||
548 | */ |
||
549 | |||
550 | 1 | protected function _errorToken() : string |
|
557 | |||
558 | /** |
||
559 | * show the access error |
||
560 | * |
||
561 | * @since 3.3.0 |
||
562 | * |
||
563 | * @return string |
||
564 | */ |
||
565 | |||
566 | protected function _errorAccess() : string |
||
573 | } |
||
574 |