Complex classes like display 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 display, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class display extends \blitze\sitemaker\services\tree\display |
||
13 | { |
||
14 | /** @var \phpbb\template\template */ |
||
15 | protected $template; |
||
16 | |||
17 | /** @var \phpbb\user */ |
||
18 | protected $user; |
||
19 | |||
20 | /** @var bool */ |
||
21 | private $expanded = false; |
||
22 | |||
23 | /** @var integer */ |
||
24 | private $max_depth = 0; |
||
25 | |||
26 | /** @var integer */ |
||
27 | private $min_depth = 0; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $parental_depth; |
||
31 | |||
32 | /** @var array */ |
||
33 | private $current_item; |
||
34 | |||
35 | /** |
||
36 | * Construct |
||
37 | * |
||
38 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
39 | * @param \phpbb\template\template $template Template object |
||
40 | * @param \phpbb\user $user User Object |
||
41 | * @param string $menu_items_table Menu Items table |
||
42 | * @param string $pk Primary key |
||
43 | */ |
||
44 | 18 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $menu_items_table, $pk) |
|
51 | |||
52 | /** |
||
53 | * @param array $params |
||
54 | * @return void |
||
55 | */ |
||
56 | 7 | public function set_params(array $params) |
|
61 | |||
62 | /** |
||
63 | * @param array $data |
||
64 | * @param \phpbb\template\twig\twig $template |
||
65 | * @param string $handle |
||
66 | * @return void |
||
67 | */ |
||
68 | 7 | public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
88 | |||
89 | /** |
||
90 | * @param array $data |
||
91 | * @return void |
||
92 | */ |
||
93 | 5 | public function generate_breadcrumb(array $data) |
|
97 | |||
98 | /** |
||
99 | * @param array $data |
||
100 | * @return void |
||
101 | */ |
||
102 | 7 | protected function prepare_items(array &$data) |
|
149 | |||
150 | /** |
||
151 | * @param array $data |
||
152 | * @return bool |
||
153 | */ |
||
154 | 7 | protected function set_current_item(array $data) |
|
174 | |||
175 | /** |
||
176 | * return void |
||
177 | */ |
||
178 | 1 | protected function default_current_item() |
|
191 | |||
192 | /** |
||
193 | * @param string $curr_page |
||
194 | * @param array $curr_parts |
||
195 | * @param array $row |
||
196 | * @return bool |
||
197 | */ |
||
198 | 7 | protected function is_current_path($curr_page, array $curr_parts, array $row) |
|
202 | |||
203 | /** |
||
204 | * @param array $row |
||
205 | * @return bool |
||
206 | */ |
||
207 | 7 | protected function is_current_item(array $row) |
|
211 | |||
212 | /** |
||
213 | * @param array $row |
||
214 | * @param int $depth |
||
215 | * @param array|null $leaf |
||
216 | * @param bool $is_current_item |
||
217 | * @return void |
||
218 | */ |
||
219 | 7 | protected function set_parental_depth(array $row, $depth, &$leaf, $is_current_item) |
|
230 | |||
231 | /** |
||
232 | * @param \phpbb\template\twig\twig $template |
||
233 | * @param string $handle |
||
234 | * @param int $repeat |
||
235 | * @return void |
||
236 | */ |
||
237 | 7 | protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
|
244 | |||
245 | /** |
||
246 | * @param int $items_depth |
||
247 | * return bool |
||
248 | */ |
||
249 | 6 | protected function needs_adjustment($items_depth) |
|
250 | { |
||
251 | 6 | return ($items_depth >= $this->max_depth) ? true : false; |
|
252 | } |
||
253 | |||
254 | /** |
||
255 | * @param array $row |
||
256 | * @return void |
||
257 | */ |
||
258 | 6 | protected function adjust_depth(array $row) |
|
259 | { |
||
260 | 6 | $depth = (int) $row['depth']; |
|
261 | 6 | if ($this->needs_adjustment($depth)) |
|
262 | 6 | { |
|
263 | 3 | $adjustment = ($this->count_descendants($row)) ? 1 : 0; |
|
264 | 3 | $this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0; |
|
265 | 3 | $this->max_depth = $depth + $adjustment; |
|
266 | 3 | } |
|
267 | 6 | } |
|
268 | |||
269 | /** |
||
270 | * @param int $item_id |
||
271 | * @param array $data |
||
272 | * @param array $leaf |
||
273 | * @return void |
||
274 | */ |
||
275 | 4 | protected function adjust_right_id($item_id, array &$data, array $leaf) |
|
282 | |||
283 | /** |
||
284 | * @param array $data |
||
285 | * @param int $parent_id |
||
286 | * @return void |
||
287 | */ |
||
288 | 5 | protected function find_parents(array $data, $parent_id) |
|
301 | } |
||
302 |