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 |
||
21 | abstract class AbstractAdapter extends Adapter implements CacheAdapter |
||
22 | { |
||
23 | /** |
||
24 | * Redis client instance |
||
25 | * |
||
26 | * @var \Redis|\Predis\Client |
||
27 | */ |
||
28 | protected $redis; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $connected; |
||
34 | |||
35 | /** |
||
36 | * @var InMemoryAdapter |
||
37 | */ |
||
38 | protected $inMemoryAdapter; |
||
39 | |||
40 | /** |
||
41 | * @param array $connections |
||
42 | * @param CacheAdapter $next |
||
43 | */ |
||
44 | abstract public function __construct(array $connections, CacheAdapter $next = null); |
||
45 | |||
46 | /** |
||
47 | * Get a value identified by $key. |
||
48 | * |
||
49 | * @param string $key |
||
50 | * |
||
51 | * @return bool|mixed |
||
52 | */ |
||
53 | View Code Duplication | public function get($key) |
|
77 | |||
78 | |||
79 | /** |
||
80 | * Set a value identified by $key and with an optional $ttl. |
||
81 | * |
||
82 | * @param string $key |
||
83 | * @param mixed $value |
||
84 | * @param int $ttl |
||
85 | * |
||
86 | * @return $this |
||
87 | */ |
||
88 | public function set($key, $value, $ttl = 0) |
||
106 | |||
107 | |||
108 | /** |
||
109 | * Delete a value identified by $key. |
||
110 | * |
||
111 | * @param string $key |
||
112 | */ |
||
113 | public function delete($key) |
||
118 | |||
119 | /** |
||
120 | * Clears all expired values from cache. |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function clear() |
||
128 | |||
129 | /** |
||
130 | * Clears all values from the cache. |
||
131 | * |
||
132 | * @return mixed |
||
133 | */ |
||
134 | public function drop() |
||
139 | } |
||
140 |
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.