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 |
||
13 | class ElasticSearchAdapter extends Adapter implements CacheAdapter |
||
14 | { |
||
15 | /** |
||
16 | * @var \NilPortugues\Cache\Adapter\ElasticSearch\Curl |
||
17 | */ |
||
18 | private $curl; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $base = ''; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $baseUrl = ''; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | private $createCache = [ |
||
34 | "mappings" => [ |
||
35 | "cache" => [ |
||
36 | "_source" => [ |
||
37 | "enabled" => false |
||
38 | ], |
||
39 | "_ttl" => [ |
||
40 | "enabled" => true, |
||
41 | "default" => "1000ms" |
||
42 | ], |
||
43 | "properties" => [ |
||
44 | "value" => [ |
||
45 | "type" => "string", |
||
46 | "index" => "not_analyzed" |
||
47 | ] |
||
48 | ] |
||
49 | ] |
||
50 | ] |
||
51 | ]; |
||
52 | |||
53 | /** |
||
54 | * @param string $baseUrl |
||
55 | * @param string $indexName |
||
56 | * @param CacheAdapter $next |
||
57 | * |
||
58 | * @throws InvalidArgumentException |
||
59 | */ |
||
60 | public function __construct($baseUrl, $indexName, CacheAdapter $next = null) |
||
79 | |||
80 | /** |
||
81 | * @codeCoverageIgnore |
||
82 | * @return Curl |
||
83 | */ |
||
84 | protected function getCurlClient() |
||
88 | |||
89 | /** |
||
90 | * Get a value identified by $key. |
||
91 | * |
||
92 | * @param string $key |
||
93 | * |
||
94 | * @return bool|mixed |
||
95 | */ |
||
96 | View Code Duplication | public function get($key) |
|
117 | |||
118 | /** |
||
119 | * Delete a value identified by $key. |
||
120 | * |
||
121 | * @param string $key |
||
122 | */ |
||
123 | public function delete($key) |
||
128 | |||
129 | /** |
||
130 | * Set a value identified by $key and with an optional $ttl. |
||
131 | * |
||
132 | * @param string $key |
||
133 | * @param mixed $value |
||
134 | * @param int $ttl |
||
135 | * |
||
136 | * @return $this |
||
137 | */ |
||
138 | public function set($key, $value, $ttl = 0) |
||
156 | |||
157 | /** |
||
158 | * Checks the availability of the cache service. |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function isAvailable() |
||
166 | |||
167 | /** |
||
168 | * Clears all expired values from cache. |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | public function clear() |
||
176 | |||
177 | /** |
||
178 | * Clears all values from the cache. |
||
179 | * |
||
180 | * @return mixed |
||
181 | */ |
||
182 | public function drop() |
||
188 | } |
||
189 |
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.