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 MartinHotelCityHandler 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 MartinHotelCityHandler, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
79 | class MartinHotelCityHandler extends XoopsObjectHandler |
||
80 | { |
||
81 | /** |
||
82 | * create a new hotel city |
||
83 | * @param bool $isNew flag the new objects as "new"? |
||
84 | * @return object HotelCity |
||
85 | */ |
||
86 | public function &create($isNew = true) |
||
95 | |||
96 | /** |
||
97 | * retrieve a hotel city |
||
98 | * |
||
99 | * @param int $id hotelcityid of the hotelcity |
||
100 | * @return mixed reference to the {@link HotelCity} object, FALSE if failed |
||
101 | */ |
||
102 | View Code Duplication | public function &get($id) |
|
119 | |||
120 | /** |
||
121 | * @得到列表 |
||
122 | * @method: |
||
123 | * @license http://www.blags.org/ |
||
124 | * @created :2010年05月23日 14时59分 |
||
125 | * @copyright 1997-2010 The Martin Group |
||
126 | * @author Martin <[email protected]> |
||
127 | * @param int $limit |
||
128 | * @param int $start |
||
129 | * @param int $city_parentid |
||
130 | * @param string $sort |
||
131 | * @param string $order |
||
132 | * @param bool $id_as_key |
||
133 | * @return array |
||
134 | */ |
||
135 | public function &getHotelCitys($limit = 0, $start = 0, $city_parentid = 0, $sort = 'city_id', $order = 'ASC', $id_as_key = true) |
||
151 | |||
152 | /** |
||
153 | * insert a new hotelcity in the database |
||
154 | * |
||
155 | * @param object $hotelcity reference to the {@link HotelCity} object |
||
156 | * @param bool $force |
||
157 | * @return bool FALSE if failed, TRUE if already present and unchanged or successful |
||
158 | */ |
||
159 | View Code Duplication | public function insert(&$hotelcity, $force = false) |
|
214 | |||
215 | /** |
||
216 | * @删除一个城市 |
||
217 | * @method:delete(city_id) |
||
218 | * @license http://www.blags.org/ |
||
219 | * @created :2010年05月21日 20时40分 |
||
220 | * @copyright 1997-2010 The Martin Group |
||
221 | * @author Martin <[email protected]> |
||
222 | * @param object $hotelcity |
||
223 | * @param bool $force |
||
224 | * @return bool|void |
||
225 | */ |
||
226 | public function delete(&$hotelcity, $force = false) |
||
248 | |||
249 | /** |
||
250 | * @ 得到所有子类 |
||
251 | * @license http://www.blags.org/ |
||
252 | * @created :2010年05月21日 20时40分 |
||
253 | * @copyright 1997-2010 The Martin Group |
||
254 | * @author Martin <[email protected]> |
||
255 | * @param $city_id |
||
256 | * @return array |
||
257 | */ |
||
258 | public function getCityIds($city_id) |
||
283 | |||
284 | /** |
||
285 | * delete hotel cities matching a set of conditions |
||
286 | * |
||
287 | * @param object $criteria {@link CriteriaElement} |
||
288 | * @return bool FALSE if deletion failed |
||
289 | */ |
||
290 | View Code Duplication | public function deleteAll($criteria = null) |
|
302 | |||
303 | /** |
||
304 | * count hotel cities matching a condition |
||
305 | * |
||
306 | * @param object $criteria {@link CriteriaElement} to match |
||
307 | * @return int count of categories |
||
308 | */ |
||
309 | View Code Duplication | public function getCount($criteria = null) |
|
323 | |||
324 | /** |
||
325 | * @得到城市 |
||
326 | * @license http://www.blags.org/ |
||
327 | * @created :2010年05月21日 20时40分 |
||
328 | * @copyright 1997-2010 The Martin Group |
||
329 | * @author Martin <[email protected]> |
||
330 | * @param null $criteria |
||
331 | * @param bool $id_as_key |
||
332 | * @return array |
||
333 | */ |
||
334 | View Code Duplication | public function &getObjects($criteria = null, $id_as_key = false) |
|
376 | |||
377 | /** |
||
378 | * @get city tree |
||
379 | * @license http://www.blags.org/ |
||
380 | * @created :2010年05月29日 11时31分 |
||
381 | * @copyright 1997-2010 The Martin Group |
||
382 | * @author Martin <[email protected]> |
||
383 | * @param $name |
||
384 | * @param $city_id |
||
385 | * @param string $prefix |
||
386 | * @return string |
||
387 | */ |
||
388 | public function &getTree($name, $city_id, $prefix = '--') |
||
400 | } |
||
401 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.