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 PhpredisAdapter 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 PhpredisAdapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class PhpredisAdapter extends AbstractAdapter implements AdapterInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var Redis |
||
15 | */ |
||
16 | protected $client; |
||
17 | |||
18 | /** |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $ttl; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $config; |
||
27 | |||
28 | public function __construct(array $config = [], bool $lazy = true) |
||
40 | |||
41 | protected function getClient(): Redis |
||
49 | |||
50 | View Code Duplication | public function get(string $key) |
|
60 | |||
61 | View Code Duplication | public function getMulti(array $keys): array |
|
74 | |||
75 | public function set(string $key, $value): bool |
||
85 | |||
86 | public function setMulti(array $data): bool |
||
99 | |||
100 | public function contains(string $key): bool |
||
104 | |||
105 | public function delete(string $key): bool |
||
111 | |||
112 | public function deleteMulti(array $keys): bool |
||
118 | |||
119 | public function flush(): bool |
||
123 | |||
124 | public function setClient(Redis $client) |
||
128 | |||
129 | protected function createClient(array $config) |
||
193 | |||
194 | protected function getConnectionParameters(array $config): array |
||
195 | { |
||
196 | $connectionParameters = []; |
||
197 | $connectionParameters['host'] = $config['host'] ?? '127.0.0.1'; |
||
198 | $connectionParameters['port'] = $config['port'] ?? 6379; |
||
199 | $connectionParameters['password'] = $config['password'] ?? null; |
||
200 | $connectionParameters['database'] = $config['database'] ?? 0; |
||
201 | $connectionParameters['timeout'] = $config['timeout'] ?? null; |
||
202 | $connectionParameters['read_timeout'] = $config['read_timeout'] ?? null; |
||
203 | $connectionParameters['retry_interval'] = $config['retry_interval'] ?? null; |
||
204 | $connectionParameters['serializer'] = $config['serializer'] ?? null; |
||
205 | $connectionParameters['connection_persistent'] = $config['connection_persistent'] ?? false; |
||
206 | $connectionParameters['pool_size'] = $config['pool_size'] ?? 1; |
||
207 | |||
208 | return $connectionParameters; |
||
209 | } |
||
210 | |||
211 | public function setNamespace(string $namespace) |
||
216 | |||
217 | public function setTtl(int $ttl) |
||
221 | } |
||
222 |
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.