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 | class MetaDataHelper |
||
17 | { |
||
18 | /** |
||
19 | * Session cache |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $cache = []; |
||
23 | |||
24 | /** |
||
25 | * Get current cache data for store to external cache |
||
26 | * @return array |
||
27 | */ |
||
28 | public function getCache() : array |
||
32 | |||
33 | /** |
||
34 | * Set cache data from external cache |
||
35 | * @param array $cache |
||
36 | * @return void |
||
37 | */ |
||
38 | public function setCache(array $cache) : void |
||
42 | |||
43 | /** |
||
44 | * Metadata for class |
||
45 | * |
||
46 | * @param string $className |
||
47 | * @return array |
||
48 | */ |
||
49 | public function classMeta(string $className) : array |
||
58 | |||
59 | /** |
||
60 | * Metadata for methods |
||
61 | * meta at name for future add return type info |
||
62 | * |
||
63 | * @param string $className |
||
64 | * @return array[] |
||
65 | */ |
||
66 | View Code Duplication | public function methodsMeta(string $className) : array |
|
80 | |||
81 | /** |
||
82 | * Metadata for fields |
||
83 | * meta at name for future add return type info |
||
84 | * |
||
85 | * @param string $className |
||
86 | * @return array |
||
87 | */ |
||
88 | View Code Duplication | public function fieldsMeta(string $className) : array |
|
102 | |||
103 | /** |
||
104 | * @param array $data |
||
105 | * @return array |
||
106 | */ |
||
107 | public function prepareRules(array $data) : array |
||
119 | } |
||
120 |
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.