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 |
||
12 | class Redis implements CacheInterface |
||
13 | { |
||
14 | const SETTINGS_REDIS = 'SETTINGS_REDIS'; |
||
15 | |||
16 | const SETTINGS_TTL_IN_SECOND = 'SETTINGS_TTL_IN_SECOND'; |
||
17 | const SETTINGS_CACHE_PREFIX = 'SETTINGS_CACHE_PREFIX'; |
||
18 | |||
19 | private $isAvailable = null; |
||
20 | private $redisInstance = null; |
||
21 | private $settings = array(); |
||
22 | |||
23 | /** |
||
24 | * Check whether redis is available |
||
25 | * @return bool |
||
26 | */ |
||
27 | 2 | public function isAvailable() |
|
28 | { |
||
29 | 2 | if (!$this->isAvailable) { |
|
30 | try { |
||
31 | 2 | $this->getRedisInstance()->ping(); |
|
32 | 1 | $this->isAvailable = true; |
|
33 | 1 | } catch (\Exception $e) { |
|
34 | 1 | $this->isAvailable = false; |
|
35 | } |
||
36 | } |
||
37 | 2 | return $this->isAvailable; |
|
38 | } |
||
39 | |||
40 | /** |
||
41 | * constructor of redis |
||
42 | * @param array $settings |
||
43 | */ |
||
44 | 15 | public function __construct($settings = array()) |
|
45 | { |
||
46 | 15 | if (isset($settings[self::SETTINGS_REDIS])) { |
|
47 | 1 | $this->setRedisInstance($settings[self::SETTINGS_REDIS]); |
|
48 | } |
||
49 | 15 | $this->settings = $settings; |
|
50 | 15 | } |
|
51 | |||
52 | /** |
||
53 | * Get the redis instance |
||
54 | * @return \Predis\Client |
||
55 | * @throws Exception |
||
56 | */ |
||
57 | 4 | public function getRedisInstance() |
|
58 | { |
||
59 | 4 | if (!$this->redisInstance) { |
|
60 | 1 | $this->redisInstance = $this->createRedis(); |
|
61 | } |
||
62 | 4 | return $this->redisInstance; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return \Predis\Client|null |
||
67 | * @throws Exception |
||
68 | * @codeCoverageIgnore |
||
69 | */ |
||
70 | private function createRedis() |
||
79 | |||
80 | /** |
||
81 | * Define a Predis instance |
||
82 | * @param \Predis\Client $redisInstance |
||
83 | * @return $this |
||
84 | */ |
||
85 | 3 | public function setRedisInstance(\Predis\Client $redisInstance) |
|
90 | |||
91 | /** |
||
92 | * @param string $key |
||
93 | * @return string |
||
94 | */ |
||
95 | 4 | protected function getCacheKey($key) |
|
100 | |||
101 | |||
102 | /** |
||
103 | * Check whether a key exists |
||
104 | * @param string $key |
||
105 | * @return bool |
||
106 | * @throws Exception |
||
107 | */ |
||
108 | 2 | public function has($key) |
|
115 | |||
116 | /** |
||
117 | * @param string $key |
||
118 | * @return mixed |
||
119 | * @throws Exception |
||
120 | */ |
||
121 | 2 | public function get($key) |
|
128 | |||
129 | /** |
||
130 | * Define a value for a given key in redis |
||
131 | * @param string $key |
||
132 | * @param mixed $value |
||
133 | * @return $this |
||
134 | * @throws Exception |
||
135 | */ |
||
136 | 3 | public function set($key, $value) |
|
137 | { |
||
138 | 3 | if (!$this->isAvailable()) { |
|
139 | 1 | throw new Exception('Redis is not available'); |
|
140 | } |
||
141 | 2 | $ttl = (int)$this->getSetting(self::SETTINGS_TTL_IN_SECOND); |
|
142 | 2 | $key = $this->getCacheKey($key); |
|
143 | 2 | $redis = $this->getRedisInstance(); |
|
144 | 2 | $return = $redis->set($key, serialize($value)); |
|
145 | 2 | if ($ttl) { |
|
146 | 2 | $redis->expireAt($key, time() + $ttl); |
|
147 | 1 | $redis->ttl($key); |
|
148 | } |
||
149 | 1 | if (!$return) { |
|
150 | throw new Exception('Error while inserting value in redis'); |
||
151 | } |
||
152 | 1 | return $this; |
|
153 | } |
||
154 | |||
155 | /** |
||
156 | * Delete a value in redis |
||
157 | * @param string $key |
||
158 | * @return $this |
||
159 | * @throws Exception |
||
160 | */ |
||
161 | 3 | View Code Duplication | public function remove($key) |
172 | |||
173 | /** |
||
174 | * Define a setting |
||
175 | * @param string $setting |
||
176 | * @param mixed $value |
||
177 | * @return $this |
||
178 | */ |
||
179 | 1 | public function setSetting($setting, $value) |
|
184 | |||
185 | /** |
||
186 | * Retrieve a defined setting |
||
187 | * @param string $setting |
||
188 | * @return mixed|null |
||
189 | */ |
||
190 | 5 | public function getSetting($setting) |
|
195 | } |
||
196 |
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.