Complex classes like Controller 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 Controller, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class Controller { |
||
20 | use |
||
21 | admin; |
||
22 | |||
23 | static function __get_settings () { |
||
24 | $User = User::instance(); |
||
25 | $module_data = Config::instance()->module('Blogs'); |
||
26 | $admin = $User->admin() && $User->get_permission('admin/Blogs', 'index'); |
||
27 | return [ |
||
28 | 'inline_editor' => functionality('inline_editor'), |
||
29 | 'max_sections' => $module_data->max_sections, |
||
30 | 'new_posts_only_from_admins' => (bool)$module_data->new_posts_only_from_admins, |
||
31 | 'comments_enabled' => $module_data->enable_comments && functionality('comments'), |
||
32 | 'admin' => $admin, |
||
33 | 'admin_edit' => $admin && $User->get_permission('admin/Blogs', 'edit_post') |
||
34 | ]; |
||
35 | } |
||
36 | /** |
||
37 | * @param \cs\Request $Request |
||
38 | * |
||
39 | * @return array |
||
40 | * |
||
41 | * @throws ExitException |
||
42 | */ |
||
43 | static function posts_get ($Request) { |
||
55 | /** |
||
56 | * @param \cs\Request $Request |
||
57 | * @param \cs\Response $Response |
||
58 | * |
||
59 | * @return array |
||
60 | * |
||
61 | * @throws ExitException |
||
62 | */ |
||
63 | static function posts_post ($Request, $Response) { |
||
89 | /** |
||
90 | * @param \cs\Request $Request |
||
91 | * |
||
92 | * @return array |
||
93 | * |
||
94 | * @throws ExitException |
||
95 | */ |
||
96 | static function posts_put ($Request) { |
||
125 | /** |
||
126 | * @param \cs\Request $Request |
||
127 | * |
||
128 | * @throws ExitException |
||
129 | */ |
||
130 | static function posts_delete ($Request) { |
||
156 | /** |
||
157 | * @param \cs\Request $Request |
||
158 | * @param Prefix $L |
||
159 | * |
||
160 | * @return array |
||
161 | * |
||
162 | * @throws ExitException |
||
163 | */ |
||
164 | protected static function check_request_data ($Request, $L) { |
||
184 | /** |
||
185 | * @param \cs\Request $Request |
||
186 | * |
||
187 | * @return array |
||
188 | * |
||
189 | * @throws ExitException |
||
190 | */ |
||
191 | static function posts_preview ($Request) { |
||
192 | $User = User::instance(); |
||
193 | if (!$User->user()) { |
||
194 | throw new ExitException(403); |
||
195 | } |
||
196 | $data = $Request->data('title', 'sections', 'content', 'tags'); |
||
197 | $data += [ |
||
198 | 'id' => 0, |
||
199 | 'path' => path($Request->data('path') ?: $data['title']), |
||
200 | 'user' => $User->id, |
||
201 | 'date' => 0 |
||
202 | ]; |
||
203 | $Posts = Posts::instance(); |
||
204 | return $Posts->post_to_jsonld($data); |
||
205 | } |
||
206 | /** |
||
207 | * @param \cs\Request $Request |
||
208 | * |
||
209 | * @return array |
||
210 | * |
||
211 | * @throws ExitException |
||
212 | */ |
||
213 | static function sections_get ($Request) { |
||
225 | } |
||
226 |
This check looks for the
else
branches ofif
statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
else
branches can be removed.could be turned into
This is much more concise to read.