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) |
|
| 45 | { |
||
| 46 | 18 | parent::__construct($db, $menu_items_table, $pk); |
|
| 47 | |||
| 48 | 18 | $this->template = $template; |
|
| 49 | 18 | $this->user = $user; |
|
| 50 | 18 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $params |
||
| 54 | * @return void |
||
| 55 | */ |
||
| 56 | 7 | public function set_params(array $params) |
|
| 57 | { |
||
| 58 | 7 | $this->expanded = (bool) $params['expanded']; |
|
| 59 | 7 | $this->max_depth = (int) $params['max_depth']; |
|
| 60 | 7 | } |
|
| 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') |
|
| 69 | { |
||
| 70 | 7 | $this->prepare_items($data); |
|
| 71 | |||
| 72 | 7 | if (sizeof($data)) |
|
| 73 | 7 | { |
|
| 74 | 7 | $this_depth = 0; |
|
| 75 | 7 | foreach ($data as $row) |
|
| 76 | { |
||
| 77 | 7 | $prev_depth = $row['prev_depth']; |
|
| 78 | 7 | $this_depth = $row['this_depth']; |
|
| 79 | 7 | $row['num_kids'] = $this->count_descendants($row); |
|
| 80 | |||
| 81 | 7 | $template->assign_block_vars($handle, array_change_key_case($row, CASE_UPPER)); |
|
| 82 | 7 | $this->close_open_tags($template, $handle . '.close', abs($prev_depth - $this_depth)); |
|
| 83 | 7 | } |
|
| 84 | |||
| 85 | 7 | $this->close_open_tags($template, 'close_' . $handle, ($this_depth - $this->min_depth)); |
|
| 86 | 7 | } |
|
| 87 | 7 | } |
|
| 88 | |||
| 89 | /** |
||
| 90 | * @param array $data |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | 5 | public function generate_breadcrumb(array $data) |
|
| 94 | { |
||
| 95 | 5 | $this->find_parents($data, $this->current_item['parent_id']); |
|
| 96 | 5 | } |
|
| 97 | |||
| 98 | /** |
||
| 99 | * @param array $data |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | 7 | protected function prepare_items(array &$data) |
|
| 103 | { |
||
| 104 | 7 | $this->set_current_item($data); |
|
| 105 | |||
| 106 | 7 | $leaf = array(); |
|
| 107 | 7 | $prev_depth = $this->min_depth; |
|
| 108 | 7 | $this->parental_depth = array(0 => -1); |
|
| 109 | |||
| 110 | 7 | foreach ($data as $item_id => $row) |
|
| 111 | { |
||
| 112 | // Skip branch |
||
| 113 | 7 | if (sizeof($leaf)) |
|
| 114 | 7 | { |
|
| 115 | 5 | if ($row['left_id'] < $leaf['right_id']) |
|
| 116 | 5 | { |
|
| 117 | 4 | $this->adjust_right_id($leaf['item_id'], $data, $leaf); |
|
| 118 | 4 | unset($data[$item_id]); |
|
| 119 | 4 | continue; |
|
| 120 | } |
||
| 121 | 5 | $leaf = array(); |
|
|
|
|||
| 122 | 5 | } |
|
| 123 | |||
| 124 | 7 | $is_current_item = $this->is_current_item($row); |
|
| 125 | 7 | $this_depth = $this->parental_depth[$row['parent_id']] + 1; |
|
| 126 | 7 | $leaf = $this->get_leaf_node($row, $is_current_item); |
|
| 127 | |||
| 128 | 7 | $this->parental_depth[$row[$this->pk]] = $this_depth; |
|
| 129 | |||
| 130 | 7 | if ($row['depth'] < $this->min_depth) |
|
| 131 | 7 | { |
|
| 132 | 2 | unset($data[$item_id]); |
|
| 133 | 2 | continue; |
|
| 134 | } |
||
| 135 | |||
| 136 | 7 | $data[$item_id] = array_merge($data[$item_id], array( |
|
| 137 | 7 | 'prev_depth' => $prev_depth, |
|
| 138 | 7 | 'this_depth' => $this_depth, |
|
| 139 | 7 | 'is_current' => $is_current_item, |
|
| 140 | 7 | 'full_url' => $this->get_full_url($row), |
|
| 141 | 7 | )); |
|
| 142 | |||
| 143 | 7 | $prev_depth = $this_depth; |
|
| 144 | 7 | } |
|
| 145 | 7 | unset($this->parental_depth, $data); |
|
| 146 | 7 | } |
|
| 147 | |||
| 148 | /** |
||
| 149 | * @param array $data |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | 7 | protected function set_current_item(array $data) |
|
| 153 | { |
||
| 154 | 7 | $curr_page = $this->user->page['page_name']; |
|
| 155 | 7 | $curr_parts = explode('&', $this->user->page['query_string']); |
|
| 156 | |||
| 157 | 7 | $data = array_values($data); |
|
| 158 | 7 | for ($i = 0, $size = sizeof($data); $i < $size; $i++) |
|
| 159 | { |
||
| 160 | 7 | $row = $data[$i]; |
|
| 161 | 7 | if ($this->is_current_path($curr_page, $curr_parts, $row)) |
|
| 162 | 7 | { |
|
| 163 | 6 | $this->adjust_depth($row); |
|
| 164 | 6 | $this->current_item = $row; |
|
| 165 | 6 | return true; |
|
| 166 | } |
||
| 167 | 4 | } |
|
| 168 | |||
| 169 | 1 | $this->current_item = $this->default_current_item(); |
|
| 170 | 1 | return false; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * return void |
||
| 175 | */ |
||
| 176 | 1 | protected function default_current_item() |
|
| 177 | { |
||
| 178 | 1 | $this->max_depth = ($this->expanded) ? $this->max_depth : 0; |
|
| 179 | 1 | $this->min_depth = 0; |
|
| 180 | |||
| 181 | return array( |
||
| 182 | 1 | 'item_id' => 0, |
|
| 183 | 1 | 'parent_id' => 0, |
|
| 184 | 1 | 'left_id' => 0, |
|
| 185 | 1 | 'right_id' => 0, |
|
| 186 | 1 | 'depth' => 0, |
|
| 187 | 1 | ); |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $curr_page |
||
| 192 | * @param array $curr_parts |
||
| 193 | * @param array $row |
||
| 194 | * @return bool |
||
| 195 | */ |
||
| 196 | 7 | protected function is_current_path($curr_page, array $curr_parts, array $row) |
|
| 197 | { |
||
| 198 | 7 | return ($curr_page === ltrim($row['url_path'], './') && (!sizeof($row['url_query']) || sizeof(array_intersect($row['url_query'], $curr_parts)))) ? true : false; |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @param array $row |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | 7 | protected function is_current_item(array $row) |
|
| 206 | { |
||
| 207 | 7 | return ($row['item_id'] === $this->current_item['item_id']) ? true : false; |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * @param array $row |
||
| 212 | * @return bool |
||
| 213 | */ |
||
| 214 | 6 | protected function is_child_of_current_item(array $row) |
|
| 215 | { |
||
| 216 | 6 | return ($row['left_id'] < $this->current_item['left_id'] && $row['right_id'] > $this->current_item['right_id']) ? true : false; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Does the branch end here? |
||
| 221 | * |
||
| 222 | * @param array $row |
||
| 223 | * @param bool $is_current_item |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 7 | protected function get_leaf_node(array $row, $is_current_item) |
|
| 227 | { |
||
| 228 | 7 | return (($row['depth'] === $this->max_depth || !$this->is_child_of_current_item($row) && !$this->expanded) && !$is_current_item && $row['is_expandable']) ? $row : array(); |
|
| 229 | } |
||
| 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 | * @return bool |
||
| 249 | */ |
||
| 250 | 6 | protected function needs_adjustment($items_depth) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param array $row |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | 6 | protected function adjust_depth(array $row) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param int $item_id |
||
| 272 | * @param array $data |
||
| 273 | * @param array $leaf |
||
| 274 | * @return void |
||
| 275 | */ |
||
| 276 | 4 | protected function adjust_right_id($item_id, array &$data, array $leaf) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * @param array $data |
||
| 286 | * @param int $parent_id |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | 5 | protected function find_parents(array $data, $parent_id) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * Append session id to local, non-directory paths |
||
| 305 | * |
||
| 306 | * @param array $row |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | 7 | protected function get_full_url(array $row) |
|
| 310 | { |
||
| 319 | } |
||
| 320 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.