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:
Complex classes like RedisProxy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use RedisProxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class RedisProxy |
||
25 | { |
||
26 | const DRIVER_REDIS = 'redis'; |
||
27 | |||
28 | const DRIVER_PREDIS = 'predis'; |
||
29 | |||
30 | private $driver; |
||
31 | |||
32 | private $host; |
||
33 | |||
34 | private $port; |
||
35 | |||
36 | private $database = 0; |
||
37 | |||
38 | private $timeout; |
||
39 | |||
40 | private $supportedDrivers = [ |
||
41 | self::DRIVER_REDIS, |
||
42 | self::DRIVER_PREDIS, |
||
43 | ]; |
||
44 | |||
45 | private $driversOrder = []; |
||
46 | |||
47 | 48 | public function __construct($host, $port, $timeout = null) |
|
54 | |||
55 | 48 | public function setDriversOrder(array $driversOrder) |
|
56 | { |
||
57 | 48 | if (empty($driversOrder)) { |
|
58 | 2 | throw new RedisProxyException('You need to set at least one driver'); |
|
59 | } |
||
60 | 46 | foreach ($driversOrder as $driver) { |
|
61 | 46 | if (!in_array($driver, $this->supportedDrivers)) { |
|
62 | 46 | throw new RedisProxyException('Driver "' . $driver . '" is not supported'); |
|
63 | } |
||
64 | } |
||
65 | 44 | $this->driversOrder = $driversOrder; |
|
66 | 44 | return $this; |
|
67 | } |
||
68 | |||
69 | 44 | private function init() |
|
74 | |||
75 | 44 | private function prepareDriver() |
|
76 | { |
||
77 | 44 | if ($this->driver !== null) { |
|
78 | 44 | return; |
|
79 | } |
||
80 | |||
81 | 44 | foreach ($this->driversOrder as $preferredDriver) { |
|
82 | 44 | if ($preferredDriver === self::DRIVER_REDIS && extension_loaded('redis')) { |
|
83 | 22 | $this->driver = new Redis(); |
|
84 | 22 | return; |
|
85 | } |
||
86 | 22 | if ($preferredDriver === self::DRIVER_PREDIS && class_exists('Predis\Client')) { |
|
87 | 22 | $this->driver = new Client(); |
|
88 | 22 | return; |
|
89 | } |
||
90 | } |
||
91 | throw new RedisProxyException('No redis library loaded (ext-redis or predis)'); |
||
92 | } |
||
93 | |||
94 | 44 | private function connect($host, $port, $timeout = null) |
|
98 | |||
99 | 44 | private function isConnected() |
|
103 | |||
104 | 44 | public function __call($name, $arguments) |
|
105 | { |
||
106 | 44 | $this->init(); |
|
107 | 44 | $result = call_user_func_array([$this->driver, $name], $arguments); |
|
108 | 44 | if ($this->driver instanceof Client && $result instanceof Status) { |
|
109 | 22 | $result = $result->getPayload() === 'OK'; |
|
110 | } |
||
111 | 44 | return $result; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * @param integer $database |
||
116 | * @return boolean true on success |
||
117 | * @throws RedisProxyException on failure |
||
118 | */ |
||
119 | 44 | public function select($database) |
|
120 | { |
||
121 | 44 | $this->prepareDriver(); |
|
122 | 44 | if (!$this->isConnected()) { |
|
123 | 44 | $this->connect($this->host, $this->port, $this->timeout); |
|
124 | } |
||
125 | try { |
||
126 | 44 | $result = $this->driver->select($database); |
|
127 | 2 | } catch (Exception $e) { |
|
128 | 2 | throw new RedisProxyException('Invalid DB index'); |
|
129 | } |
||
130 | 44 | if ($this->driver instanceof Client) { |
|
131 | 22 | $result = $result->getPayload() === 'OK'; |
|
132 | } |
||
133 | 44 | if ($result === false) { |
|
134 | 2 | throw new RedisProxyException('Invalid DB index'); |
|
135 | } |
||
136 | 44 | $this->database = $database; |
|
137 | 44 | return $result; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @param string|null $section |
||
142 | * @return array |
||
143 | */ |
||
144 | 8 | public function info($section = null) |
|
145 | { |
||
146 | 8 | $this->init(); |
|
147 | 8 | if ($section === null) { |
|
148 | 4 | $result = $this->driver->info(); |
|
149 | } else { |
||
150 | 8 | $section = strtolower($section); |
|
151 | 8 | $result = $this->driver->info($section); |
|
152 | } |
||
153 | |||
154 | 8 | $databases = $section === null || $section === 'keyspace' ? $this->config('get', 'databases')['databases'] : null; |
|
155 | 8 | $groupedResult = InfoHelper::createInfoArray($this->driver, $result, $databases); |
|
156 | 8 | if ($section === null) { |
|
157 | 4 | return $groupedResult; |
|
158 | } |
||
159 | 8 | if (isset($groupedResult[$section])) { |
|
160 | 4 | return $groupedResult[$section]; |
|
161 | } |
||
162 | 4 | throw new RedisProxyException('Info section "' . $section . '" doesn\'t exist'); |
|
163 | } |
||
164 | |||
165 | /** |
||
166 | * @param string $key |
||
167 | * @return string |
||
168 | */ |
||
169 | 12 | public function get($key) |
|
170 | { |
||
171 | 12 | $this->init(); |
|
172 | 12 | $result = $this->driver->get($key); |
|
173 | 12 | if ($this->driver instanceof Client) { |
|
174 | 6 | $result = $result === null ? false : $result; |
|
175 | } |
||
176 | 12 | return $result; |
|
177 | } |
||
178 | |||
179 | 12 | public function del(...$key) |
|
184 | |||
185 | 8 | public function delete(...$key) |
|
189 | |||
190 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
|
200 | |||
201 | /** |
||
202 | * Get the value of a hash field |
||
203 | * @param string $key |
||
204 | * @param string $field |
||
205 | * @return string|boolean false if hash field is not set |
||
206 | */ |
||
207 | 4 | public function hget($key, $field) |
|
208 | { |
||
209 | 4 | $this->init(); |
|
210 | 4 | $result = $this->driver->hget($key, $field); |
|
211 | 4 | if ($this->driver instanceof Client) { |
|
212 | 2 | $result = $result === null ? false : $result; |
|
213 | } |
||
214 | 4 | return $result; |
|
215 | } |
||
216 | |||
217 | /** |
||
218 | * Delete one or more hash fields, returns number of deleted fields |
||
219 | * @param string $key |
||
220 | * @param array $fields |
||
221 | * @return integer |
||
222 | */ |
||
223 | 8 | public function hdel($key, ...$fields) |
|
224 | { |
||
225 | 8 | if (is_array($fields[0])) { |
|
226 | 4 | $fields = $fields[0]; |
|
227 | } |
||
228 | 8 | $res = $this->driver->hdel($key, ...$fields); |
|
229 | |||
230 | 8 | return $res; |
|
231 | } |
||
232 | |||
233 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
|
243 | |||
244 | View Code Duplication | public function zscan($key, &$iterator, $pattern = null, $count = null) |
|
254 | |||
255 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
|
265 | } |
||
266 |
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.