Complex classes like poll 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 poll, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class poll |
||
13 | { |
||
14 | /** @var \phpbb\auth\auth */ |
||
15 | protected $auth; |
||
16 | |||
17 | /** @var \phpbb\config\config */ |
||
18 | protected $config; |
||
19 | |||
20 | /** @var \phpbb\db\driver\driver_interface */ |
||
21 | protected $db; |
||
22 | |||
23 | /** @var \phpbb\request\request_interface */ |
||
24 | protected $request; |
||
25 | |||
26 | /** @var \phpbb\user */ |
||
27 | protected $user; |
||
28 | |||
29 | /** @var \blitze\sitemaker\services\util */ |
||
30 | protected $sitemaker; |
||
31 | |||
32 | /** @var string */ |
||
33 | protected $phpbb_root_path; |
||
34 | |||
35 | /** @var string */ |
||
36 | protected $php_ext; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * |
||
41 | * @param \phpbb\auth\auth $auth Permission object |
||
42 | * @param \phpbb\config\config $config Config object |
||
43 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
44 | * @param \phpbb\request\request_interface $request Request object |
||
45 | * @param \phpbb\user $user User object |
||
46 | * @param \blitze\sitemaker\services\util $sitemaker Sitemaker Object |
||
47 | * @param string $phpbb_root_path Path to the phpbb includes directory. |
||
48 | * @param string $php_ext php file extension |
||
49 | */ |
||
50 | 4 | public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\request\request_interface $request, \phpbb\user $user, \blitze\sitemaker\services\util $sitemaker, $phpbb_root_path, $php_ext) |
|
61 | |||
62 | /** |
||
63 | * @param array $topic_data |
||
64 | * @param \phpbb\template\twig\twig $template |
||
65 | */ |
||
66 | 2 | public function build(array $topic_data, \phpbb\template\twig\twig &$template) |
|
101 | |||
102 | /** |
||
103 | * @param int $forum_id |
||
104 | * @param array $topic_data |
||
105 | * @param array $cur_voted_id |
||
106 | * @return bool |
||
107 | */ |
||
108 | 2 | private function _user_can_vote($forum_id, array $topic_data, array $cur_voted_id) |
|
117 | |||
118 | /** |
||
119 | * @param array $topic_data |
||
120 | * @param int $poll_total |
||
121 | * @param int $poll_most |
||
122 | * @return array |
||
123 | */ |
||
124 | 2 | private function _get_poll_info(array $topic_data, &$poll_total, &$poll_most) |
|
148 | |||
149 | /** |
||
150 | * @param array $topic_data |
||
151 | * @param array $poll_info |
||
152 | * @return array |
||
153 | */ |
||
154 | 2 | private function _parse_poll(array &$topic_data, array $poll_info) |
|
167 | |||
168 | /** |
||
169 | * @param array $cur_voted_id |
||
170 | * @param array $poll_info |
||
171 | * @param int $poll_total |
||
172 | * @param int $poll_most |
||
173 | * @param \phpbb\template\twig\twig $template |
||
174 | */ |
||
175 | 2 | private function _build_poll_options(array $cur_voted_id, array $poll_info, $poll_total, $poll_most, \phpbb\template\twig\twig &$template) |
|
195 | |||
196 | /** |
||
197 | * @param int $topic_id |
||
198 | * @return array |
||
199 | */ |
||
200 | 2 | private function _get_users_votes($topic_id) |
|
231 | |||
232 | /** |
||
233 | * @param int $poll_option_id |
||
234 | * @param array $cur_voted_id |
||
235 | * @return bool |
||
236 | */ |
||
237 | 2 | private function _user_has_voted_option($poll_option_id, array $cur_voted_id) |
|
241 | |||
242 | /** |
||
243 | * @param int $poll_option_total |
||
244 | * @param int $poll_total |
||
245 | * @return float|int |
||
246 | */ |
||
247 | 2 | private function _calculate_option_percent($poll_option_total, $poll_total) |
|
251 | |||
252 | /** |
||
253 | * @param int $poll_option_total |
||
254 | * @param int $poll_most |
||
255 | * @return float|int |
||
256 | */ |
||
257 | 2 | private function _calculate_option_percent_rel($poll_option_total, $poll_most) |
|
261 | |||
262 | /** |
||
263 | * @param int $poll_option_total |
||
264 | * @param int $poll_most |
||
265 | * @return bool |
||
266 | */ |
||
267 | 2 | private function _is_most_voted($poll_option_total, $poll_most) |
|
271 | |||
272 | /** |
||
273 | * @param int $poll_max_options |
||
274 | * @return bool |
||
275 | */ |
||
276 | 2 | private function _poll_is_multiple_choice($poll_max_options) |
|
280 | |||
281 | /** |
||
282 | * @param int $poll_length |
||
283 | * @param int $poll_end |
||
284 | * @return string |
||
285 | */ |
||
286 | 2 | private function _get_poll_length_lang($poll_length, $poll_end) |
|
290 | |||
291 | /** |
||
292 | * @param bool $s_can_vote |
||
293 | * @param array $cur_voted_id |
||
294 | * @return bool |
||
295 | */ |
||
296 | 2 | private function _show_results($s_can_vote, array $cur_voted_id) |
|
300 | } |
||
301 |