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 |
||
| 12 | class Environment |
||
| 13 | { |
||
| 14 | const DEPLOY_PATH = 'deploy_path'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Globally defaults values. |
||
| 18 | * |
||
| 19 | * @var \Deployer\Type\DotArray |
||
| 20 | */ |
||
| 21 | private static $defaults = null; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Array of env values. |
||
| 25 | * @var \Deployer\Type\DotArray |
||
| 26 | */ |
||
| 27 | private $values = null; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Values represented by their keys here are protected, and cannot be |
||
| 31 | * changed by calling the `set` method. |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | private $protectedNames = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor |
||
| 38 | */ |
||
| 39 | 49 | public function __construct() |
|
| 40 | { |
||
| 41 | 49 | $this->values = new DotArray(); |
|
| 42 | 49 | } |
|
| 43 | |||
| 44 | /** |
||
| 45 | * @param string $name |
||
| 46 | * @param bool|int|string|array $value |
||
| 47 | */ |
||
| 48 | 33 | public function set($name, $value) |
|
| 49 | { |
||
| 50 | 33 | $this->checkIfNameIsProtected($name); |
|
| 51 | 33 | $this->values[$name] = $value; |
|
| 52 | 33 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * @param string $name |
||
| 56 | * @param bool|int|string|array $value |
||
| 57 | */ |
||
| 58 | 21 | public function setAsProtected($name, $value) |
|
| 59 | { |
||
| 60 | 21 | $this->set($name, $value); |
|
| 61 | 21 | $this->protectedNames[] = $name; |
|
| 62 | 21 | } |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Checks whether the given name was registered as protected, or if there is |
||
| 66 | * a protected parameter which would be overwritten. |
||
| 67 | * @param string $name |
||
| 68 | * @throws \RuntimeException if the value already exists and is protected. |
||
| 69 | * @throws \RuntimeException if there's a protected parameter which would |
||
| 70 | * be overwritten. |
||
| 71 | */ |
||
| 72 | 33 | private function checkIfNameIsProtected($name) |
|
| 73 | { |
||
| 74 | 33 | $length = strlen($name); |
|
| 75 | |||
| 76 | 33 | foreach ($this->protectedNames as $protectedName) { |
|
| 77 | 20 | $len = strlen($protectedName); |
|
| 78 | 20 | if ($name === $protectedName) { |
|
| 79 | 2 | throw new \RuntimeException("The parameter `$name` cannot be set, because it's protected."); |
|
| 80 | 18 | } elseif ($len < $length && '.' === $name[$len] && 0 === strpos($name, $protectedName)) { |
|
| 81 | 2 | throw new \RuntimeException("The parameter `$name` cannot be set, because `$protectedName` is protected."); |
|
| 82 | 16 | } elseif ($len > $length && '.' === $protectedName[$length] && 0 === strpos($protectedName, $name)) { |
|
| 83 | 1 | throw new \RuntimeException("The parameter `$name` could not be set, because a protected parameter named `$protectedName` already exists."); |
|
| 84 | } |
||
| 85 | 33 | } |
|
| 86 | 33 | } |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param string $name |
||
| 90 | * @param bool|int|string|array $default |
||
| 91 | * @return bool|int|string|array |
||
| 92 | * @throws \RuntimeException |
||
| 93 | */ |
||
| 94 | 22 | public function get($name, $default = null) |
|
| 95 | { |
||
| 96 | 22 | if ($this->values->hasKey($name)) { |
|
| 97 | 21 | $value = $this->values[$name]; |
|
| 98 | 21 | } else { |
|
| 99 | 16 | if (null !== self::$defaults && isset(self::$defaults[$name])) { |
|
| 100 | 10 | if (is_callable(self::$defaults[$name])) { |
|
| 101 | 9 | $value = $this->values[$name] = call_user_func(self::$defaults[$name]); |
|
| 102 | 9 | } else { |
|
| 103 | 3 | $value = $this->values[$name] = self::$defaults[$name]; |
|
| 104 | } |
||
| 105 | 10 | } else { |
|
| 106 | 16 | if (null === $default) { |
|
| 107 | 1 | throw new \RuntimeException("Environment parameter `$name` does not exists."); |
|
| 108 | } else { |
||
| 109 | 16 | $value = $default; |
|
| 110 | } |
||
| 111 | } |
||
| 112 | } |
||
| 113 | |||
| 114 | 22 | return $this->parse($value); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Checks if env var exists. |
||
| 119 | * |
||
| 120 | * @param string $name |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | 15 | public function has($name) |
|
| 124 | { |
||
| 125 | 15 | return $this->values->hasKey($name); |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param string $name |
||
| 130 | * @return mixed |
||
| 131 | */ |
||
| 132 | 2 | View Code Duplication | public static function getDefault($name) |
| 133 | { |
||
| 134 | 2 | if (null === self::$defaults) { |
|
| 135 | self::$defaults = new DotArray(); |
||
| 136 | } |
||
| 137 | 2 | return self::$defaults[$name]; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $name |
||
| 142 | * @param mixed $value |
||
| 143 | */ |
||
| 144 | 14 | View Code Duplication | public static function setDefault($name, $value) |
| 145 | { |
||
| 146 | 14 | if (null === self::$defaults) { |
|
| 147 | 1 | self::$defaults = new DotArray(); |
|
| 148 | 1 | } |
|
| 149 | 14 | self::$defaults[$name] = $value; |
|
| 150 | 14 | } |
|
| 151 | |||
| 152 | /** |
||
| 153 | * Parse env values. |
||
| 154 | * |
||
| 155 | * @param string $value |
||
| 156 | * @return string |
||
| 157 | */ |
||
| 158 | 23 | public function parse($value) |
|
| 159 | { |
||
| 160 | 23 | if (is_string($value)) { |
|
| 161 | 18 | $value = preg_replace_callback('/\{\{\s*([\w\.]+)\s*\}\}/', [$this, 'parseCallback'], $value); |
|
| 162 | 18 | } |
|
| 163 | |||
| 164 | 23 | return $value; |
|
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Replace env values callback for parse |
||
| 169 | * |
||
| 170 | * @param array $matches |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | 15 | private function parseCallback($matches) |
|
| 177 | } |
||
| 178 |