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 |
||
9 | class MemcachedEngine implements CacheEngineInterface |
||
10 | { |
||
11 | |||
12 | /** |
||
13 | * |
||
14 | * @var Memcached |
||
15 | */ |
||
16 | protected $memCached = null; |
||
17 | |||
18 | protected $logger = null; |
||
19 | |||
20 | protected $servers = null; |
||
21 | |||
22 | View Code Duplication | public function __construct($servers = null, $logger = null) |
|
36 | |||
37 | protected function lazyLoadMemCachedServers() |
||
52 | |||
53 | /** |
||
54 | * @param string $key The object KEY |
||
55 | * @param int $ttl IGNORED IN MEMCACHED. |
||
56 | * @return object Description |
||
57 | */ |
||
58 | public function get($key, $ttl = 0) |
||
70 | |||
71 | /** |
||
72 | * @param string $key The object Key |
||
73 | * @param object $object The object to be cached |
||
74 | * @param int $ttl The time to live in seconds of this objects |
||
75 | * @return bool If the object is successfully posted |
||
76 | */ |
||
77 | public function set($key, $object, $ttl = 0) |
||
89 | |||
90 | /** |
||
91 | * Unlock resource |
||
92 | * @param string $key |
||
93 | */ |
||
94 | public function release($key) |
||
100 | |||
101 | /** |
||
102 | * |
||
103 | * @param string $key |
||
104 | * @param string $str |
||
105 | * @return bool |
||
106 | */ |
||
107 | public function append($key, $str) |
||
114 | |||
115 | /** |
||
116 | * Lock resource before set it. |
||
117 | * @param string $key |
||
118 | */ |
||
119 | public function lock($key) |
||
125 | |||
126 | /** |
||
127 | * UnLock resource after set it |
||
128 | * @param string $key |
||
129 | */ |
||
130 | public function unlock($key) |
||
136 | |||
137 | public function isAvailable() |
||
150 | } |
||
151 |
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.