Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Walker 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 Walker, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Walker { |
||
| 15 | /** |
||
| 16 | * What the class handles. |
||
| 17 | * |
||
| 18 | * @since 2.1.0 |
||
| 19 | * @access public |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | public $tree_type; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * DB fields to use. |
||
| 26 | * |
||
| 27 | * @since 2.1.0 |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | public $db_fields; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Max number of pages walked by the paged walker |
||
| 34 | * |
||
| 35 | * @since 2.7.0 |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | public $max_pages = 1; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Whether the current element has children or not. |
||
| 42 | * |
||
| 43 | * To be used in start_el(). |
||
| 44 | * |
||
| 45 | * @since 4.0.0 |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | public $has_children; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Starts the list before the elements are added. |
||
| 52 | * |
||
| 53 | * The $args parameter holds additional values that may be used with the child |
||
| 54 | * class methods. This method is called at the start of the output list. |
||
| 55 | * |
||
| 56 | * @since 2.1.0 |
||
| 57 | * @abstract |
||
| 58 | * |
||
| 59 | * @param string $output Passed by reference. Used to append additional content. |
||
| 60 | * @param int $depth Depth of the item. |
||
| 61 | * @param array $args An array of additional arguments. |
||
| 62 | */ |
||
| 63 | public function start_lvl( &$output, $depth = 0, $args = array() ) {} |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Ends the list of after the elements are added. |
||
| 67 | * |
||
| 68 | * The $args parameter holds additional values that may be used with the child |
||
| 69 | * class methods. This method finishes the list at the end of output of the elements. |
||
| 70 | * |
||
| 71 | * @since 2.1.0 |
||
| 72 | * @abstract |
||
| 73 | * |
||
| 74 | * @param string $output Passed by reference. Used to append additional content. |
||
| 75 | * @param int $depth Depth of the item. |
||
| 76 | * @param array $args An array of additional arguments. |
||
| 77 | */ |
||
| 78 | public function end_lvl( &$output, $depth = 0, $args = array() ) {} |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Start the element output. |
||
| 82 | * |
||
| 83 | * The $args parameter holds additional values that may be used with the child |
||
| 84 | * class methods. Includes the element output also. |
||
| 85 | * |
||
| 86 | * @since 2.1.0 |
||
| 87 | * @abstract |
||
| 88 | * |
||
| 89 | * @param string $output Passed by reference. Used to append additional content. |
||
| 90 | * @param object $object The data object. |
||
| 91 | * @param int $depth Depth of the item. |
||
| 92 | * @param array $args An array of additional arguments. |
||
| 93 | * @param int $current_object_id ID of the current item. |
||
| 94 | */ |
||
| 95 | public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {} |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Ends the element output, if needed. |
||
| 99 | * |
||
| 100 | * The $args parameter holds additional values that may be used with the child class methods. |
||
| 101 | * |
||
| 102 | * @since 2.1.0 |
||
| 103 | * @abstract |
||
| 104 | * |
||
| 105 | * @param string $output Passed by reference. Used to append additional content. |
||
| 106 | * @param object $object The data object. |
||
| 107 | * @param int $depth Depth of the item. |
||
| 108 | * @param array $args An array of additional arguments. |
||
| 109 | */ |
||
| 110 | public function end_el( &$output, $object, $depth = 0, $args = array() ) {} |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Traverse elements to create list from elements. |
||
| 114 | * |
||
| 115 | * Display one element if the element doesn't have any children otherwise, |
||
| 116 | * display the element and its children. Will only traverse up to the max |
||
| 117 | * depth and no ignore elements under that depth. It is possible to set the |
||
| 118 | * max depth to include all depths, see walk() method. |
||
| 119 | * |
||
| 120 | * This method should not be called directly, use the walk() method instead. |
||
| 121 | * |
||
| 122 | * @since 2.5.0 |
||
| 123 | * |
||
| 124 | * @param object $element Data object. |
||
| 125 | * @param array $children_elements List of elements to continue traversing. |
||
| 126 | * @param int $max_depth Max depth to traverse. |
||
| 127 | * @param int $depth Depth of current element. |
||
| 128 | * @param array $args An array of arguments. |
||
| 129 | * @param string $output Passed by reference. Used to append additional content. |
||
| 130 | */ |
||
| 131 | public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Display array of elements hierarchically. |
||
| 177 | * |
||
| 178 | * Does not assume any existing order of elements. |
||
| 179 | * |
||
| 180 | * $max_depth = -1 means flatly display every element. |
||
| 181 | * $max_depth = 0 means display all levels. |
||
| 182 | * $max_depth > 0 specifies the number of display levels. |
||
| 183 | * |
||
| 184 | * @since 2.1.0 |
||
| 185 | * |
||
| 186 | * @param array $elements An array of elements. |
||
| 187 | * @param int $max_depth The maximum hierarchical depth. |
||
| 188 | * @return string The hierarchical item output. |
||
| 189 | */ |
||
| 190 | public function walk( $elements, $max_depth ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * paged_walk() - produce a page of nested elements |
||
| 262 | * |
||
| 263 | * Given an array of hierarchical elements, the maximum depth, a specific page number, |
||
| 264 | * and number of elements per page, this function first determines all top level root elements |
||
| 265 | * belonging to that page, then lists them and all of their children in hierarchical order. |
||
| 266 | * |
||
| 267 | * $max_depth = 0 means display all levels. |
||
| 268 | * $max_depth > 0 specifies the number of display levels. |
||
| 269 | * |
||
| 270 | * @since 2.7.0 |
||
| 271 | * |
||
| 272 | * @param array $elements |
||
| 273 | * @param int $max_depth The maximum hierarchical depth. |
||
| 274 | * @param int $page_num The specific page number, beginning with 1. |
||
| 275 | * @param int $per_page |
||
| 276 | * @return string XHTML of the specified page of elements |
||
| 277 | */ |
||
| 278 | public function paged_walk( $elements, $max_depth, $page_num, $per_page ) { |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Calculates the total number of root elements. |
||
| 386 | * |
||
| 387 | * @since 2.7.0 |
||
| 388 | * @access public |
||
| 389 | * |
||
| 390 | * @param array $elements Elements to list. |
||
| 391 | * @return int Number of root elements. |
||
| 392 | */ |
||
| 393 | public function get_number_of_root_elements( $elements ){ |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Unset all the children for a given top level element. |
||
| 406 | * |
||
| 407 | * @param object $e |
||
| 408 | * @param array $children_elements |
||
| 409 | */ |
||
| 410 | public function unset_children( $e, &$children_elements ){ |
||
| 424 | |||
| 425 | } // Walker |
||
| 426 |
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: