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 AdapterMemcached implements iAdapter |
||
14 | { |
||
15 | /** |
||
16 | * @var bool |
||
17 | */ |
||
18 | public $installed = false; |
||
19 | |||
20 | /** |
||
21 | * @var Memcached |
||
22 | */ |
||
23 | private $memcached; |
||
24 | |||
25 | /** |
||
26 | * __construct |
||
27 | * |
||
28 | * @param Memcached|null $memcached |
||
29 | */ |
||
30 | 9 | public function __construct($memcached = null) |
|
36 | |||
37 | /** |
||
38 | * @param Memcached $memcached |
||
39 | */ |
||
40 | 9 | public function setMemcached(Memcached $memcached) |
|
47 | |||
48 | /** |
||
49 | * {@inheritdoc} |
||
50 | */ |
||
51 | 2 | public function exists(string $key): bool |
|
55 | |||
56 | /** |
||
57 | * {@inheritdoc} |
||
58 | */ |
||
59 | 6 | public function get(string $key) |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 9 | public function installed(): bool |
|
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function remove(string $key): bool |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function removeAll(): bool |
||
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | 3 | View Code Duplication | public function set(string $key, $value): bool |
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 1 | public function setExpired(string $key, $value, int $ttl = 0): bool |
|
112 | |||
113 | /** |
||
114 | * Set the MemCached settings. |
||
115 | * |
||
116 | * @noinspection PhpUndefinedClassConstantInspection -> MSGPACK is not added into phpstorm stubs |
||
117 | */ |
||
118 | 9 | private function setSettings() |
|
133 | } |
||
134 |
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.