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 |
||
| 35 | class Settings implements ConfigContract |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * The tag for the cache. |
||
| 39 | * |
||
| 40 | * @var string |
||
| 41 | */ |
||
| 42 | public static $cache_tag = "Settings"; |
||
| 43 | /** |
||
| 44 | * The amount of time in minutes to store values in cache |
||
| 45 | * |
||
| 46 | * @var int |
||
| 47 | */ |
||
| 48 | private $cache_time; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Settings constructor. |
||
| 52 | */ |
||
| 53 | public function __construct() |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set a key value pair into the Settings store. |
||
| 60 | * |
||
| 61 | * @param string $key A . separated path to this setting |
||
| 62 | * @param array|null $value A value or an array. If value is an array it will be converted to a . separate path(s) concatinated onto the given key |
||
| 63 | * @throws \Exception |
||
| 64 | */ |
||
| 65 | public function set($key, $value = null) |
||
| 66 | { |
||
| 67 | if (is_array($value)) { |
||
| 68 | // repeat until value contains no arrays |
||
| 69 | foreach ($value as $k => $v) { |
||
| 70 | if (!empty($key)) { |
||
| 71 | if (is_string($k) && !str_contains($k, '.') && DbConfig::exactKey($key)->exists() && DbConfig::key($key)->count() == 1) { |
||
|
|
|||
| 72 | // check that we aren't trying to set an array onto an existing value only setting |
||
| 73 | throw new \Exception("Attempting to set array value to existing non-array value at the key '" . $key . "'"); |
||
| 74 | } |
||
| 75 | else { |
||
| 76 | // we are not at the leaf yet, add this chunk to the key and recurse |
||
| 77 | $this->set($key . '.' . $k, $v); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | else { |
||
| 81 | // a leaf, recurse one last time |
||
| 82 | $this->set($k, $v); |
||
| 83 | } |
||
| 84 | } |
||
| 85 | } |
||
| 86 | else { |
||
| 87 | // make sure we can save this |
||
| 88 | if ($this->isReadOnly($key)) { |
||
| 89 | throw new \Exception("The setting '" . $key . "' is read only"); |
||
| 90 | } |
||
| 91 | |||
| 92 | // flush the cache and save the value in db and cache |
||
| 93 | $this->flush($key); |
||
| 94 | DbConfig::updateOrCreate(['config_name' => $key], ['config_value' => $value]); |
||
| 95 | Cache::tags(self::$cache_tag)->put($key, $value, $this->cache_time); |
||
| 96 | } |
||
| 97 | |||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get a value from the Settings store. |
||
| 102 | * |
||
| 103 | * @param string $key A full or partial . separated key. |
||
| 104 | * @param null $default If the key isn't found, return this value. By default undefined keys return null. |
||
| 105 | * @return mixed If the $key is a full path, a bare value will be returned. If it is a partial path, a nested array will be retuned. |
||
| 106 | */ |
||
| 107 | public function get($key, $default = null) |
||
| 108 | { |
||
| 109 | // return value from cache or fetch it and return it |
||
| 110 | return Cache::tags(self::$cache_tag)->remember($key, $this->cache_time, function () use ($key, $default) { |
||
| 111 | // fetch the value from config.php first |
||
| 112 | if (Config::has('config.' . $key)) { |
||
| 113 | $config_data = Config::get('config.' . $key, $default); |
||
| 114 | if (!is_array($config_data)) { |
||
| 115 | // return the value from config.php if it is a value |
||
| 116 | return $config_data; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | // fetch the value from the database |
||
| 121 | $db_data = DbConfig::key($key)->get(['config_name', 'config_value']); |
||
| 122 | |||
| 123 | if (count($db_data) == 1 && $db_data->first()->config_name == $key) { |
||
| 124 | // return a value if we are getting one item |
||
| 125 | return $db_data->first()->config_value; |
||
| 126 | } |
||
| 127 | elseif (count($db_data) >= 1) { |
||
| 128 | // convert the collection to an array |
||
| 129 | $result = self::collectionToArray($db_data, $key); |
||
| 130 | |||
| 131 | // if we have config_data, merge them |
||
| 132 | if (isset($config_data)) { |
||
| 133 | return array_replace_recursive($result, $config_data); |
||
| 134 | } |
||
| 135 | else { |
||
| 136 | return $result; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | // we couldn't find the key, return the default |
||
| 140 | return $default; |
||
| 141 | }); |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Convert an Eloquent Collection into a nested array |
||
| 146 | * |
||
| 147 | * @param $data \Illuminate\Database\Eloquent\Collection The Collection. |
||
| 148 | * @param string $prefix Path to prepend. Do not include trailing . |
||
| 149 | * @return array The resulting nested array. |
||
| 150 | */ |
||
| 151 | private static function collectionToArray($data, $prefix = "") |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Check if the key is defined in the Settings store. |
||
| 173 | * |
||
| 174 | * @param string $key Only full paths will return true. |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | public function has($key) |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Check if the key is read only. This is when the setting is defined in config.php. |
||
| 184 | * |
||
| 185 | * @param string $key The path to check |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function isReadOnly($key) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Forget a key and all children |
||
| 195 | * This cannot forget variables set in config.php |
||
| 196 | * |
||
| 197 | * @param $key string Explicit key to forget |
||
| 198 | */ |
||
| 199 | public function forget($key) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Get all settings defined in the Settings store. |
||
| 216 | * |
||
| 217 | * @return array A nested array of all settings. |
||
| 218 | */ |
||
| 219 | public function all() |
||
| 226 | |||
| 227 | // ---- Local Utility functions ---- |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Clear the settings cache. |
||
| 231 | * If path is set, only clear the path and it's parents. |
||
| 232 | * This will not clear children. |
||
| 233 | * |
||
| 234 | * @param string $key The path to clear. |
||
| 235 | */ |
||
| 236 | public function flush($key = null) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Prepend a value onto an array configuration value. |
||
| 254 | * |
||
| 255 | * @param string $key |
||
| 256 | * @param mixed $value |
||
| 257 | * @return void |
||
| 258 | */ |
||
| 259 | public function prepend($key, $value) |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Push a value onto an array configuration value. |
||
| 281 | * |
||
| 282 | * @param string $key |
||
| 283 | * @param mixed $value |
||
| 284 | * @return void |
||
| 285 | */ |
||
| 286 | public function push($key, $value) |
||
| 304 | } |
||
| 305 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.