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\user */ |
||
15 | protected $user; |
||
16 | |||
17 | /** @var bool */ |
||
18 | private $expanded = true; |
||
19 | |||
20 | /** @var integer */ |
||
21 | private $max_depth = 100; |
||
22 | |||
23 | /** @var integer */ |
||
24 | private $min_depth = 0; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $parental_depth; |
||
28 | |||
29 | /** @var array */ |
||
30 | private $current_item; |
||
31 | |||
32 | /** |
||
33 | * Construct |
||
34 | * |
||
35 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
36 | * @param \phpbb\user $user User Object |
||
37 | * @param string $menu_items_table Menu Items table |
||
38 | * @param string $pk Primary key |
||
39 | */ |
||
40 | 21 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\user $user, $menu_items_table, $pk) |
|
46 | |||
47 | /** |
||
48 | * @param array $params |
||
49 | * @return void |
||
50 | */ |
||
51 | 10 | public function set_params(array $params) |
|
56 | |||
57 | /** |
||
58 | * @param array $data |
||
59 | * @param \phpbb\template\twig\twig $template |
||
60 | * @param string $handle |
||
61 | * @return void |
||
62 | */ |
||
63 | 10 | public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
84 | |||
85 | /** |
||
86 | * @param array $data |
||
87 | * @return bool |
||
88 | */ |
||
89 | 10 | protected function set_current_item(array $data) |
|
90 | { |
||
91 | 10 | $paths = (array) $data['paths']; |
|
92 | 10 | $this->min_depth = 0; |
|
93 | |||
94 | 10 | arsort($paths); |
|
95 | |||
96 | 10 | $curr_path = $this->get_current_path(); |
|
97 | 10 | foreach ($paths as $item_id => $test_url) |
|
98 | { |
||
99 | 8 | if (strpos($curr_path, $test_url) !== false) |
|
100 | 8 | { |
|
101 | 6 | $row = $data['items'][$item_id]; |
|
102 | 6 | $this->adjust_depth($row); |
|
103 | 6 | $this->current_item = $row; |
|
104 | |||
105 | 6 | return true; |
|
106 | } |
||
107 | 9 | } |
|
108 | |||
109 | 4 | $this->current_item = $this->default_current_item(); |
|
110 | 4 | return false; |
|
111 | } |
||
112 | |||
113 | /** |
||
114 | * @return string |
||
115 | */ |
||
116 | 10 | protected function get_current_path() |
|
125 | |||
126 | /** |
||
127 | * return void |
||
128 | */ |
||
129 | 4 | protected function default_current_item() |
|
130 | { |
||
131 | 4 | $this->max_depth = ($this->expanded) ? $this->max_depth : 0; |
|
132 | 4 | $this->min_depth = 0; |
|
133 | |||
134 | return array( |
||
135 | 4 | $this->column_item_id => 0, |
|
136 | 4 | $this->column_parent_id => 0, |
|
137 | 4 | $this->column_left_id => 0, |
|
138 | 4 | $this->column_right_id => 0, |
|
139 | 4 | $this->column_depth => 0, |
|
140 | 4 | ); |
|
141 | } |
||
142 | |||
143 | /** |
||
144 | * @param array $data |
||
145 | * @return void |
||
146 | */ |
||
147 | 10 | protected function prepare_items(array &$data) |
|
148 | { |
||
149 | 10 | $leaf = array(); |
|
150 | 10 | $prev_depth = $this->min_depth; |
|
151 | 10 | $this->parental_depth = array(0 => -1); |
|
152 | |||
153 | 10 | foreach ($data as $item_id => $row) |
|
154 | { |
||
155 | // Skip branch |
||
156 | 10 | if ($this->should_skip_branch($row, $leaf)) |
|
157 | 10 | { |
|
158 | 4 | $this->adjust_right_id($leaf[$this->column_item_id], $data, $leaf); |
|
159 | 4 | unset($data[$item_id]); |
|
160 | 4 | continue; |
|
161 | } |
||
162 | |||
163 | 10 | $is_current_item = $this->is_current_item($row); |
|
164 | 10 | $is_parent = $this->is_parent_of_current_item($row); |
|
165 | 10 | $this_depth = $this->parental_depth[$row[$this->column_parent_id]] + 1; |
|
166 | 10 | $leaf = $this->get_leaf_node($row, $is_current_item, $is_parent); |
|
167 | |||
168 | 10 | $this->parental_depth[$row[$this->pk]] = $this_depth; |
|
169 | |||
170 | 10 | if ($row[$this->column_depth] < $this->min_depth) |
|
171 | 10 | { |
|
172 | 2 | unset($data[$item_id]); |
|
173 | 2 | continue; |
|
174 | } |
||
175 | |||
176 | 10 | $data[$item_id] = array_merge($data[$item_id], array( |
|
177 | 10 | 'prev_depth' => $prev_depth, |
|
178 | 10 | 'this_depth' => $this_depth, |
|
179 | 10 | 'is_current' => $is_current_item, |
|
180 | 10 | 'is_parent' => $is_parent, |
|
181 | 10 | 'full_url' => $this->get_full_url($row), |
|
182 | 10 | )); |
|
183 | |||
184 | 10 | $prev_depth = $this_depth; |
|
185 | 10 | } |
|
186 | 10 | unset($this->parental_depth, $data); |
|
187 | 10 | } |
|
188 | |||
189 | /** |
||
190 | * @param array $row |
||
191 | * @param array $leaf |
||
192 | * @return bool |
||
193 | */ |
||
194 | 10 | protected function should_skip_branch(array $row, array $leaf) |
|
198 | |||
199 | /** |
||
200 | * @param array $row |
||
201 | * @return bool |
||
202 | */ |
||
203 | 10 | protected function is_current_item(array $row) |
|
207 | |||
208 | /** |
||
209 | * @param array $row |
||
210 | * @return bool |
||
211 | */ |
||
212 | 10 | protected function is_parent_of_current_item(array $row) |
|
216 | |||
217 | /** |
||
218 | * Does the branch end here? |
||
219 | * |
||
220 | * @param array $row |
||
221 | * @param bool $is_current_item |
||
222 | * @param bool $is_current_items_parent |
||
223 | * @return array |
||
224 | */ |
||
225 | 10 | protected function get_leaf_node(array $row, $is_current_item, $is_current_items_parent) |
|
229 | |||
230 | /** |
||
231 | * @param array $row |
||
232 | * @param bool $is_current_items_parent |
||
233 | * @return bool |
||
234 | */ |
||
235 | 10 | protected function must_not_expand(array $row, $is_current_items_parent) |
|
239 | |||
240 | /** |
||
241 | * @param \phpbb\template\twig\twig $template |
||
242 | * @param string $handle |
||
243 | * @param int $repeat |
||
244 | * @return void |
||
245 | */ |
||
246 | 10 | protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
|
253 | |||
254 | /** |
||
255 | * @param int $items_depth |
||
256 | * return bool |
||
257 | * @return bool |
||
258 | */ |
||
259 | 6 | protected function needs_adjustment($items_depth) |
|
263 | |||
264 | /** |
||
265 | * @param array $row |
||
266 | * @return void |
||
267 | */ |
||
268 | 6 | protected function adjust_depth(array $row) |
|
277 | |||
278 | /** |
||
279 | * @param int $item_id |
||
280 | * @param array $data |
||
281 | * @param array $leaf |
||
282 | * @return void |
||
283 | */ |
||
284 | 4 | protected function adjust_right_id($item_id, array &$data, array $leaf) |
|
285 | { |
||
286 | 4 | if (isset($data[$item_id])) |
|
287 | 4 | { |
|
288 | 4 | $data[$leaf[$this->column_item_id]][$this->column_right_id] -= 2; |
|
289 | 4 | } |
|
290 | 4 | } |
|
291 | |||
292 | /** |
||
293 | * Append session id to local, non-directory paths |
||
294 | * |
||
295 | * @param array $row |
||
296 | * @return string |
||
297 | */ |
||
298 | 10 | protected function get_full_url(array $row) |
|
299 | { |
||
300 | 10 | $full_url = $row['full_url']; |
|
301 | 10 | if ($row['is_navigable']) |
|
302 | 10 | { |
|
303 | 6 | $full_url = append_sid($row['full_url'], false, false); |
|
304 | 6 | } |
|
305 | |||
306 | 10 | return $full_url; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * @param int $depth |
||
311 | * @param int $adjustment |
||
312 | */ |
||
313 | 2 | protected function set_depth_limits($depth, $adjustment) |
|
318 | } |
||
319 |