Complex classes like TimberMenu 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 TimberMenu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 44 | class TimberMenu extends TimberCore { |
||
| 45 | |||
| 46 | public $MenuItemClass = 'TimberMenuItem'; |
||
| 47 | public $PostClass = 'TimberPost'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @api |
||
| 51 | * @var TimberMenuItem[]|null $items you need to iterate through |
||
| 52 | */ |
||
| 53 | public $items = null; |
||
| 54 | /** |
||
| 55 | * @api |
||
| 56 | * @var integer $id the ID# of the menu, corresponding to the wp_terms table |
||
| 57 | */ |
||
| 58 | public $id; |
||
| 59 | public $ID; |
||
| 60 | /** |
||
| 61 | * @api |
||
| 62 | * @var string $name of the menu (ex: `Main Navigation`) |
||
| 63 | */ |
||
| 64 | public $name; |
||
| 65 | /** |
||
| 66 | * @var integer $id the ID# of the menu, corresponding to the wp_terms table |
||
| 67 | */ |
||
| 68 | public $term_id; |
||
| 69 | /** |
||
| 70 | * @api |
||
| 71 | * @var string $name of the menu (ex: `Main Navigation`) |
||
| 72 | */ |
||
| 73 | public $title; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param int|string $slug |
||
| 77 | */ |
||
| 78 | function __construct($slug = 0) { |
||
|
|
|||
| 79 | $locations = get_nav_menu_locations(); |
||
| 80 | if ($slug != 0 && is_numeric($slug)) { |
||
| 81 | $menu_id = $slug; |
||
| 82 | } else if (is_array($locations) && count($locations)) { |
||
| 83 | $menu_id = $this->get_menu_id_from_locations($slug, $locations); |
||
| 84 | } else if ($slug === false) { |
||
| 85 | $menu_id = false; |
||
| 86 | } else { |
||
| 87 | $menu_id = $this->get_menu_id_from_terms($slug); |
||
| 88 | } |
||
| 89 | if ($menu_id) { |
||
| 90 | $this->init($menu_id); |
||
| 91 | } else { |
||
| 92 | $this->init_as_page_menu(); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @internal |
||
| 98 | * @param int $menu_id |
||
| 99 | */ |
||
| 100 | protected function init($menu_id) { |
||
| 101 | $menu = wp_get_nav_menu_items($menu_id); |
||
| 102 | if ($menu) { |
||
| 103 | _wp_menu_item_classes_by_context($menu); |
||
| 104 | if (is_array($menu)){ |
||
| 105 | $menu = self::order_children($menu); |
||
| 106 | } |
||
| 107 | $this->items = $menu; |
||
| 108 | $menu_info = wp_get_nav_menu_object($menu_id); |
||
| 109 | $this->import($menu_info); |
||
| 110 | $this->ID = $this->term_id; |
||
| 111 | $this->id = $this->term_id; |
||
| 112 | $this->title = $this->name; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @internal |
||
| 118 | */ |
||
| 119 | protected function init_as_page_menu() { |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @internal |
||
| 135 | * @param string $slug |
||
| 136 | * @param array $locations |
||
| 137 | * @return integer |
||
| 138 | */ |
||
| 139 | protected function get_menu_id_from_locations($slug, $locations) { |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @internal |
||
| 154 | * @param int $slug |
||
| 155 | * @return int |
||
| 156 | */ |
||
| 157 | protected function get_menu_id_from_terms($slug = 0) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param array $menu_items |
||
| 180 | * @param int $parent_id |
||
| 181 | * @return TimberMenuItem|null |
||
| 182 | */ |
||
| 183 | function find_parent_item_in_menu($menu_items, $parent_id) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @internal |
||
| 193 | * @param array $items |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | protected function order_children($items) { |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return array |
||
| 229 | */ |
||
| 230 | function get_items() { |
||
| 236 | } |
||
| 237 | |||
| 239 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.