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 |
||
14 | class AdapterMemcache implements iAdapter |
||
15 | { |
||
16 | /** |
||
17 | * @var bool |
||
18 | */ |
||
19 | public $installed = false; |
||
20 | |||
21 | /** |
||
22 | * @var \Memcache |
||
23 | */ |
||
24 | private $memcache; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $compressed = false; |
||
30 | |||
31 | /** |
||
32 | * __construct |
||
33 | * |
||
34 | * @param \Memcache|null $memcache |
||
35 | */ |
||
36 | public function __construct($memcache = null) |
||
42 | |||
43 | /** |
||
44 | * @param \Memcache $memcache |
||
45 | */ |
||
46 | public function setMemcache(\Memcache $memcache) { |
||
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | public function exists(string $key): bool |
||
58 | |||
59 | /** |
||
60 | * @inheritdoc |
||
61 | */ |
||
62 | public function get(string $key) |
||
66 | |||
67 | /** |
||
68 | * @inheritdoc |
||
69 | */ |
||
70 | public function installed(): bool |
||
74 | |||
75 | /** |
||
76 | * @inheritdoc |
||
77 | */ |
||
78 | public function remove(string $key): bool |
||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | public function removeAll(): bool |
||
90 | |||
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | View Code Duplication | public function set(string $key, $value): bool |
|
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | public function setExpired(string $key, $value, int $ttl = 0): bool |
||
115 | |||
116 | /** |
||
117 | * Get the compressed-flag from MemCache. |
||
118 | * |
||
119 | * @return int 2 || 0 |
||
120 | */ |
||
121 | private function getCompressedFlag(): int |
||
125 | |||
126 | /** |
||
127 | * Check if compression from MemCache is active. |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | public function isCompressed(): bool |
||
135 | |||
136 | /** |
||
137 | * Activate the compression from MemCache. |
||
138 | * |
||
139 | * @param mixed $value will be converted to bool |
||
140 | */ |
||
141 | public function setCompressed($value) |
||
145 | |||
146 | } |
||
147 |
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.