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 |
||
11 | class CloudEnvironment extends Environment implements CloudEnvironmentInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $sitegroup; |
||
17 | |||
18 | /** |
||
19 | * @var string |
||
20 | */ |
||
21 | private $sitename; |
||
22 | |||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | private $currentregion; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $filepath; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | 78 | private $creds; |
|
37 | |||
38 | 78 | /** |
|
39 | 78 | * Acquia Cloud variables may be set in settings.inc after PHP init, |
|
40 | 63 | * so make sure that we are loading them. |
|
41 | * |
||
42 | * @param string $key |
||
43 | 63 | * @return string The value of the environment variable or false if not found |
|
44 | * @see https://github.com/acquia/acquia-sdk-php/pull/58#issuecomment-45167451 |
||
45 | */ |
||
46 | 63 | protected function getenv($key) |
|
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | 6 | */ |
|
63 | public function init() |
||
68 | |||
69 | /** |
||
70 | 6 | * {@inheritDoc} |
|
71 | */ |
||
72 | 6 | public function isAcquia() |
|
76 | |||
77 | /** |
||
78 | * @return bool |
||
79 | */ |
||
80 | 3 | public function isProduction() |
|
84 | |||
85 | /** |
||
86 | * @param string $sitegroup |
||
87 | * |
||
88 | * @return \Acquia\Cloud\Environment\CloudEnvironment |
||
89 | */ |
||
90 | public function setSiteGroup($sitegroup) |
||
95 | 12 | ||
96 | 3 | /** |
|
97 | * @return string |
||
98 | 9 | * |
|
99 | 12 | * @throws \UnexpectedValueException |
|
100 | */ |
||
101 | View Code Duplication | public function getSiteGroup() |
|
111 | |||
112 | /** |
||
113 | * @return string |
||
114 | * |
||
115 | * @throws \UnexpectedValueException |
||
116 | 21 | */ |
|
117 | View Code Duplication | public function getSiteName() |
|
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | 15 | * |
|
131 | * @throws \UnexpectedValueException |
||
132 | 15 | */ |
|
133 | 15 | View Code Duplication | public function getCurrentRegion() |
143 | |||
144 | /** |
||
145 | * @param string $filepath |
||
146 | * |
||
147 | * @return \Acquia\Cloud\Environment\CloudEnvironment |
||
148 | */ |
||
149 | public function setCredentialsFilepath($filepath) |
||
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | public function getCredentialsFilepath() |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | * |
||
170 | * @throws \RuntimeException |
||
171 | */ |
||
172 | public function serviceCredentials() |
||
180 | } |
||
181 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: