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 |
||
8 | class Instance implements InstanceInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var array |
||
12 | */ |
||
13 | protected $required = []; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $config = []; |
||
19 | |||
20 | /** |
||
21 | * @return array |
||
22 | */ |
||
23 | public static function getRequired() : array |
||
28 | |||
29 | /** |
||
30 | * @param $key |
||
31 | * @param $value |
||
32 | */ |
||
33 | public function set($key, $value) |
||
37 | |||
38 | /** |
||
39 | * @return bool |
||
40 | * @throws InstanceCorruptException |
||
41 | */ |
||
42 | public function check() : bool |
||
64 | |||
65 | /** |
||
66 | * @param $key |
||
67 | * @return mixed |
||
68 | */ |
||
69 | protected function get($key) |
||
73 | } |
||
74 |
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.