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 MemcachedStore implements StoreInterface, IncrementorInterface |
||
15 | { |
||
16 | /** |
||
17 | * Memcached instance |
||
18 | */ |
||
19 | protected $memcached; |
||
20 | |||
21 | /** |
||
22 | * Fix bug in memcached PHP module |
||
23 | * @see https://bugs.php.net/bug.php?id=51434 |
||
24 | * @var boolean |
||
25 | */ |
||
26 | public $bug51434fix = true; |
||
27 | |||
28 | /** |
||
29 | * Creates a Memcached store |
||
30 | * |
||
31 | * @param Memcached $memcached |
||
32 | */ |
||
33 | public function __construct(Memcached $memcached) |
||
37 | |||
38 | /** |
||
39 | * Creates MemcachedStore instance |
||
40 | * |
||
41 | * @param array $config Server Configurations |
||
42 | * |
||
43 | * @return MemcachedStore |
||
44 | */ |
||
45 | public static function factory(array $config = array()) |
||
70 | |||
71 | /** |
||
72 | * {@inheritdoc} |
||
73 | */ |
||
74 | public function add($key, $value, $ttl = 0) |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | public function set($key, $value, $ttl = 0) |
||
86 | |||
87 | /** |
||
88 | * {@inheritdoc} |
||
89 | */ |
||
90 | View Code Duplication | public function get($key) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | View Code Duplication | public function has($key) |
|
113 | |||
114 | /** |
||
115 | * {@inheritdoc} |
||
116 | */ |
||
117 | public function delete($key) |
||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | */ |
||
125 | public function flush() |
||
129 | |||
130 | /** |
||
131 | * {@inheritdoc} |
||
132 | */ |
||
133 | View Code Duplication | public function inc($key, $step = 1) |
|
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | View Code Duplication | public function dec($key, $step = 1) |
|
169 | |||
170 | /** |
||
171 | * Checks is the memcache server is running |
||
172 | * |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function checkRunning() |
||
193 | } |
||
194 |
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.