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 defined('SYSPATH') or die('No direct access allowed.'); |
||
| 14 | class Cache_Core |
||
| 15 | { |
||
| 16 | protected static $instances = array(); |
||
| 17 | |||
| 18 | // For garbage collection |
||
| 19 | protected static $loaded; |
||
| 20 | |||
| 21 | // Configuration |
||
| 22 | protected $config; |
||
| 23 | |||
| 24 | // Driver object |
||
| 25 | protected $driver; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns a singleton instance of Cache. |
||
| 29 | * |
||
| 30 | * @param string configuration |
||
| 31 | * @return Cache_Core |
||
| 32 | */ |
||
| 33 | View Code Duplication | public static function & instance($config = false) |
|
| 42 | |||
| 43 | /** |
||
| 44 | * Loads the configured driver and validates it. |
||
| 45 | * |
||
| 46 | * @param array|string custom configuration or config group name |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | public function __construct($config = false) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Fetches a cache by id. NULL is returned when a cache item is not found. |
||
| 106 | * |
||
| 107 | * @param string cache id |
||
| 108 | * @return mixed cached data or NULL |
||
| 109 | */ |
||
| 110 | public function get($id) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Fetches all of the caches for a given tag. An empty array will be |
||
| 120 | * returned when no matching caches are found. |
||
| 121 | * |
||
| 122 | * @param string cache tag |
||
| 123 | * @return array all cache items matching the tag |
||
| 124 | */ |
||
| 125 | public function find($tag) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Set a cache item by id. Tags may also be added and a custom lifetime |
||
| 132 | * can be set. Non-string data is automatically serialized. |
||
| 133 | * |
||
| 134 | * @param string unique cache id |
||
| 135 | * @param mixed data to cache |
||
| 136 | * @param array|string tags for this item |
||
| 137 | * @param integer number of seconds until the cache expires |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | public function set($id, $data, $tags = null, $lifetime = null) |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Delete a cache item by id. |
||
| 159 | * |
||
| 160 | * @param string cache id |
||
| 161 | * @return boolean |
||
| 162 | */ |
||
| 163 | public function delete($id) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Delete all cache items with a given tag. |
||
| 173 | * |
||
| 174 | * @param string cache tag name |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | public function delete_tag($tag) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Delete ALL cache items items. |
||
| 184 | * |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | public function delete_all() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Replaces troublesome characters with underscores. |
||
| 194 | * |
||
| 195 | * @param string cache id |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | protected function sanitize_id($id) |
||
| 203 | } // End Cache |
||
| 204 |
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.