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 | * @param string $sitename |
||
| 114 | * |
||
| 115 | * @return \Acquia\Cloud\Environment\CloudEnvironment |
||
| 116 | 21 | */ |
|
| 117 | public function setSiteName($sitename) |
||
| 122 | 21 | ||
| 123 | /** |
||
| 124 | * @return string |
||
| 125 | * |
||
| 126 | * @throws \UnexpectedValueException |
||
| 127 | */ |
||
| 128 | View Code Duplication | public function getSiteName() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param string $currentregion |
||
| 141 | * |
||
| 142 | * @return \Acquia\Cloud\Environment\CloudEnvironment |
||
| 143 | */ |
||
| 144 | public function setCurrentRegion($currentregion) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | * |
||
| 153 | * @throws \UnexpectedValueException |
||
| 154 | */ |
||
| 155 | View Code Duplication | public function getCurrentRegion() |
|
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $filepath |
||
| 168 | * |
||
| 169 | * @return \Acquia\Cloud\Environment\CloudEnvironment |
||
| 170 | */ |
||
| 171 | public function setCredentialsFilepath($filepath) |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getCredentialsFilepath() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @return array |
||
| 191 | * |
||
| 192 | * @throws \RuntimeException |
||
| 193 | */ |
||
| 194 | public function serviceCredentials() |
||
| 202 | } |
||
| 203 |
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: