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 DBCollection 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 DBCollection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | class DBCollection extends Collection |
||
| 5 | { |
||
| 6 | const TABLE_NAME = ''; // Name of SQL table containing items |
||
| 7 | const ITEM_TYPE = ''; // Class of items in collection |
||
| 8 | const DB_CONFIG = ''; // Database configuration identifier |
||
| 9 | const PARENT_ID_NAME = 'parent_id'; // Name of the field referencing to parent_id |
||
| 10 | const PARENT_OBJECT_TYPE = ''; // Parent object type |
||
| 11 | |||
| 12 | |||
| 13 | protected $lazyLoad = false; |
||
| 14 | protected $parentId; // Id of the parent |
||
| 15 | protected $parentFilterName; // Name of field used for filtering |
||
| 16 | protected $parentFilterType; // Value of filter |
||
| 17 | |||
| 18 | |||
| 19 | |||
| 20 | protected $itemOffset = 0; |
||
| 21 | |||
| 22 | public function setLazyLoad($lazyLoad) |
||
| 28 | |||
| 29 | |||
| 30 | public function purgeItems() |
||
| 36 | |||
| 37 | |||
| 38 | /** |
||
| 39 | * Load entire table into collection |
||
| 40 | * @return Collection Loaded collection |
||
| 41 | */ |
||
| 42 | public static function loadAll() |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Static wrapper for loadFromSql |
||
| 74 | * @param string $sql SQL Statement |
||
| 75 | * @param array $sqlParams SQL Parameters |
||
| 76 | * @return Suricate\Collection Loaded collection |
||
| 77 | */ |
||
| 78 | public static function buildFromSql($sql, $sqlParams = array()) |
||
| 87 | |||
| 88 | View Code Duplication | public function loadFromSql($sql, $sqlParams = array()) |
|
| 106 | |||
| 107 | View Code Duplication | public function lazyLoadFromSql($sql, $sqlParams = array()) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Load items linked to a parentId |
||
| 128 | * @param mixed $parentId Parent id description |
||
| 129 | * @param string $parentIdField Name of parent id referencing field |
||
| 130 | * @param closure $validate Callback use to validate add to items collection |
||
| 131 | */ |
||
| 132 | public static function loadForParentId($parentId, $parentIdField = null, $validate = null) |
||
| 177 | |||
| 178 | public function setParentIdForAll($parentId) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Load Parent Item, if item type is defined |
||
| 188 | * @return item type Parent Object |
||
| 189 | */ |
||
| 190 | public function loadParent() |
||
| 206 | |||
| 207 | public function craftItem($itemData) |
||
| 228 | |||
| 229 | public function save() |
||
| 254 | |||
| 255 | public function addItem(Interfaces\IDBObject $item) |
||
| 266 | |||
| 267 | public function getItemsType() |
||
| 271 | |||
| 272 | public function getParentIdName() |
||
| 276 | |||
| 277 | public function getParentId() |
||
| 281 | } |
||
| 282 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.