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 |
||
32 | View Code Duplication | trait ServerVarsObjectTrait |
|
|
|||
33 | { |
||
34 | /** |
||
35 | * Sets a value to specific server var |
||
36 | * |
||
37 | * @param string $serverVar The server var to set |
||
38 | * @param string $value The value to server var |
||
39 | * |
||
40 | * @return void |
||
41 | */ |
||
42 | public function setServerVar($serverVar, $value) |
||
46 | |||
47 | /** |
||
48 | * Unsets a specific server var |
||
49 | * |
||
50 | * @param string $serverVar The server var to unset |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public function unsetServerVar($serverVar) |
||
58 | |||
59 | /** |
||
60 | * Returns a value for specific server var |
||
61 | * |
||
62 | * @param string $serverVar The server var to get value for |
||
63 | * |
||
64 | * @throws \AppserverIo\Server\Exceptions\ServerException |
||
65 | * |
||
66 | * @return string The value to given server var |
||
67 | */ |
||
68 | public function getServerVar($serverVar) |
||
73 | |||
74 | /** |
||
75 | * Returns all the server vars as array key value pair format |
||
76 | * |
||
77 | * @return array The server vars as array |
||
78 | */ |
||
79 | public function getServerVars() |
||
83 | |||
84 | /** |
||
85 | * Checks if value exists for given server var |
||
86 | * |
||
87 | * @param string $serverVar The server var to check |
||
88 | * |
||
89 | * @return bool Weather it has serverVar (true) or not (false) |
||
90 | */ |
||
91 | public function hasServerVar($serverVar) |
||
96 | |||
97 | /** |
||
98 | * Clears the server vars storage |
||
99 | * |
||
100 | * @return void |
||
101 | */ |
||
102 | public function clearServerVars() |
||
106 | } |
||
107 |
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.