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 string */ |
||
| 21 | protected $php_ext; |
||
| 22 | |||
| 23 | /** @var bool */ |
||
| 24 | private $expanded = true; |
||
| 25 | |||
| 26 | /** @var integer */ |
||
| 27 | private $max_depth = 100; |
||
| 28 | |||
| 29 | /** @var integer */ |
||
| 30 | private $min_depth = 0; |
||
| 31 | |||
| 32 | /** @var array */ |
||
| 33 | private $parental_depth; |
||
| 34 | |||
| 35 | /** @var array */ |
||
| 36 | private $current_item; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Construct |
||
| 40 | * |
||
| 41 | * @param \phpbb\db\driver\driver_interface $db Database connection |
||
| 42 | * @param \phpbb\template\template $template Template object |
||
| 43 | * @param \phpbb\user $user User Object |
||
| 44 | * @param string $menu_items_table Menu Items table |
||
| 45 | * @param string $pk Primary key |
||
| 46 | * @param string $php_ext php file extension |
||
| 47 | */ |
||
| 48 | 21 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $menu_items_table, $pk, $php_ext) |
|
| 49 | { |
||
| 50 | 21 | parent::__construct($db, $menu_items_table, $pk); |
|
| 51 | |||
| 52 | 21 | $this->template = $template; |
|
| 53 | 21 | $this->user = $user; |
|
| 54 | 21 | $this->php_ext = $php_ext; |
|
| 55 | 21 | } |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param array $params |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | 10 | public function set_params(array $params) |
|
| 62 | { |
||
| 63 | 10 | $this->expanded = (bool) ((isset($params['expanded'])) ? $params['expanded'] : true); |
|
| 64 | 10 | $this->max_depth = (int) ((isset($params['max_depth'])) ? $params['max_depth'] : 100); |
|
| 65 | 10 | } |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $data |
||
| 69 | * @param \phpbb\template\twig\twig $template |
||
| 70 | * @param string $handle |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | 10 | public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
| 74 | { |
||
| 75 | 10 | $this->set_current_item($data); |
|
| 76 | 10 | $this->prepare_items($data['items']); |
|
| 77 | |||
| 78 | 10 | if (sizeof($data['items'])) |
|
| 79 | 10 | { |
|
| 80 | 10 | $this_depth = 0; |
|
| 81 | 10 | foreach ($data['items'] as $row) |
|
| 82 | { |
||
| 83 | 10 | $prev_depth = $row['prev_depth']; |
|
| 84 | 10 | $this_depth = $row['this_depth']; |
|
| 85 | 10 | $row['num_kids'] = $this->count_descendants($row); |
|
| 86 | |||
| 87 | 10 | $template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER)); |
|
| 88 | 10 | $this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth)); |
|
| 89 | 10 | } |
|
| 90 | |||
| 91 | 10 | $this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth)); |
|
| 92 | 10 | } |
|
| 93 | 10 | } |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @param array $data |
||
| 97 | * @return bool |
||
| 98 | */ |
||
| 99 | 10 | protected function set_current_item(array $data) |
|
| 100 | { |
||
| 101 | 10 | $paths = (array) $data['paths']; |
|
| 102 | 10 | $this->min_depth = 0; |
|
| 103 | |||
| 104 | 10 | arsort($paths); |
|
| 105 | |||
| 106 | 10 | $curr_path = $this->get_current_path(); |
|
| 107 | 10 | foreach ($paths as $item_id => $test_url) |
|
| 108 | { |
||
| 109 | 8 | if (strpos($curr_path, $test_url) !== false) |
|
| 110 | 8 | { |
|
| 111 | 6 | $row = $data['items'][$item_id]; |
|
| 112 | 6 | $this->adjust_depth($row); |
|
| 113 | 6 | $this->current_item = $row; |
|
| 114 | |||
| 115 | 6 | return true; |
|
| 116 | } |
||
| 117 | 9 | } |
|
| 118 | |||
| 119 | 4 | $this->current_item = $this->default_current_item(); |
|
| 120 | 4 | return false; |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | 10 | protected function get_current_path() |
|
| 127 | { |
||
| 128 | 10 | $curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './'); |
|
| 129 | 10 | $curr_parts = explode('&', $this->user->page['query_string']); |
|
| 130 | |||
| 131 | 10 | sort($curr_parts); |
|
| 132 | |||
| 133 | 10 | return $curr_page . '?' . join('&', $curr_parts); |
|
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * return void |
||
| 138 | */ |
||
| 139 | 4 | protected function default_current_item() |
|
| 140 | { |
||
| 141 | 4 | $this->max_depth = ($this->expanded) ? $this->max_depth : 0; |
|
| 142 | 4 | $this->min_depth = 0; |
|
| 143 | |||
| 144 | return array( |
||
| 145 | 4 | 'item_id' => 0, |
|
| 146 | 4 | 'parent_id' => 0, |
|
| 147 | 4 | 'left_id' => 0, |
|
| 148 | 4 | 'right_id' => 0, |
|
| 149 | 4 | 'depth' => 0, |
|
| 150 | 4 | ); |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @param array $data |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | 10 | protected function prepare_items(array &$data) |
|
| 158 | { |
||
| 159 | 10 | $leaf = array(); |
|
| 160 | 10 | $prev_depth = $this->min_depth; |
|
| 161 | 10 | $this->parental_depth = array(0 => -1); |
|
| 162 | |||
| 163 | 10 | foreach ($data as $item_id => $row) |
|
| 164 | { |
||
| 165 | // Skip branch |
||
| 166 | 10 | if ($this->should_skip_branch($row, $leaf)) |
|
| 167 | 10 | { |
|
| 168 | 4 | $this->adjust_right_id($leaf['item_id'], $data, $leaf); |
|
| 169 | 4 | unset($data[$item_id]); |
|
| 170 | 4 | continue; |
|
| 171 | } |
||
| 172 | |||
| 173 | 10 | $is_current_item = $this->is_current_item($row); |
|
| 174 | 10 | $is_parent = $this->is_parent_of_current_item($row); |
|
| 175 | 10 | $this_depth = $this->parental_depth[$row['parent_id']] + 1; |
|
| 176 | 10 | $leaf = $this->get_leaf_node($row, $is_current_item, $is_parent); |
|
| 177 | |||
| 178 | 10 | $this->parental_depth[$row[$this->pk]] = $this_depth; |
|
| 179 | |||
| 180 | 10 | if ($row['depth'] < $this->min_depth) |
|
| 181 | 10 | { |
|
| 182 | 2 | unset($data[$item_id]); |
|
| 183 | 2 | continue; |
|
| 184 | } |
||
| 185 | |||
| 186 | 10 | $data[$item_id] = array_merge($data[$item_id], array( |
|
| 187 | 10 | 'prev_depth' => $prev_depth, |
|
| 188 | 10 | 'this_depth' => $this_depth, |
|
| 189 | 10 | 'is_current' => $is_current_item, |
|
| 190 | 10 | 'is_parent' => $is_parent, |
|
| 191 | 10 | 'full_url' => $this->get_full_url($row), |
|
| 192 | 10 | )); |
|
| 193 | |||
| 194 | 10 | $prev_depth = $this_depth; |
|
| 195 | 10 | } |
|
| 196 | 10 | unset($this->parental_depth, $data); |
|
| 197 | 10 | } |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param array $row |
||
| 201 | * @param array $leaf |
||
| 202 | * @return bool |
||
| 203 | */ |
||
| 204 | 10 | protected function should_skip_branch(array $row, array $leaf) |
|
| 205 | { |
||
| 206 | 10 | return (sizeof($leaf) && $row['left_id'] < $leaf['right_id']); |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param array $row |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | 10 | protected function is_current_item(array $row) |
|
| 214 | { |
||
| 215 | 10 | return ($row['item_id'] === $this->current_item['item_id']) ? true : false; |
|
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @param array $row |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | 10 | protected function is_parent_of_current_item(array $row) |
|
| 223 | { |
||
| 224 | 10 | return ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']) ? true : false; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Does the branch end here? |
||
| 229 | * |
||
| 230 | * @param array $row |
||
| 231 | * @param bool $is_current_item |
||
| 232 | * @param bool $is_current_items_parent |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 10 | protected function get_leaf_node(array $row, $is_current_item, $is_current_items_parent) |
|
| 236 | { |
||
| 237 | 10 | return ($this->must_not_expand($row, $is_current_items_parent) && !$is_current_item && $row['is_expandable']) ? $row : array(); |
|
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * @param array $row |
||
| 242 | * @param bool $is_current_items_parent |
||
| 243 | * @return bool |
||
| 244 | */ |
||
| 245 | 10 | protected function must_not_expand(array $row, $is_current_items_parent) |
|
| 246 | { |
||
| 247 | 10 | return ($row['depth'] === $this->max_depth || !$is_current_items_parent && !$this->expanded) ? true : false; |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param \phpbb\template\twig\twig $template |
||
| 252 | * @param string $handle |
||
| 253 | * @param int $repeat |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | 10 | protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
|
| 257 | { |
||
| 258 | 10 | for ($i = 0; $i < $repeat; $i++) |
|
| 259 | { |
||
| 260 | 8 | $template->assign_block_vars($handle, array()); |
|
| 261 | 8 | } |
|
| 262 | 10 | } |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @param int $items_depth |
||
| 266 | * return bool |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | 6 | protected function needs_adjustment($items_depth) |
|
| 270 | { |
||
| 271 | 6 | return (!$this->expanded && $items_depth >= $this->max_depth) ? true : false; |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param array $row |
||
| 276 | * @return void |
||
| 277 | */ |
||
| 278 | 6 | protected function adjust_depth(array $row) |
|
| 279 | { |
||
| 280 | 6 | $depth = (int) $row['depth']; |
|
| 281 | 6 | if ($this->needs_adjustment($depth)) |
|
| 282 | 6 | { |
|
| 283 | 2 | $adjustment = ($this->count_descendants($row)) ? 1 : 0; |
|
| 284 | 2 | $this->set_depth_limits($depth, $adjustment); |
|
| 285 | 2 | } |
|
| 286 | 6 | } |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param int $item_id |
||
| 290 | * @param array $data |
||
| 291 | * @param array $leaf |
||
| 292 | * @return void |
||
| 293 | */ |
||
| 294 | 4 | protected function adjust_right_id($item_id, array &$data, array $leaf) |
|
| 295 | { |
||
| 296 | 4 | if (isset($data[$item_id])) |
|
| 297 | 4 | { |
|
| 298 | 4 | $data[$leaf['item_id']]['right_id'] -= 2; |
|
| 299 | 4 | } |
|
| 300 | 4 | } |
|
| 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 | /** |
||
| 320 | * @param int $depth |
||
| 321 | * @param int $adjustment |
||
| 322 | */ |
||
| 323 | 2 | protected function set_depth_limits($depth, $adjustment) |
|
| 324 | { |
||
| 325 | 2 | $this->min_depth = ($this->max_depth && $depth >= $this->max_depth) ? $depth - $this->max_depth + $adjustment : 0; |
|
| 326 | 2 | $this->max_depth = $depth + $adjustment; |
|
| 327 | 2 | } |
|
| 328 | } |
||
| 329 |