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:
| 1 | <?php |
||
| 43 | class Oledrion_XoopsObjectTree |
||
| 44 | { |
||
| 45 | /**#@+ |
||
| 46 | * @access private |
||
| 47 | */ |
||
| 48 | public $_parentId; |
||
| 49 | public $_myId; |
||
| 50 | public $_rootId = null; |
||
| 51 | public $_tree = array(); |
||
| 52 | public $_objects; |
||
| 53 | |||
| 54 | /**#@-*/ |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Constructor |
||
| 58 | * |
||
| 59 | * @param array $objectArr Array of {@link XoopsObject}s |
||
| 60 | * @param string $myId field name of object ID |
||
| 61 | * @param string $parentId field name of parent object ID |
||
| 62 | * @param string $rootId field name of root object ID |
||
| 63 | **/ |
||
| 64 | public function __construct(&$objectArr, $myId, $parentId, $rootId = null) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Initialize the object |
||
| 77 | * |
||
| 78 | * @access private |
||
| 79 | **/ |
||
| 80 | public function _initialize() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get the tree |
||
| 96 | * |
||
| 97 | * @return array Associative array comprising the tree |
||
| 98 | **/ |
||
| 99 | public function getTree() |
||
| 103 | |||
| 104 | /** |
||
| 105 | * returns an object from the tree specified by its id |
||
| 106 | * |
||
| 107 | * @param string $key ID of the object to retrieve |
||
| 108 | * @return object Object within the tree |
||
| 109 | **/ |
||
| 110 | public function getByKey($key) |
||
| 114 | |||
| 115 | /** |
||
| 116 | * returns an array of all the first child object of an object specified by its id |
||
| 117 | * |
||
| 118 | * @param string $key ID of the parent object |
||
| 119 | * @return array Array of children of the parent |
||
| 120 | **/ |
||
| 121 | public function getFirstChild($key) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * returns an array of all child objects of an object specified by its id |
||
| 135 | * |
||
| 136 | * @param string $key ID of the parent |
||
| 137 | * @param array $ret (Empty when called from client) Array of children from previous recursions. |
||
| 138 | * @return array Array of child nodes. |
||
| 139 | **/ |
||
| 140 | public function getAllChild($key, $ret = array()) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * returns an array of all parent objects. |
||
| 157 | * the key of returned array represents how many levels up from the specified object |
||
| 158 | * |
||
| 159 | * @param string $key ID of the child object |
||
| 160 | * @param array $ret (empty when called from outside) Result from previous recursions |
||
| 161 | * @param int $uplevel (empty when called from outside) level of recursion |
||
| 162 | * @return array Array of parent nodes. |
||
| 163 | **/ |
||
| 164 | public function getAllParent($key, $ret = array(), $uplevel = 1) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Make options for a select box from |
||
| 179 | * |
||
| 180 | * @param string $fieldName Name of the member variable from the |
||
| 181 | * node objects that should be used as the title for the options. |
||
| 182 | * @param string $selected Value to display as selected |
||
| 183 | * @param int $key ID of the object to display as the root of select options |
||
| 184 | * @param string $ret (reference to a string when called from outside) Result from previous recursions |
||
| 185 | * @param string $prefix_orig String to indent items at deeper levels |
||
| 186 | * @param string $prefix_curr String to indent the current item |
||
| 187 | * @access private |
||
| 188 | */ |
||
| 189 | public function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '') |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Make a select box with options from the tree |
||
| 209 | * |
||
| 210 | * @param string $name Name of the select box |
||
| 211 | * @param string $fieldName Name of the member variable from the node objects that should be used as the title for the options. |
||
| 212 | * @param string $prefix String to indent deeper levels |
||
| 213 | * @param string $selected Value to display as selected |
||
| 214 | * @param bool|string $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
||
| 215 | * @param integer $key ID of the object to display as the root of select options |
||
| 216 | * @param string $additional |
||
| 217 | * @return string HTML select box |
||
| 218 | */ |
||
| 219 | public function makeSelBox( |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Internal function used by makeTreeAsArray |
||
| 247 | * @param $fieldName |
||
| 248 | * @param $key |
||
| 249 | * @param $ret |
||
| 250 | * @param $prefix_orig |
||
| 251 | * @param string $prefix_curr |
||
| 252 | */ |
||
| 253 | public function _recursiveMakeTreeAsArray($fieldName, $key, &$ret, $prefix_orig, $prefix_curr = '') |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Identical function as makeSelBox but returns an array |
||
| 269 | * |
||
| 270 | * @param string $fieldName Name of the member variable from the node objects that should be used as the title for the options. |
||
| 271 | * @param string $prefix String to indent deeper levels |
||
| 272 | * @param integer $key ID of the object to display as the root of select options |
||
| 273 | * @param null $empty |
||
| 274 | * @return array key = object ID, value = $fieldName |
||
| 275 | */ |
||
| 276 | public function makeTreeAsArray($fieldName, $prefix = '-', $key = 0, $empty = null) |
||
| 286 | } |
||
| 287 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.