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 |
||
16 | abstract class InMemory extends Memcache |
||
17 | { |
||
18 | /** |
||
19 | * List of callback functions. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $callbacks = array( |
||
24 | 'increment' => null, |
||
25 | 'decrement' => null, |
||
26 | 'exists' => null, |
||
27 | 'load' => null, |
||
28 | 'save' => null, |
||
29 | ); |
||
30 | |||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | * |
||
34 | * @codeCoverageIgnore |
||
35 | * @param array $options |
||
36 | */ |
||
37 | public function __construct(array $options = array()) |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | * |
||
45 | * @param string $id |
||
46 | * @param integer $value |
||
47 | * @return integer |
||
48 | */ |
||
49 | View Code Duplication | public function increment($id, $value = 1) |
|
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | * |
||
66 | * @param string $id |
||
67 | * @param integer $value |
||
68 | * @return integer |
||
69 | */ |
||
70 | View Code Duplication | public function decrement($id, $value = 1) |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | * |
||
87 | * @param string $id |
||
88 | * @return boolean|mixed |
||
89 | */ |
||
90 | protected function doLoad($id) |
||
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | * |
||
98 | * @param array $identifiers |
||
99 | * @return array |
||
100 | */ |
||
101 | protected function doLoadMany(array $identifiers) |
||
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | * |
||
127 | * @param string $id |
||
128 | * @return mixed|false |
||
129 | */ |
||
130 | protected function doLoadRaw($id) |
||
134 | |||
135 | /** |
||
136 | * {@inheritdoc} |
||
137 | * |
||
138 | * @param mixed $data |
||
139 | * @param string $id |
||
140 | * @param array $tags |
||
141 | * @return boolean |
||
142 | */ |
||
143 | protected function doSave($data, $id, array $tags = array()) |
||
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | * |
||
157 | * @param mixed $data |
||
158 | * @param string $id |
||
159 | * @param integer|boolean $lifetime |
||
160 | * @return boolean |
||
161 | */ |
||
162 | protected function doSaveScalar($data, $id, $lifetime = false) |
||
166 | } |
||
167 |
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.