Complex classes like builder 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 builder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class builder |
||
16 | { |
||
17 | /** @var \phpbb\auth\auth */ |
||
18 | protected $auth; |
||
19 | |||
20 | /** @var \phpbb\event\dispatcher_interface */ |
||
21 | protected $phpbb_dispatcher; |
||
22 | |||
23 | /** @var \phpbb\language\language */ |
||
24 | protected $language; |
||
25 | |||
26 | /** @var \phpbb\request\request_interface */ |
||
27 | protected $request; |
||
28 | |||
29 | /** @var \phpbb\template\context */ |
||
30 | protected $template_context; |
||
31 | |||
32 | /** @var \phpbb\user */ |
||
33 | protected $user; |
||
34 | |||
35 | /* @var \blitze\content\services\fields */ |
||
36 | protected $fields; |
||
37 | |||
38 | /* @var \blitze\content\services\types */ |
||
39 | protected $types; |
||
40 | |||
41 | /** @var \blitze\content\services\form\form */ |
||
42 | protected $form; |
||
43 | |||
44 | /** @var string */ |
||
45 | protected $phpbb_root_path; |
||
46 | |||
47 | /** @var string */ |
||
48 | protected $php_ext; |
||
49 | |||
50 | /** @var bool */ |
||
51 | protected $req_approval = false; |
||
52 | |||
53 | /** @var bool */ |
||
54 | protected $req_mod_input = false; |
||
55 | |||
56 | /** @var bool */ |
||
57 | protected $user_is_mod = false; |
||
58 | |||
59 | /** @var array */ |
||
60 | protected $content_fields = array(); |
||
61 | |||
62 | /** |
||
63 | * Constructor |
||
64 | * |
||
65 | * @param \phpbb\auth\auth $auth Auth object |
||
66 | * @param \phpbb\event\dispatcher_interface $phpbb_dispatcher Event dispatcher object |
||
67 | * @param \phpbb\language\language $language Language object |
||
68 | * @param \phpbb\request\request_interface $request Request object |
||
69 | * @param \phpbb\template\context $template_context Template context object |
||
70 | * @param \phpbb\user $user User object |
||
71 | * @param \blitze\content\services\fields $fields Content fields object |
||
72 | * @param \blitze\content\services\types $types Content types object |
||
73 | * @param \blitze\content\services\form\form $form Form object |
||
74 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
75 | * @param string $php_ext php file extension |
||
76 | */ |
||
77 | public function __construct(\phpbb\auth\auth $auth, \phpbb\event\dispatcher_interface $phpbb_dispatcher, \phpbb\language\language $language, \phpbb\request\request_interface $request, \phpbb\template\context $template_context, \phpbb\user $user, \blitze\content\services\fields $fields, \blitze\content\services\types $types, \blitze\content\services\form\form $form, $phpbb_root_path, $php_ext) |
||
91 | |||
92 | /** |
||
93 | * @param int $forum_id |
||
94 | * @param int $topic_id |
||
95 | * @param string $mode |
||
96 | * @param bool $save_draft |
||
97 | * @return string|false |
||
98 | */ |
||
99 | public function init($forum_id, $topic_id, $mode, $save_draft) |
||
132 | |||
133 | /** |
||
134 | * @return string |
||
135 | */ |
||
136 | public function generate_message() |
||
149 | |||
150 | /** |
||
151 | * @param int $topic_id |
||
152 | * @return void |
||
153 | */ |
||
154 | public function save_db_fields($topic_id) |
||
158 | |||
159 | /** |
||
160 | * @return array |
||
161 | */ |
||
162 | public function get_errors() |
||
166 | |||
167 | /** |
||
168 | * @return string |
||
169 | */ |
||
170 | public function get_cp_url() |
||
174 | |||
175 | /** |
||
176 | * @param string $content_type |
||
177 | * @param array $topic_data |
||
178 | * @return string |
||
179 | */ |
||
180 | public function get_post_url($content_type, array $topic_data) |
||
184 | |||
185 | /** |
||
186 | * @param string $content_type |
||
187 | * @param array $post_data |
||
188 | * @param string $view |
||
189 | * @return string |
||
190 | */ |
||
191 | public function get_content_view($content_type, array $post_data, $view) |
||
202 | |||
203 | /** |
||
204 | * @param string $content_type |
||
205 | * @param array $post_data |
||
206 | * @return string |
||
207 | */ |
||
208 | public function generate_preview($content_type, array $post_data) |
||
215 | |||
216 | /** |
||
217 | * @param array $sql_data |
||
218 | * @return void |
||
219 | */ |
||
220 | public function modify_posting_data(array &$sql_data) |
||
239 | |||
240 | /** |
||
241 | * @param int $topic_id |
||
242 | * @param array $post_data |
||
243 | * @param array $page_data |
||
244 | * @return string |
||
245 | */ |
||
246 | public function generate_form($topic_id, array &$post_data, array $page_data = array()) |
||
265 | |||
266 | /** |
||
267 | * @param string $field |
||
268 | * @param array $field_data |
||
269 | * @param int $topic_id |
||
270 | * @return void |
||
271 | */ |
||
272 | protected function add_field($field, array $field_data, $topic_id) |
||
283 | |||
284 | /** |
||
285 | * @param array $post_data |
||
286 | * @return void |
||
287 | */ |
||
288 | protected function add_moderator_fields(array $post_data) |
||
316 | |||
317 | /** |
||
318 | * @param array $data |
||
319 | * @return void |
||
320 | */ |
||
321 | public function force_visibility(array &$data) |
||
338 | |||
339 | /** |
||
340 | * @return bool |
||
341 | */ |
||
342 | protected function force_state() |
||
346 | |||
347 | /** |
||
348 | * @param int $topic_id |
||
349 | * @param string $post_text |
||
350 | * @return void |
||
351 | */ |
||
352 | protected function set_field_values($topic_id, $post_text) |
||
364 | |||
365 | /** |
||
366 | * Get fields data from post |
||
367 | * |
||
368 | * @param string $post_text |
||
369 | * @param array $fields |
||
370 | * @return array |
||
371 | */ |
||
372 | protected function get_fields_data_from_post($post_text, array $fields) |
||
384 | } |
||
385 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.