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 |
||
| 3 | class DeploymentStrategy extends ViewableData { |
||
| 4 | |||
| 5 | const SUCCESS_CODE = 'success'; |
||
| 6 | |||
| 7 | const WARNING_CODE = 'warning'; |
||
| 8 | |||
| 9 | const ERROR_CODE = 'error'; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @var DNEnvironment |
||
| 13 | */ |
||
| 14 | protected $environment; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var string |
||
| 18 | */ |
||
| 19 | protected $actionTitle = 'Deploy'; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $actionCode = 'default'; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $estimatedTime = 0; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $changes = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | protected $options; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Validation code |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | protected $validationCode = DeploymentStrategy::SUCCESS_CODE; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $messages = []; |
||
| 52 | |||
| 53 | |||
| 54 | /** |
||
| 55 | * @param DNEnvironment $environment |
||
| 56 | * @param array $options |
||
| 57 | */ |
||
| 58 | public function __construct(DNEnvironment $environment, $options = array()) { |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param string $title |
||
| 65 | */ |
||
| 66 | public function setActionTitle($title) { |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | public function getActionTitle() { |
||
| 76 | |||
| 77 | /** |
||
| 78 | */ |
||
| 79 | public function setActionCode($code) { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return string |
||
| 85 | */ |
||
| 86 | public function getActionCode() { |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param int |
||
| 92 | */ |
||
| 93 | public function setEstimatedTime($seconds) { |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return int Time in minutes |
||
| 99 | */ |
||
| 100 | public function getEstimatedTime() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $title |
||
| 106 | * @param string $from |
||
| 107 | * @param string $to |
||
| 108 | */ |
||
| 109 | public function setChange($title, $from, $to) { |
||
| 110 | return $this->changes[$title] = array( |
||
| 111 | 'from' => $from, |
||
| 112 | 'to' => $to |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $title |
||
| 118 | * @param string $desc |
||
| 119 | */ |
||
| 120 | public function setChangeDescriptionOnly($title, $desc) { |
||
| 121 | return $this->changes[$title] = array( |
||
| 122 | 'description' => $desc |
||
| 123 | ); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Filter the changeset where modification was not required. |
||
| 128 | * |
||
| 129 | * @return array |
||
| 130 | */ |
||
| 131 | public function getChangesModificationNeeded() { |
||
| 132 | $filtered = []; |
||
| 133 | foreach ($this->changes as $change => $details) { |
||
| 134 | if (array_key_exists('description', $details)) { |
||
| 135 | $filtered[$change] = $details; |
||
| 136 | } else if ( |
||
| 137 | (array_key_exists('from', $details) || array_key_exists('to', $details)) |
||
| 138 | && $details['from'] !== $details['to'] |
||
| 139 | ) { |
||
| 140 | $filtered[$change] = $details; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $filtered; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return array Associative array of changes, e.g. |
||
| 149 | * array( |
||
| 150 | * 'SHA' => array( |
||
| 151 | * 'from' => 'abc', |
||
| 152 | * 'to' => 'def' |
||
| 153 | * ) |
||
| 154 | * ) |
||
| 155 | */ |
||
| 156 | public function getChanges() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Returns a change or a given key. |
||
| 162 | * |
||
| 163 | * @return ArrayData|null |
||
| 164 | */ |
||
| 165 | public function getChange($key) { |
||
| 166 | $changes = $this->getChanges(); |
||
| 167 | if(array_key_exists($key, $changes)) { |
||
| 168 | return new ArrayData($changes[$key]); |
||
| 169 | } |
||
| 170 | return null; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $option |
||
| 175 | * @param string $value |
||
| 176 | */ |
||
| 177 | public function setOption($option, $value) { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $option |
||
| 183 | * @return string|null |
||
| 184 | */ |
||
| 185 | public function getOption($option) { |
||
| 186 | if(!empty($this->options[$option])) { |
||
| 187 | return $this->options[$option]; |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getOptions() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $code |
||
| 200 | */ |
||
| 201 | public function setValidationCode($code) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getValidationCode() { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @param string $msg |
||
| 214 | */ |
||
| 215 | public function setMessage($msg, $code = self::ERROR_CODE) { |
||
| 216 | $this->messages[] = [ |
||
| 217 | 'text' => $msg, |
||
| 218 | 'code' => $code |
||
| 219 | ]; |
||
| 220 | |||
| 221 | $current = $this->getValidationCode(); |
||
| 222 | $map = [ |
||
| 223 | DeploymentStrategy::SUCCESS_CODE => 0, |
||
| 224 | DeploymentStrategy::WARNING_CODE => 1, |
||
| 225 | DeploymentStrategy::ERROR_CODE => 2 |
||
| 226 | ]; |
||
| 227 | if($map[$current] < $map[$code]) { |
||
| 228 | $this->setValidationCode($code); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | public function getMessages() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Transform the deployment strategy to an array. |
||
| 241 | * |
||
| 242 | * @return array |
||
| 243 | */ |
||
| 244 | View Code Duplication | public function toArray() { |
|
| 245 | $fields = array( |
||
| 246 | 'actionTitle', |
||
| 247 | 'actionCode', |
||
| 248 | 'estimatedTime', |
||
| 249 | 'changes', |
||
| 250 | 'options', |
||
| 251 | 'validationCode', |
||
| 252 | 'messages' |
||
| 253 | ); |
||
| 254 | |||
| 255 | $output = array(); |
||
| 256 | foreach($fields as $field) { |
||
| 257 | $output[$field] = $this->$field; |
||
| 258 | } |
||
| 259 | return $output; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function toJSON() { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Load from JSON associative array. |
||
| 271 | * Environment must be set by the callee when creating this object. |
||
| 272 | * |
||
| 273 | * @param string $json |
||
| 274 | */ |
||
| 275 | public function fromJSON($json) { |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Load from array. |
||
| 282 | * Environment must be set by the callee when creating this object. |
||
| 283 | * |
||
| 284 | * @param string $data |
||
| 285 | */ |
||
| 286 | View Code Duplication | public function fromArray($data) { |
|
| 287 | $fields = array( |
||
| 288 | 'actionTitle', |
||
| 289 | 'actionCode', |
||
| 290 | 'estimatedTime', |
||
| 291 | 'changes', |
||
| 292 | 'options', |
||
| 293 | 'validationCode', |
||
| 294 | 'messages' |
||
| 295 | ); |
||
| 296 | |||
| 297 | foreach($fields as $field) { |
||
| 298 | if(!empty($data[$field])) { |
||
| 299 | $this->$field = $data[$field]; |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @return DNDeployment |
||
| 306 | */ |
||
| 307 | public function createDeployment() { |
||
| 317 | |||
| 318 | } |
||
| 319 | |||
| 320 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..