| Total Complexity | 40 |
| Total Lines | 207 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like LSX_Bootstrap_Navwalker 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.
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 LSX_Bootstrap_Navwalker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 27 | class LSX_Bootstrap_Navwalker extends Walker_Nav_Menu { |
||
| 28 | |||
| 29 | /** |
||
|
|
|||
| 30 | * Used to append additional content. |
||
| 31 | * |
||
| 32 | * @see Walker::start_lvl() |
||
| 33 | * @since 3.0.0 |
||
| 34 | * |
||
| 35 | * @param string $output Passed by reference. Used to append additional content. |
||
| 36 | * @param int $depth Depth of page. Used for padding. |
||
| 37 | */ |
||
| 38 | public function start_lvl( &$output, $depth = 0, $args = array() ) { |
||
| 39 | $indent = str_repeat( "\t", $depth ); |
||
| 40 | $output .= "\n$indent<ul role=\"menu\" class=\" dropdown-menu\">\n"; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Used to append additional content. |
||
| 45 | * @param string $item Passed by reference. |
||
| 46 | */ |
||
| 47 | public function filter_default_pages( &$item ) { |
||
| 48 | return $item; |
||
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @see Walker::start_el() |
||
| 53 | * @since 3.0.0 |
||
| 54 | * |
||
| 55 | * @param string $output Passed by reference. Used to append additional content. |
||
| 56 | * @param object $item Menu item data object. |
||
| 57 | * @param int $depth Depth of menu item. Used for padding. |
||
| 58 | * @param int $current_page Menu item ID. |
||
| 59 | * @param object $args |
||
| 60 | */ |
||
| 61 | public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) { |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Traverse elements to create list from elements. |
||
| 154 | * |
||
| 155 | * Display one element if the element doesn't have any children otherwise, |
||
| 156 | * display the element and its children. Will only traverse up to the max |
||
| 157 | * depth and no ignore elements under that depth. |
||
| 158 | * |
||
| 159 | * This method shouldn't be called directly, use the walk() method instead. |
||
| 160 | * |
||
| 161 | * @see Walker::start_el() |
||
| 162 | * @since 2.5.0 |
||
| 163 | * |
||
| 164 | * @param object $element Data object |
||
| 165 | * @param array $children_elements List of elements to continue traversing. |
||
| 166 | * @param int $max_depth Max depth to traverse. |
||
| 167 | * @param int $depth Depth of current element. |
||
| 168 | * @param array $args |
||
| 169 | * @param string $output Passed by reference. Used to append additional content. |
||
| 170 | * @return null Null on failure with no changes to parameters. |
||
| 171 | */ |
||
| 172 | public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
||
| 173 | if ( ! $element ) { |
||
| 174 | return; |
||
| 175 | } |
||
| 176 | |||
| 177 | $id_field = $this->db_fields['id']; |
||
| 178 | |||
| 179 | if ( is_object( $args[0] ) ) { |
||
| 180 | $args[0]->has_children = ! empty( $children_elements[ $element->$id_field ] ); |
||
| 181 | } |
||
| 182 | |||
| 183 | parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Menu Fallback |
||
| 188 | * ============= |
||
| 189 | * If this function is assigned to the wp_nav_menu's fallback_cb variable |
||
| 190 | * and a manu has not been assigned to the theme location in the WordPress |
||
| 191 | * menu manager the function with display nothing to a non-logged in user, |
||
| 192 | * and will add a link to the WordPress menu manager if logged in as an admin. |
||
| 193 | * |
||
| 194 | * @param array $args passed from the wp_nav_menu function. |
||
| 195 | * |
||
| 196 | */ |
||
| 197 | public static function fallback( $args ) { |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | } |
||
| 240 |