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 | 12 | public function __construct(\phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, $menu_items_table, $pk) |
|
| 51 | |||
| 52 | 7 | public function set_params(array $params) |
|
| 57 | |||
| 58 | /** |
||
| 59 | * |
||
| 60 | */ |
||
| 61 | 7 | public function display_list(array $data, \phpbb\template\twig\twig &$template, $handle = 'tree') |
|
| 80 | |||
| 81 | 5 | public function generate_breadcrumb(array $data) |
|
| 85 | |||
| 86 | 7 | protected function prepare_items(array &$data) |
|
| 133 | |||
| 134 | 7 | protected function set_current_item($data) |
|
| 154 | |||
| 155 | 1 | protected function default_current_item() |
|
| 168 | |||
| 169 | 7 | protected function is_current_path($curr_page, array $curr_parts, array $row) |
|
| 173 | |||
| 174 | 7 | protected function is_current_item(array $row) |
|
| 178 | |||
| 179 | 7 | protected function set_parental_depth($row, $depth, $is_current_item) |
|
| 186 | |||
| 187 | 7 | protected function close_open_tags(\phpbb\template\twig\twig &$template, $handle, $repeat) |
|
| 194 | |||
| 195 | 6 | protected function needs_adjustment($items_depth) |
|
| 199 | |||
| 200 | 6 | protected function adjust_depth(array $row) |
|
| 209 | |||
| 210 | 4 | protected function adjust_right_id($item_id, array &$data, array $leaf) |
|
| 217 | |||
| 218 | 5 | protected function find_parents(array $data, $parent_id) |
|
| 231 | } |
||
| 232 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: