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 |
||
| 41 | class MyXoopsObjectTree |
||
|
|
|||
| 42 | { |
||
| 43 | /**#@+ |
||
| 44 | * @access private |
||
| 45 | */ |
||
| 46 | public $_parentId; |
||
| 47 | public $_myId; |
||
| 48 | public $_rootId = null; |
||
| 49 | public $_tree = array(); |
||
| 50 | public $_objects; |
||
| 51 | /**#@-*/ |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Constructor |
||
| 55 | * |
||
| 56 | * @param array $objectArr Array of {@link XoopsObject}s |
||
| 57 | * @param string $myId field name of object ID |
||
| 58 | * @param string $parentId field name of parent object ID |
||
| 59 | * @param string $rootId field name of root object ID |
||
| 60 | **/ |
||
| 61 | public function __construct(&$objectArr, $myId, $parentId, $rootId = null) |
||
| 62 | { |
||
| 63 | //$this->db = xoopsDatabaseFactory::getDatabaseConnection(); |
||
| 64 | $this->_objects =& $objectArr; |
||
| 65 | $this->_myId = $myId; |
||
| 66 | $this->_parentId = $parentId; |
||
| 67 | if (isset($rootId)) { |
||
| 68 | $this->_rootId = $rootId; |
||
| 69 | } |
||
| 70 | $this->_initialize(); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Initialize the object |
||
| 75 | * |
||
| 76 | * @access private |
||
| 77 | **/ |
||
| 78 | public function _initialize() |
||
| 79 | { |
||
| 80 | foreach (array_keys($this->_objects) as $i) { |
||
| 81 | $key1 = $this->_objects[$i]->getVar($this->_myId); |
||
| 82 | $this->_tree[$key1]['obj'] = $this->_objects[$i]; |
||
| 83 | $key2 = $this->_objects[$i]->getVar($this->_parentId); |
||
| 84 | $this->_tree[$key1]['parent'] = $key2; |
||
| 85 | $this->_tree[$key2]['child'][] = $key1; |
||
| 86 | if (isset($this->_rootId)) { |
||
| 87 | $this->_tree[$key1]['root'] = $this->_objects[$i]->getVar($this->_rootId); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Get the tree |
||
| 94 | * |
||
| 95 | * @return array Associative array comprising the tree |
||
| 96 | **/ |
||
| 97 | public function &getTree() |
||
| 101 | |||
| 102 | /** |
||
| 103 | * returns an object from the tree specified by its id |
||
| 104 | * |
||
| 105 | * @param string $key ID of the object to retrieve |
||
| 106 | * |
||
| 107 | * @return object Object within the tree |
||
| 108 | **/ |
||
| 109 | public function &getByKey($key) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * returns an array of all the first child object of an object specified by its id |
||
| 116 | * |
||
| 117 | * @param string $key ID of the parent object |
||
| 118 | * |
||
| 119 | * @return array Array of children of the parent |
||
| 120 | **/ |
||
| 121 | public function getFirstChild($key) |
||
| 122 | { |
||
| 123 | $ret = array(); |
||
| 124 | if (isset($this->_tree[$key]['child'])) { |
||
| 125 | foreach ($this->_tree[$key]['child'] as $childkey) { |
||
| 126 | $ret[$childkey] = $this->_tree[$childkey]['obj']; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | return $ret; |
||
| 131 | } |
||
| 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 | * |
||
| 139 | * @return array Array of child nodes. |
||
| 140 | **/ |
||
| 141 | public function getAllChild($key, $ret = array()) |
||
| 142 | { |
||
| 143 | if (isset($this->_tree[$key]['child'])) { |
||
| 144 | foreach ($this->_tree[$key]['child'] as $childkey) { |
||
| 145 | $ret[$childkey] = $this->_tree[$childkey]['obj']; |
||
| 146 | $children = $this->getAllChild($childkey, $ret); |
||
| 147 | foreach (array_keys($children) as $newkey) { |
||
| 148 | $ret[$newkey] = $children[$newkey]; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $ret; |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * returns an array of all parent objects. |
||
| 158 | * the key of returned array represents how many levels up from the specified object |
||
| 159 | * |
||
| 160 | * @param string $key ID of the child object |
||
| 161 | * @param array $ret (empty when called from outside) Result from previous recursions |
||
| 162 | * @param int $uplevel (empty when called from outside) level of recursion |
||
| 163 | * |
||
| 164 | * @return array Array of parent nodes. |
||
| 165 | **/ |
||
| 166 | public function getAllParent($key, $ret = array(), $uplevel = 1) |
||
| 167 | { |
||
| 168 | if (isset($this->_tree[$key]['parent']) && isset($this->_tree[$this->_tree[$key]['parent']]['obj'])) { |
||
| 169 | $ret[$uplevel] = $this->_tree[$this->_tree[$key]['parent']]['obj']; |
||
| 170 | $parents = $this->getAllParent($this->_tree[$key]['parent'], $ret, $uplevel + 1); |
||
| 171 | foreach (array_keys($parents) as $newkey) { |
||
| 172 | $ret[$newkey] = $parents[$newkey]; |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | return $ret; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Make options for a select box from |
||
| 181 | * |
||
| 182 | * @param string $fieldName Name of the member variable from the |
||
| 183 | * node objects that should be used as the title for the options. |
||
| 184 | * @param string $selected Value to display as selected |
||
| 185 | * @param int $key ID of the object to display as the root of select options |
||
| 186 | * @param string $ret (reference to a string when called from outside) Result from previous recursions |
||
| 187 | * @param string $prefix_orig String to indent items at deeper levels |
||
| 188 | * @param string $prefix_curr String to indent the current item |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | @access private |
||
| 192 | */ |
||
| 193 | public function _makeSelBoxOptions($fieldName, $selected, $key, &$ret, $prefix_orig, $prefix_curr = '') |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Make a select box with options from the tree |
||
| 213 | * |
||
| 214 | * @param string $name Name of the select box |
||
| 215 | * @param string $fieldName Name of the member variable from the node objects that should be used as the title for the options. |
||
| 216 | * @param string $prefix String to indent deeper levels |
||
| 217 | * @param string $selected Value to display as selected |
||
| 218 | * @param bool $addEmptyOption Set TRUE to add an empty option with value "0" at the top of the hierarchy |
||
| 219 | * @param integer $key ID of the object to display as the root of select options |
||
| 220 | * @param string $additional |
||
| 221 | * |
||
| 222 | * @return string HTML select box |
||
| 223 | */ |
||
| 224 | public function makeSelBox($name, $fieldName, $prefix = '-', $selected = '', $addEmptyOption = false, $key = 0, $additional = '') |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Internal function used by makeTreeAsArray |
||
| 241 | * @param $fieldName |
||
| 242 | * @param $key |
||
| 243 | * @param $ret |
||
| 244 | * @param $prefix_orig |
||
| 245 | * @param string $prefix_curr |
||
| 246 | */ |
||
| 247 | public function _recursiveMakeTreeAsArray($fieldName, $key, &$ret, $prefix_orig, $prefix_curr = '') |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Identical function as makeSelBox but returns an array |
||
| 263 | * |
||
| 264 | * @param string $fieldName Name of the member variable from the node objects that should be used as the title for the options. |
||
| 265 | * @param string $prefix String to indent deeper levels |
||
| 266 | * @param integer $key ID of the object to display as the root of select options |
||
| 267 | * @param null $empty |
||
| 268 | * |
||
| 269 | * @return array key = object ID, value = $fieldName |
||
| 270 | */ |
||
| 271 | public function makeTreeAsArray($fieldName, $prefix = '-', $key = 0, $empty = null) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Enter description here... |
||
| 284 | * |
||
| 285 | * @param unknown_type $sel_id |
||
| 286 | * @param unknown_type $order |
||
| 287 | * @param unknown_type $parray |
||
| 288 | * @param unknown_type $r_prefix |
||
| 289 | * |
||
| 290 | * @return unknown |
||
| 291 | */ |
||
| 292 | // function getChildTreeArray($sel_id = 0, $order = "", $parray = array(), $r_prefix = "") |
||
| 293 | // { |
||
| 294 | // $sel_id = (int)($sel_id); |
||
| 295 | // $sql = "SELECT * FROM " . $this->table . " WHERE " . $this->pid . "=" . $sel_id . ""; |
||
| 296 | // if ($order != "") { |
||
| 297 | // $sql .= " ORDER BY $order"; |
||
| 298 | // } |
||
| 299 | // $result = $this->db->query($sql); |
||
| 300 | // $count = $this->db->getRowsNum($result); |
||
| 301 | // if ($count == 0) { |
||
| 302 | // return $parray; |
||
| 303 | // } |
||
| 304 | // while ($row = $this->db->fetchArray($result)) { |
||
| 305 | // $row['prefix'] = $r_prefix . "."; |
||
| 306 | // array_push($parray, $row); |
||
| 307 | // $parray = $this->getChildTreeArray($row[$this->id], $order, $parray, $row['prefix']); |
||
| 308 | // } |
||
| 309 | // return $parray; |
||
| 310 | // } |
||
| 311 | } |
||
| 312 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.