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