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 /** MicroDbDriver */ |
||
21 | class DbDriver extends BaseCacheDriver |
||
22 | { |
||
23 | /** @var IConnection $driver DB driver */ |
||
24 | protected $driver; |
||
25 | /** @var string $table table name */ |
||
26 | protected $table; |
||
27 | |||
28 | |||
29 | /** |
||
30 | * Constructor |
||
31 | * |
||
32 | * @access public |
||
33 | * |
||
34 | * @param IContainer $container |
||
35 | * @param array $config config array |
||
36 | * |
||
37 | * @result void |
||
38 | */ |
||
39 | public function __construct(IContainer $container, array $config = []) |
||
60 | |||
61 | /** |
||
62 | * @inheritdoc |
||
63 | */ |
||
64 | public function check() |
||
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | * @throws \Micro\Base\Exception |
||
72 | */ |
||
73 | public function get($name) |
||
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | * @throws \Micro\Base\Exception |
||
86 | */ |
||
87 | View Code Duplication | protected function getElement($name) |
|
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | public function set($name, $value) |
||
106 | |||
107 | /** |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | public function delete($name) |
||
114 | |||
115 | /** |
||
116 | * @inheritdoc |
||
117 | */ |
||
118 | public function clean() |
||
122 | |||
123 | /** |
||
124 | * @inheritdoc |
||
125 | */ |
||
126 | public function info() |
||
130 | |||
131 | /** |
||
132 | * @inheritdoc |
||
133 | * @throws \Micro\Base\Exception |
||
134 | */ |
||
135 | public function getMeta($id) |
||
139 | |||
140 | /** |
||
141 | * @inheritdoc |
||
142 | */ |
||
143 | public function increment($name, $offset = 1) |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function decrement($name, $offset = 1) |
||
155 | } |
||
156 |
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.