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 |
||
25 | class CachePool implements CacheItemPoolInterface |
||
26 | { |
||
27 | /** |
||
28 | * @type CacheItemPoolInterface |
||
29 | */ |
||
30 | protected $pool; |
||
31 | |||
32 | /** |
||
33 | * @type LoggerInterface |
||
34 | */ |
||
35 | private $logger; |
||
36 | |||
37 | /** |
||
38 | * @type string |
||
39 | */ |
||
40 | private $name; |
||
41 | |||
42 | /** |
||
43 | * @type string |
||
44 | */ |
||
45 | private $level = 'info'; |
||
46 | |||
47 | /** |
||
48 | * @type array calls |
||
49 | */ |
||
50 | private $calls = []; |
||
51 | |||
52 | /** |
||
53 | * @param CacheItemPoolInterface $pool |
||
54 | */ |
||
55 | public function __construct(CacheItemPoolInterface $pool) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function getItem($key) |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function hasItem($key) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | View Code Duplication | public function deleteItem($key) |
|
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | View Code Duplication | public function save(CacheItemInterface $item) |
|
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | View Code Duplication | public function saveDeferred(CacheItemInterface $item) |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | */ |
||
142 | public function getItems(array $keys = []) |
||
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | public function clear() |
||
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | View Code Duplication | public function deleteItems(array $keys) |
|
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function commit() |
||
204 | |||
205 | public function getCalls() |
||
209 | |||
210 | protected function start($name, $argument = null) |
||
219 | |||
220 | /** |
||
221 | * @param LoggerInterface $logger |
||
222 | */ |
||
223 | public function setLogger(LoggerInterface $logger = null) |
||
227 | |||
228 | /** |
||
229 | * @param string $name |
||
230 | */ |
||
231 | public function setName($name) |
||
237 | |||
238 | /** |
||
239 | * @param string $level |
||
240 | */ |
||
241 | public function setLevel($level) |
||
247 | } |
||
248 | |||
262 |
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.