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 |
||
| 38 | class Redis |
||
| 39 | { |
||
| 40 | /** |
||
| 41 | * Default config |
||
| 42 | * |
||
| 43 | * @var array |
||
| 44 | */ |
||
| 45 | protected $redisConf = [ |
||
| 46 | 'host' => '127.0.0.1', |
||
| 47 | 'password' => null, |
||
| 48 | 'port' => 6379, |
||
| 49 | 'timeout' => 0, |
||
| 50 | 'database' => 0, |
||
| 51 | ]; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Redis connection |
||
| 55 | * |
||
| 56 | * @var Redis |
||
| 57 | */ |
||
| 58 | protected $redis; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Class constructor |
||
| 62 | * |
||
| 63 | * Setup Redis |
||
| 64 | * |
||
| 65 | * Loads Redis config file if present. Will halt execution |
||
| 66 | * if a Redis connection can't be established. |
||
| 67 | * |
||
| 68 | * @param array $config |
||
| 69 | * |
||
| 70 | * @throws \Kotori\Exception\CacheException |
||
| 71 | */ |
||
| 72 | public function __construct($config = []) |
||
| 73 | { |
||
| 74 | if (!$this->isSupported()) { |
||
| 75 | throw new CacheException('Failed to create Redis object; extension not loaded?'); |
||
| 76 | } |
||
| 77 | |||
| 78 | if (empty($config)) { |
||
| 79 | $config = Container::get('config')->get('cache'); |
||
| 80 | $config = array_merge($this->redisConf, $config); |
||
| 81 | } |
||
| 82 | |||
| 83 | $this->redis = new \Redis(); |
||
|
|
|||
| 84 | |||
| 85 | try { |
||
| 86 | if (!$this->redis->connect($config['host'], ($config['host'][0] === '/' ? 0 : $config['port']), $config['timeout'])) { |
||
| 87 | throw new CacheException('Redis connection failed. Check your configuration.'); |
||
| 88 | } |
||
| 89 | |||
| 90 | View Code Duplication | if (isset($config['password']) && !$this->redis->auth($config['password'])) { |
|
| 91 | throw new CacheException('Redis authentication failed.'); |
||
| 92 | } |
||
| 93 | |||
| 94 | View Code Duplication | if (isset($config['database']) && $config['database'] > 0 && !$this->redis->select($config['database'])) { |
|
| 95 | throw new CacheException('Redis select database failed.'); |
||
| 96 | } |
||
| 97 | } catch (RedisException $e) { |
||
| 98 | throw new CacheException('Redis connection refused (' . $e->getMessage() . ')'); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Get cache |
||
| 104 | * |
||
| 105 | * @param string $key |
||
| 106 | * @return mixed |
||
| 107 | */ |
||
| 108 | public function get($key) |
||
| 109 | { |
||
| 110 | $value = $this->redis->hMGet($key, ['type', 'value']); |
||
| 111 | |||
| 112 | if (!isset($value['type'], $value['value']) || $value['value'] === false) { |
||
| 113 | return false; |
||
| 114 | } |
||
| 115 | |||
| 116 | switch ($value['type']) { |
||
| 117 | case 'array': |
||
| 118 | case 'object': |
||
| 119 | return unserialize($value['value']); |
||
| 120 | case 'boolean': |
||
| 121 | case 'integer': |
||
| 122 | case 'double': // Yes, 'double' is returned and NOT 'float' |
||
| 123 | case 'string': |
||
| 124 | case 'NULL': |
||
| 125 | return settype($value['value'], $value['type']) |
||
| 126 | ? $value['value'] |
||
| 127 | : false; |
||
| 128 | case 'resource': |
||
| 129 | default: |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Save cache |
||
| 136 | * |
||
| 137 | * @param string $key |
||
| 138 | * @param mixed $value |
||
| 139 | * @param int $ttl |
||
| 140 | * @return boolean |
||
| 141 | */ |
||
| 142 | public function set($key, $value, $ttl = 60) |
||
| 143 | { |
||
| 144 | $dataType = gettype($value); |
||
| 145 | |||
| 146 | switch ($dataType) { |
||
| 147 | case 'array': |
||
| 148 | case 'object': |
||
| 149 | $value = serialize($value); |
||
| 150 | break; |
||
| 151 | case 'boolean': |
||
| 152 | case 'integer': |
||
| 153 | case 'double': // Yes, 'double' is returned and NOT 'float' |
||
| 154 | case 'string': |
||
| 155 | case 'NULL': |
||
| 156 | break; |
||
| 157 | case 'resource': |
||
| 158 | default: |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!$this->redis->hMSet($key, ['type' => $dataType, 'value' => $value])) { |
||
| 163 | return false; |
||
| 164 | } elseif ($ttl) { |
||
| 165 | $this->redis->expireAt($key, time() + $ttl); |
||
| 166 | } |
||
| 167 | |||
| 168 | return true; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Delete from cache |
||
| 173 | * |
||
| 174 | * @param string $key |
||
| 175 | * @return boolean |
||
| 176 | */ |
||
| 177 | public function delete($key) |
||
| 178 | { |
||
| 179 | return ($this->redis->delete($key) === 1); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Clean cache |
||
| 184 | * |
||
| 185 | * @return boolean |
||
| 186 | */ |
||
| 187 | public function clear() |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check if Redis driver is supported |
||
| 194 | * |
||
| 195 | * @return boolean |
||
| 196 | */ |
||
| 197 | public function isSupported() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Class destructor |
||
| 204 | * |
||
| 205 | * Closes the connection to Redis if present. |
||
| 206 | */ |
||
| 207 | public function __destruct() |
||
| 213 | } |
||
| 214 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..