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 namespace XoopsModules\Smartobject; |
||
| 35 | class ObjectsRegistry |
||
| 36 | { |
||
| 37 | public $_registryArray; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Access the only instance of this class |
||
| 41 | * |
||
| 42 | * @return \XoopsModules\Smartobject\ObjectsRegistry |
||
| 43 | * |
||
| 44 | * @static |
||
| 45 | * @staticvar object |
||
| 46 | */ |
||
| 47 | public static function getInstance() |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Adding objects to the registry |
||
| 59 | * |
||
| 60 | * @param PersistableObjectHandler $handler of the objects to add |
||
| 61 | * @param bool|CriteriaCompo $criteria to pass to the getObjects method of the handler (with id_as_key) |
||
| 62 | * @return FALSE if an error occured |
||
| 63 | */ |
||
| 64 | public function addObjectsFromHandler(&$handler, $criteria = false) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Adding objects to the registry from an item name |
||
| 78 | * This method will fetch the handler of the item / module and call the addObjectsFromHandler |
||
| 79 | * |
||
| 80 | * @param string $item name of the item |
||
| 81 | * @param bool|string $modulename name of the module |
||
| 82 | * @param bool|CriteriaCompo $criteria to pass to the getObjects method of the handler (with id_as_key) |
||
| 83 | * @return FALSE if an error occured |
||
| 84 | */ |
||
| 85 | public function addObjectsFromItemName($item, $modulename = false, $criteria = false) |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Fetching objects from the registry |
||
| 109 | * |
||
| 110 | * @param string $itemname |
||
| 111 | * @param string $modulename |
||
| 112 | * |
||
| 113 | * @return false|array the requested objects or FALSE if they don't exists in the registry |
||
| 114 | */ |
||
| 115 | View Code Duplication | public function getObjects($itemname, $modulename) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * Fetching objects from the registry, as a list: objectid => identifier |
||
| 141 | * |
||
| 142 | * @param string $itemname |
||
| 143 | * @param string $modulename |
||
| 144 | * |
||
| 145 | * @return false|array the requested objects or FALSE if they don't exists in the registry |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function getList($itemname, $modulename) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Retreive a single object |
||
| 173 | * |
||
| 174 | * @param string $itemname |
||
| 175 | * @param string $key |
||
| 176 | * |
||
| 177 | * @param bool $modulename |
||
| 178 | * @return false|\XoopsObject the requestd object or FALSE if they don't exists in the registry |
||
| 179 | */ |
||
| 180 | public function getSingleObject($itemname, $key, $modulename = false) |
||
| 202 | } |
||
| 203 |
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.