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 |
||
34 | View Code Duplication | trait ModuleVarsArrayTrait |
|
|
|||
35 | { |
||
36 | |||
37 | /** |
||
38 | * Sets a value to specific module var |
||
39 | * |
||
40 | * @param string $moduleVar The module var to set |
||
41 | * @param string $value The value to module var |
||
42 | * |
||
43 | * @return void |
||
44 | */ |
||
45 | public function setModuleVar($moduleVar, $value) |
||
51 | |||
52 | /** |
||
53 | * Unsets a specific module var |
||
54 | * |
||
55 | * @param string $moduleVar The module var to unset |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | public function unsetModuleVar($moduleVar) |
||
65 | |||
66 | /** |
||
67 | * Returns a value for specific module var |
||
68 | * |
||
69 | * @param string $moduleVar The module var to get value for |
||
70 | * |
||
71 | * @throws \AppserverIo\Server\Exceptions\ServerException |
||
72 | * |
||
73 | * @return mixed The value to given module var |
||
74 | */ |
||
75 | public function getModuleVar($moduleVar) |
||
85 | |||
86 | |||
87 | /** |
||
88 | * Returns all the module vars as array key value pair format |
||
89 | * |
||
90 | * @return array The module vars as array |
||
91 | */ |
||
92 | public function getModuleVars() |
||
96 | |||
97 | /** |
||
98 | * Checks if value exists for given module var |
||
99 | * |
||
100 | * @param string $moduleVar The module var to check |
||
101 | * |
||
102 | * @return boolean Weather it has moduleVar (true) or not (false) |
||
103 | */ |
||
104 | public function hasModuleVar($moduleVar) |
||
113 | |||
114 | /** |
||
115 | * Clears the module vars storage |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | public function clearModuleVars() |
||
125 | } |
||
126 |
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.