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 = false; |
||
| 25 | |||
| 26 | /** @var integer */ |
||
| 27 | private $max_depth = 0; |
||
| 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 | 2 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $menu_items_table, $pk, $php_ext) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param array $params |
||
| 59 | * @return void |
||
| 60 | */ |
||
| 61 | 2 | public function set_params(array $params) |
|
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $data |
||
| 69 | * @param \phpbb\template\twig\twig $template |
||
| 70 | * @param string $handle |
||
| 71 | * @return void |
||
| 72 | */ |
||
| 73 | 2 | public function display_navlist(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $data |
||
| 96 | * @return void |
||
| 97 | */ |
||
| 98 | public function generate_breadcrumb(array $data) |
||
| 99 | { |
||
| 100 | $this->find_parents($data, $this->current_item['parent_id']); |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param array $data |
||
| 105 | * @return void |
||
| 106 | */ |
||
| 107 | 2 | protected function prepare_items(array &$data) |
|
| 108 | { |
||
| 109 | 2 | $this->set_current_item($data); |
|
| 110 | |||
| 111 | 2 | $leaf = array(); |
|
| 112 | 2 | $prev_depth = $this->min_depth; |
|
| 113 | 2 | $this->parental_depth = array(0 => -1); |
|
| 114 | |||
| 115 | 2 | foreach ($data as $item_id => $row) |
|
| 116 | { |
||
| 117 | // Skip branch |
||
| 118 | 2 | if ($this->should_skip_branch($row, $leaf)) |
|
| 119 | 2 | { |
|
| 120 | $this->adjust_right_id($leaf['item_id'], $data, $leaf); |
||
| 121 | unset($data[$item_id]); |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | |||
| 125 | 2 | $is_current_item = $this->is_current_item($row); |
|
| 126 | 2 | $this_depth = $this->parental_depth[$row['parent_id']] + 1; |
|
| 127 | 2 | $leaf = $this->get_leaf_node($row, $is_current_item); |
|
| 128 | |||
| 129 | 2 | $this->parental_depth[$row[$this->pk]] = $this_depth; |
|
| 130 | |||
| 131 | 2 | if ($row['depth'] < $this->min_depth) |
|
| 132 | 2 | { |
|
| 133 | 1 | unset($data[$item_id]); |
|
| 134 | 1 | continue; |
|
| 135 | } |
||
| 136 | |||
| 137 | 2 | $data[$item_id] = array_merge($data[$item_id], array( |
|
| 138 | 2 | 'prev_depth' => $prev_depth, |
|
| 139 | 2 | 'this_depth' => $this_depth, |
|
| 140 | 2 | 'is_current' => $is_current_item, |
|
| 141 | 2 | 'full_url' => $this->get_full_url($row), |
|
| 142 | 2 | )); |
|
| 143 | |||
| 144 | 2 | $prev_depth = $this_depth; |
|
| 145 | 2 | } |
|
| 146 | 2 | unset($this->parental_depth, $data); |
|
| 147 | 2 | } |
|
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $row |
||
| 151 | * @param array $leaf |
||
| 152 | * @return bool |
||
| 153 | */ |
||
| 154 | 2 | protected function should_skip_branch(array $row, array $leaf) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param array $data |
||
| 161 | * @return bool |
||
| 162 | */ |
||
| 163 | 2 | protected function set_current_item(array $data) |
|
| 164 | { |
||
| 165 | 2 | $curr_page = '/' . ltrim($this->user->page['page_dir'] . '/' . $this->user->page['page_name'], './'); |
|
| 166 | 2 | $curr_page = str_replace('/index.' . $this->php_ext, '/', $curr_page); |
|
| 167 | 2 | $curr_parts = explode('&', $this->user->page['query_string']); |
|
| 168 | |||
| 169 | 2 | $data = array_values($data); |
|
| 170 | 2 | for ($i = 0, $size = sizeof($data); $i < $size; $i++) |
|
| 171 | { |
||
| 172 | 2 | $row = $data[$i]; |
|
| 173 | 2 | if ($this->is_current_path($curr_page, $curr_parts, $row)) |
|
| 174 | 2 | { |
|
| 175 | 2 | $this->adjust_depth($row); |
|
| 176 | 2 | $this->current_item = $row; |
|
| 177 | 2 | return true; |
|
| 178 | } |
||
| 179 | 1 | } |
|
| 180 | |||
| 181 | $this->current_item = $this->default_current_item(); |
||
| 182 | return false; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * return void |
||
| 187 | */ |
||
| 188 | protected function default_current_item() |
||
| 189 | { |
||
| 190 | $this->max_depth = ($this->expanded) ? $this->max_depth : 0; |
||
| 191 | $this->min_depth = 0; |
||
| 192 | |||
| 193 | return array( |
||
| 194 | 'item_id' => 0, |
||
| 195 | 'parent_id' => 0, |
||
| 196 | 'left_id' => 0, |
||
| 197 | 'right_id' => 0, |
||
| 198 | 'depth' => 0, |
||
| 199 | ); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $curr_page |
||
| 204 | * @param array $curr_parts |
||
| 205 | * @param array $row |
||
| 206 | * @return bool |
||
| 207 | */ |
||
| 208 | 2 | protected function is_current_path($curr_page, array $curr_parts, array $row) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param array $row |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 2 | protected function is_current_item(array $row) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @param array $row |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | 2 | protected function is_child_of_current_item(array $row) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Does the branch end here? |
||
| 233 | * |
||
| 234 | * @param array $row |
||
| 235 | * @param bool $is_current_item |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | 2 | protected function get_leaf_node(array $row, $is_current_item) |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param array $row |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | 2 | protected function must_not_expand(array $row) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param \phpbb\template\twig\twig $template |
||
| 254 | * @param string $handle |
||
| 255 | * @param int $repeat |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | 2 | protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param int $items_depth |
||
| 268 | * return bool |
||
| 269 | * @return bool |
||
| 270 | */ |
||
| 271 | 2 | protected function needs_adjustment($items_depth) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @param array $row |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | 2 | protected function adjust_depth(array $row) |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @param int $item_id |
||
| 292 | * @param array $data |
||
| 293 | * @param array $leaf |
||
| 294 | * @return void |
||
| 295 | */ |
||
| 296 | protected function adjust_right_id($item_id, array &$data, array $leaf) |
||
| 297 | { |
||
| 298 | if (isset($data[$item_id])) |
||
| 299 | { |
||
| 300 | $data[$leaf['item_id']]['right_id'] -= 2; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param array $data |
||
| 306 | * @param int $parent_id |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | protected function find_parents(array $data, $parent_id) |
||
| 310 | { |
||
| 311 | if (isset($data[$parent_id]) && $data[$parent_id]['item_url'] !== 'index.php') |
||
| 312 | { |
||
| 313 | $row = $data[$parent_id]; |
||
| 314 | $this->template->alter_block_array('navlinks', array( |
||
| 315 | 'FORUM_NAME' => $row['item_title'], |
||
| 316 | 'U_VIEW_FORUM' => $row['full_url'], |
||
| 317 | )); |
||
| 318 | |||
| 319 | $this->find_parents($data, $row['parent_id']); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Append session id to local, non-directory paths |
||
| 325 | * |
||
| 326 | * @param array $row |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | 2 | protected function get_full_url(array $row) |
|
| 330 | { |
||
| 331 | 2 | $full_url = $row['full_url']; |
|
| 332 | 2 | if ($row['is_navigable']) |
|
| 333 | 2 | { |
|
| 334 | $full_url = append_sid($row['full_url'], false, false); |
||
| 335 | } |
||
| 336 | |||
| 337 | 2 | return $full_url; |
|
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @param int $depth |
||
| 342 | * @param int $adjustment |
||
| 343 | */ |
||
| 344 | 2 | protected function set_depth_limits($depth, $adjustment) |
|
| 349 | } |
||
| 350 |