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 smarttree 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 smarttree, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class smarttree |
||
|
|||
36 | { |
||
37 | public $table; //table with parent-child structure |
||
38 | public $id; //name of unique id for records in table $table |
||
39 | public $pid; // name of parent id used in table $table |
||
40 | public $order; //specifies the order of query results |
||
41 | public $title; // name of a field in table $table which will be used when selection box and paths are generated |
||
42 | public $db; |
||
43 | |||
44 | //constructor of class SmartTree |
||
45 | //sets the names of table, unique id, and parend id |
||
46 | /** |
||
47 | * SmartTree constructor. |
||
48 | * @param $table_name |
||
49 | * @param $id_name |
||
50 | * @param $pid_name |
||
51 | */ |
||
52 | public function __construct($table_name, $id_name, $pid_name) |
||
59 | |||
60 | // returns an array of first child objects for a given id($sel_id) |
||
61 | /** |
||
62 | * @param $sel_id |
||
63 | * @param string $order |
||
64 | * @return array |
||
65 | */ |
||
66 | public function getFirstChild($sel_id, $order = '') |
||
84 | |||
85 | // returns an array of all FIRST child ids of a given id($sel_id) |
||
86 | /** |
||
87 | * @param $sel_id |
||
88 | * @return array |
||
89 | */ |
||
90 | public function getFirstChildId($sel_id) |
||
104 | |||
105 | //returns an array of ALL child ids for a given id($sel_id) |
||
106 | /** |
||
107 | * @param $sel_id |
||
108 | * @param string $order |
||
109 | * @param array $idarray |
||
110 | * @return array |
||
111 | */ |
||
112 | View Code Duplication | public function getAllChildId($sel_id, $order = '', $idarray = array()) |
|
130 | |||
131 | //returns an array of ALL parent ids for a given id($sel_id) |
||
132 | /** |
||
133 | * @param $sel_id |
||
134 | * @param string $order |
||
135 | * @param array $idarray |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getAllParentId($sel_id, $order = '', $idarray = array()) |
||
154 | |||
155 | //generates path from the root id to a given id($sel_id) |
||
156 | // the path is delimetered with "/" |
||
157 | /** |
||
158 | * @param $sel_id |
||
159 | * @param $title |
||
160 | * @param string $path |
||
161 | * @return string |
||
162 | */ |
||
163 | public function getPathFromId($sel_id, $title, $path = '') |
||
180 | |||
181 | //makes a nicely ordered selection box |
||
182 | //$preset_id is used to specify a preselected item |
||
183 | //set $none to 1 to add a option with value 0 |
||
184 | /** |
||
185 | * @param $title |
||
186 | * @param string $order |
||
187 | * @param int $preset_id |
||
188 | * @param int $none |
||
189 | * @param string $sel_name |
||
190 | * @param string $onchange |
||
191 | * @param bool $multiple |
||
192 | */ |
||
193 | public function makeMySelBox($title, $order = '', $preset_id = 0, $none = 0, $sel_name = '', $onchange = '', $multiple = false) |
||
272 | |||
273 | //generates nicely formatted linked path from the root id to a given id |
||
274 | /** |
||
275 | * @param $sel_id |
||
276 | * @param $title |
||
277 | * @param $funcURL |
||
278 | * @param string $path |
||
279 | * @return string |
||
280 | */ |
||
281 | public function getNicePathFromId($sel_id, $title, $funcURL, $path = '') |
||
299 | |||
300 | //generates id path from the root id to a given id |
||
301 | // the path is delimetered with "/" |
||
302 | /** |
||
303 | * @param $sel_id |
||
304 | * @param string $path |
||
305 | * @return string |
||
306 | */ |
||
307 | public function getIdPathFromId($sel_id, $path = '') |
||
322 | |||
323 | /** |
||
324 | * @param int $sel_id |
||
325 | * @param string $order |
||
326 | * @param array $parray |
||
327 | * @return array |
||
328 | */ |
||
329 | View Code Duplication | public function getAllChild($sel_id = 0, $order = '', $parray = array()) |
|
347 | |||
348 | /** |
||
349 | * @param int $sel_id |
||
350 | * @param string $order |
||
351 | * @param array $parray |
||
352 | * @param string $r_prefix |
||
353 | * @return array |
||
354 | */ |
||
355 | public function getChildTreeArray($sel_id = 0, $order = '', $parray = array(), $r_prefix = '') |
||
374 | } |
||
375 |
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.