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 |
||
17 | class Memcached extends Memcache |
||
18 | { |
||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | * |
||
22 | * Additional options: |
||
23 | * "client" => the instance of \Memcached object |
||
24 | * |
||
25 | * @codeCoverageIgnore |
||
26 | * @param array $options |
||
27 | * @throws \Endeveit\Cache\Exception |
||
28 | */ |
||
29 | public function __construct(array $options = array()) |
||
37 | |||
38 | /** |
||
39 | * {@inheritdoc} |
||
40 | * |
||
41 | * @param string $id |
||
42 | * @return mixed|false |
||
43 | */ |
||
44 | protected function doLoad($id) |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | * |
||
52 | * @param array $identifiers |
||
53 | * @return array |
||
54 | */ |
||
55 | View Code Duplication | protected function doLoadMany(array $identifiers) |
|
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | * |
||
82 | * @param string $id |
||
83 | * @return mixed|false |
||
84 | */ |
||
85 | protected function doLoadRaw($id) |
||
89 | |||
90 | /** |
||
91 | * {@inheritdoc} |
||
92 | * |
||
93 | * @param mixed $data |
||
94 | * @param string $id |
||
95 | * @param array $tags |
||
96 | * @return boolean |
||
97 | */ |
||
98 | View Code Duplication | protected function doSave($data, $id, array $tags = array()) |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | * |
||
112 | * @param mixed $data |
||
113 | * @param string $id |
||
114 | * @param integer|boolean $lifetime |
||
115 | * @return boolean |
||
116 | */ |
||
117 | protected function doSaveScalar($data, $id, $lifetime = false) |
||
121 | } |
||
122 |
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.