Complex classes like helper 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 helper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class helper |
||
13 | { |
||
14 | /** @var \phpbb\auth\auth */ |
||
15 | protected $auth; |
||
16 | |||
17 | /** @var \phpbb\config\db */ |
||
18 | protected $config; |
||
19 | |||
20 | /** @var \phpbb\user */ |
||
21 | protected $user; |
||
22 | |||
23 | /** @var string */ |
||
24 | protected $phpbb_root_path; |
||
25 | |||
26 | /** @var string */ |
||
27 | protected $php_ext; |
||
28 | |||
29 | /** |
||
30 | * Constructor |
||
31 | * |
||
32 | * @param \phpbb\auth\auth $auth Auth object |
||
33 | * @param \phpbb\config\db $config Config object |
||
34 | * @param \phpbb\user $user User object |
||
35 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
36 | * @param string $php_ext php file extension |
||
37 | */ |
||
38 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\db $config, \phpbb\user $user, $phpbb_root_path, $php_ext) |
||
46 | |||
47 | /** |
||
48 | * @param array $post_data |
||
49 | * @param array $topic_data |
||
50 | * @param string $cp_mode |
||
51 | * @return string |
||
52 | */ |
||
53 | public function get_edit_url(array $post_data, array $topic_data, $cp_mode = '') |
||
58 | |||
59 | /** |
||
60 | * @param array $post_data |
||
61 | * @param array $topic_data |
||
62 | * @param string $cp_mode |
||
63 | * @return string |
||
64 | */ |
||
65 | public function get_delete_url(array $post_data, array $topic_data, $cp_mode = '') |
||
70 | |||
71 | /** |
||
72 | * @param array $post_data |
||
73 | * @param array $topic_data |
||
74 | * @return string |
||
75 | */ |
||
76 | public function get_quote_url(array $post_data, array $topic_data) |
||
80 | |||
81 | /** |
||
82 | * @param array $topic_data |
||
83 | * @return string |
||
84 | */ |
||
85 | public function get_viewtopic_url(array $topic_data) |
||
89 | |||
90 | /** |
||
91 | * @param array $topic_data |
||
92 | * @return string |
||
93 | */ |
||
94 | public function get_print_topic_url(array $topic_data) |
||
98 | |||
99 | /** |
||
100 | * @param array $topic_data |
||
101 | * @return string |
||
102 | */ |
||
103 | public function get_email_topic_url(array $topic_data) |
||
107 | |||
108 | /** |
||
109 | * @param int $forum_id |
||
110 | * @param string $username |
||
111 | * @return string |
||
112 | */ |
||
113 | public function get_search_users_posts_url($forum_id, $username) |
||
117 | |||
118 | /** |
||
119 | * @param array $post_data |
||
120 | * @return string |
||
121 | */ |
||
122 | public function get_info_url(array $post_data) |
||
127 | |||
128 | /** |
||
129 | * @param array $post_data |
||
130 | * @param string $viewtopic_url |
||
131 | * @return string |
||
132 | */ |
||
133 | public function get_approve_url(array $post_data, $viewtopic_url) |
||
137 | |||
138 | /** |
||
139 | * @param array $post_data |
||
140 | * @param array $topic_data |
||
141 | * @return string |
||
142 | */ |
||
143 | public function get_mcp_edit_url(array $post_data, array $topic_data) |
||
147 | |||
148 | /** |
||
149 | * @param array $post_data |
||
150 | * @return string |
||
151 | * |
||
152 | public function get_mcp_approve_url(array $post_data) |
||
153 | { |
||
154 | return append_sid("{$this->phpbb_root_path}mcp.$this->php_ext", 'i=queue&mode=approve_details&f=' . $post_data['forum_id'] . '&p=' . $post_data['post_id'], true, $this->user->session_id); |
||
155 | } |
||
156 | */ |
||
157 | |||
158 | /** |
||
159 | * @param array $post_data |
||
160 | * @return string |
||
161 | */ |
||
162 | public function get_mcp_report_url(array $post_data) |
||
166 | |||
167 | /** |
||
168 | * @param array $post_data |
||
169 | * @param array $topic_data |
||
170 | * @return string |
||
171 | */ |
||
172 | public function get_mcp_restore_url(array $post_data, array $topic_data) |
||
176 | |||
177 | /** |
||
178 | * @param array $post_data |
||
179 | * @return string |
||
180 | */ |
||
181 | public function get_notes_url(array $post_data) |
||
185 | |||
186 | /** |
||
187 | * @param array $post_data |
||
188 | * @return string |
||
189 | */ |
||
190 | public function get_warning_url(array $post_data) |
||
194 | |||
195 | /** |
||
196 | * @param int $topic_id |
||
197 | * @return string |
||
198 | */ |
||
199 | public function get_mcp_queue_url($topic_id) |
||
203 | |||
204 | /** |
||
205 | * @param string $content_type |
||
206 | * @param int $topic_id |
||
207 | * @return string |
||
208 | */ |
||
209 | public function get_mcp_review_url($content_type, $topic_id) |
||
213 | |||
214 | /** |
||
215 | * @param array $post_data |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function display_attachments_notice(array $post_data) |
||
222 | |||
223 | /** |
||
224 | * @param array $post_data |
||
225 | * @return bool |
||
226 | */ |
||
227 | public function permanent_delete_allowed(array $post_data) |
||
232 | |||
233 | /** |
||
234 | * @param int $poster_id |
||
235 | * @return bool |
||
236 | */ |
||
237 | public function user_is_poster($poster_id) |
||
241 | |||
242 | /** |
||
243 | * @param int $forum_id |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function can_report_post($forum_id) |
||
250 | |||
251 | /** |
||
252 | * @param array $topic_data |
||
253 | * @return bool |
||
254 | */ |
||
255 | public function topic_has_unapproved_posts(array $topic_data) |
||
259 | |||
260 | /** |
||
261 | * @param array $topic_data |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function topic_is_reported(array $topic_data) |
||
268 | |||
269 | /** |
||
270 | * @param array $topic_data |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function topic_is_locked(array $topic_data) |
||
277 | |||
278 | /** |
||
279 | * @param array $post_data |
||
280 | * @return bool |
||
281 | */ |
||
282 | public function post_is_unapproved(array $post_data) |
||
286 | |||
287 | /** |
||
288 | * @param array $post_data |
||
289 | * @param array $topic_data |
||
290 | * @param string $cp_mode |
||
291 | * @return string |
||
292 | */ |
||
293 | protected function get_cp_param(array $post_data, array $topic_data, $cp_mode) |
||
302 | |||
303 | /** |
||
304 | * @param array $post_data |
||
305 | * @param array $topic_data |
||
306 | * @return bool |
||
307 | */ |
||
308 | protected function edit_allowed(array $post_data, array $topic_data) |
||
316 | |||
317 | /** |
||
318 | * @param array $topic_data |
||
319 | * @return bool |
||
320 | */ |
||
321 | protected function quote_allowed(array $topic_data) |
||
327 | |||
328 | /** |
||
329 | * @param array $post_data |
||
330 | * @param array $topic_data |
||
331 | * @return bool |
||
332 | */ |
||
333 | protected function post_is_quotable(array $post_data, array $topic_data) |
||
337 | |||
338 | /** |
||
339 | * @param array $post_data |
||
340 | * @param array $topic_data |
||
341 | * @return bool |
||
342 | */ |
||
343 | protected function delete_allowed(array $post_data, array $topic_data) |
||
352 | |||
353 | /** |
||
354 | * @param array $post_data |
||
355 | * @return bool |
||
356 | */ |
||
357 | protected function softdelete_allowed(array $post_data) |
||
362 | |||
363 | /** |
||
364 | * @param array $post_data |
||
365 | * @return bool |
||
366 | */ |
||
367 | protected function cannot_edit(array $post_data) |
||
371 | |||
372 | /** |
||
373 | * @param array $post_data |
||
374 | * @return bool |
||
375 | */ |
||
376 | protected function cannot_edit_time(array $post_data) |
||
380 | |||
381 | /** |
||
382 | * @param array $post_data |
||
383 | * @param array $topic_data |
||
384 | * @return bool |
||
385 | */ |
||
386 | protected function cannot_edit_locked(array $post_data, array $topic_data) |
||
390 | |||
391 | /** |
||
392 | * @param array $post_data |
||
393 | * @return bool |
||
394 | */ |
||
395 | protected function cannot_delete(array $post_data) |
||
402 | |||
403 | /** |
||
404 | * @param array $post_data |
||
405 | * @param array $topic_data |
||
406 | * @return bool |
||
407 | */ |
||
408 | protected function cannot_delete_lastpost(array $post_data, array $topic_data) |
||
412 | |||
413 | /** |
||
414 | * @param array $post_data |
||
415 | * @return bool |
||
416 | */ |
||
417 | protected function cannot_delete_time(array $post_data) |
||
421 | |||
422 | /** |
||
423 | * we do not want to allow removal of the last post if a moderator locked it! |
||
424 | * @param array $post_data |
||
425 | * @param array $topic_data |
||
426 | * @return bool |
||
427 | */ |
||
428 | protected function cannot_delete_locked(array $post_data, array $topic_data) |
||
432 | } |
||
433 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.