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 | 21 | 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 | 10 | public function set_params(array $params) |
|
57 | { |
||
58 | 10 | $this->expanded = (bool) $params['expanded']; |
|
59 | 10 | $this->max_depth = (int) $params['max_depth']; |
|
60 | 10 | } |
|
61 | |||
62 | /** |
||
63 | * @param array $data |
||
64 | * @param \phpbb\template\twig\twig $template |
||
65 | * @param string $handle |
||
66 | * @return void |
||
67 | */ |
||
68 | 10 | public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
88 | |||
89 | /** |
||
90 | * @param array $data |
||
91 | * @return void |
||
92 | */ |
||
93 | 8 | public function generate_breadcrumb(array $data) |
|
97 | |||
98 | /** |
||
99 | * @param array $data |
||
100 | * @return void |
||
101 | */ |
||
102 | 10 | protected function prepare_items(array &$data) |
|
146 | |||
147 | /** |
||
148 | * @param array $data |
||
149 | * @return bool |
||
150 | */ |
||
151 | 10 | protected function set_current_item(array $data) |
|
171 | |||
172 | /** |
||
173 | * return void |
||
174 | */ |
||
175 | 4 | protected function default_current_item() |
|
188 | |||
189 | /** |
||
190 | * @param string $curr_page |
||
191 | * @param array $curr_parts |
||
192 | * @param array $row |
||
193 | * @return bool |
||
194 | */ |
||
195 | 10 | protected function is_current_path($curr_page, array $curr_parts, array $row) |
|
199 | |||
200 | /** |
||
201 | * @param array $row |
||
202 | * @return bool |
||
203 | */ |
||
204 | 10 | protected function is_current_item(array $row) |
|
208 | |||
209 | /** |
||
210 | * @param array $row |
||
211 | * @return bool |
||
212 | */ |
||
213 | 9 | protected function is_child_of_current_item(array $row) |
|
217 | |||
218 | /** |
||
219 | * Does the branch end here? |
||
220 | * |
||
221 | * @param array $row |
||
222 | * @param bool $is_current_item |
||
223 | * @return array |
||
224 | */ |
||
225 | 10 | protected function get_leaf_node(array $row, $is_current_item) |
|
229 | |||
230 | /** |
||
231 | * @param \phpbb\template\twig\twig $template |
||
232 | * @param string $handle |
||
233 | * @param int $repeat |
||
234 | * @return void |
||
235 | */ |
||
236 | 10 | protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
|
237 | { |
||
238 | 10 | for ($i = 0; $i < $repeat; $i++) |
|
239 | { |
||
240 | 8 | $template->assign_block_vars($handle, array()); |
|
241 | 8 | } |
|
242 | 10 | } |
|
243 | |||
244 | /** |
||
245 | * @param int $items_depth |
||
246 | * return bool |
||
247 | * @return bool |
||
248 | */ |
||
249 | 6 | protected function needs_adjustment($items_depth) |
|
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) |
|
276 | { |
||
277 | 4 | if (isset($data[$item_id])) |
|
278 | 4 | { |
|
279 | 4 | $data[$leaf['item_id']]['right_id'] -= 2; |
|
280 | 4 | } |
|
281 | 4 | } |
|
282 | |||
283 | /** |
||
284 | * @param array $data |
||
285 | * @param int $parent_id |
||
286 | * @return void |
||
287 | */ |
||
288 | 8 | protected function find_parents(array $data, $parent_id) |
|
289 | { |
||
290 | 8 | if (isset($data[$parent_id]) && $data[$parent_id]['item_url'] !== 'index.php') |
|
291 | 8 | { |
|
292 | 2 | $row = $data[$parent_id]; |
|
293 | 2 | $this->template->alter_block_array('navlinks', array( |
|
294 | 2 | 'FORUM_NAME' => $row['item_title'], |
|
295 | 2 | 'U_VIEW_FORUM' => $row['full_url'], |
|
296 | 2 | )); |
|
297 | |||
298 | 2 | $this->find_parents($data, $row['parent_id']); |
|
299 | 2 | } |
|
300 | 8 | } |
|
301 | |||
302 | /** |
||
303 | * Append session id to local, non-directory paths |
||
304 | * |
||
305 | * @param array $row |
||
306 | * @return string |
||
307 | */ |
||
308 | 10 | protected function get_full_url(array $row) |
|
318 | } |
||
319 |