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 Cornford\Setter; |
||
| 8 | class Setting extends SettingBase implements SettableInterface, CacheableInterface { |
||
|
|
|||
| 9 | |||
| 10 | /** |
||
| 11 | * Set a setting by key and value |
||
| 12 | * |
||
| 13 | * @param string $key |
||
| 14 | * @param string $value |
||
| 15 | * |
||
| 16 | * @return boolean |
||
| 17 | */ |
||
| 18 | public function set($key, $value) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Get a setting by key, optionally set a default or fallback to config lookup |
||
| 40 | * |
||
| 41 | * @param string $key |
||
| 42 | * @param string $default |
||
| 43 | * |
||
| 44 | * @return string|array|boolean |
||
| 45 | */ |
||
| 46 | public function get($key, $default = null) |
||
| 47 | { |
||
| 48 | if (!$this->getUncached() && $this->cacheEnabled() && $this->cacheHas($this->attachCacheTag($key))) { |
||
| 49 | return $this->returnCache($key); |
||
| 50 | } |
||
| 51 | |||
| 52 | $results = $this->database |
||
| 53 | ->table('settings') |
||
| 54 | ->where('settings.key', '=', $key) |
||
| 55 | ->whereRaw('settings.key LIKE "' . $key . '.%"', array(), 'or') |
||
| 56 | ->lists('value', 'key'); |
||
| 57 | |||
| 58 | if ($results) { |
||
| 59 | return $this->returnResults($results, $key); |
||
| 60 | } |
||
| 61 | |||
| 62 | if ($default) { |
||
| 63 | return $default; |
||
| 64 | } |
||
| 65 | |||
| 66 | if ($this->configHas($key)) { |
||
| 67 | return $this->returnConfig($key); |
||
| 68 | } |
||
| 69 | |||
| 70 | return false; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Forget a setting by key |
||
| 75 | * |
||
| 76 | * @param string $key |
||
| 77 | * |
||
| 78 | * @return boolean |
||
| 79 | */ |
||
| 80 | public function forget($key) |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Check a setting exists by key |
||
| 97 | * |
||
| 98 | * @param string $key |
||
| 99 | * |
||
| 100 | * @return boolean |
||
| 101 | */ |
||
| 102 | public function has($key) |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Get all stored settings |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | public function all() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Clear all stored settings |
||
| 133 | * |
||
| 134 | * @return boolean |
||
| 135 | */ |
||
| 136 | public function clear() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set the expiry |
||
| 151 | * |
||
| 152 | * @param boolean|integer|Datetime $expiry |
||
| 153 | * |
||
| 154 | * @throws SettingArgumentException |
||
| 155 | * |
||
| 156 | * @return self |
||
| 157 | */ |
||
| 158 | public function expires($expiry) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Enable caching. |
||
| 167 | * |
||
| 168 | * @return self |
||
| 169 | */ |
||
| 170 | public function enableCache() |
||
| 171 | { |
||
| 172 | $this->setCacheEnabled(true); |
||
| 173 | |||
| 174 | return $this; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Disable caching. |
||
| 179 | * |
||
| 180 | * @return self |
||
| 181 | */ |
||
| 182 | public function disableCache() |
||
| 183 | { |
||
| 184 | $this->setCacheEnabled(false); |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets the uncached flag to request an item from the DB and re-cache the item. |
||
| 191 | * |
||
| 192 | * @return self |
||
| 193 | */ |
||
| 194 | public function uncached() |
||
| 195 | { |
||
| 196 | $this->setUncached(true); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set the cache expiry |
||
| 201 | * |
||
| 202 | * @param boolean|integer|Datetime $expiry |
||
| 203 | * |
||
| 204 | * @throws SettingArgumentException |
||
| 205 | * |
||
| 206 | * @return self |
||
| 207 | */ |
||
| 208 | View Code Duplication | public function cacheExpires($expiry) |
|
| 218 | |||
| 219 | } |
||
| 220 |