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:
Complex classes like DeploymentStrategy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use DeploymentStrategy, and based on these observations, apply Extract Interface, too.
| 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 array |
||
| 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) { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @param array $data |
||
| 124 | */ |
||
| 125 | public function setChanges($data) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $title |
||
| 131 | * @param string $desc |
||
| 132 | */ |
||
| 133 | public function setChangeDescriptionOnly($title, $desc) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Filter the changeset where modification was not required. |
||
| 141 | * |
||
| 142 | * @return array |
||
| 143 | */ |
||
| 144 | public function getChangesModificationNeeded() { |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @return array Associative array of changes, e.g. |
||
| 162 | * array( |
||
| 163 | * 'SHA' => array( |
||
| 164 | * 'from' => 'abc', |
||
| 165 | * 'to' => 'def' |
||
| 166 | * ) |
||
| 167 | * ) |
||
| 168 | */ |
||
| 169 | public function getChanges() { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Returns a change or a given key. |
||
| 175 | * |
||
| 176 | * @return ArrayData|null |
||
| 177 | */ |
||
| 178 | public function getChange($key) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param string $option |
||
| 188 | * @param string $value |
||
| 189 | */ |
||
| 190 | public function setOption($option, $value) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $option |
||
| 196 | * @return string|null |
||
| 197 | */ |
||
| 198 | public function getOption($option) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @return string |
||
| 206 | */ |
||
| 207 | public function getOptions() { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param string $code |
||
| 213 | */ |
||
| 214 | public function setValidationCode($code) { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getValidationCode() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @param string $msg |
||
| 227 | */ |
||
| 228 | public function setMessage($msg, $code = self::ERROR_CODE) { |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return array |
||
| 247 | */ |
||
| 248 | public function getMessages() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Transform the deployment strategy to an array. |
||
| 254 | * |
||
| 255 | * @return array |
||
| 256 | */ |
||
| 257 | View Code Duplication | public function toArray() { |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function toJSON() { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Load from JSON associative array. |
||
| 284 | * Environment must be set by the callee when creating this object. |
||
| 285 | * |
||
| 286 | * @param string $json |
||
| 287 | */ |
||
| 288 | public function fromJSON($json) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Load from array. |
||
| 295 | * Environment must be set by the callee when creating this object. |
||
| 296 | * |
||
| 297 | * @param string $data |
||
| 298 | */ |
||
| 299 | View Code Duplication | public function fromArray($data) { |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @return DNDeployment |
||
| 319 | */ |
||
| 320 | View Code Duplication | public function createDeployment() { |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @param int $deploymentID |
||
| 340 | * @return \DNDeployment |
||
| 341 | */ |
||
| 342 | View Code Duplication | public function updateDeployment($deploymentID) { |
|
| 359 | |||
| 360 | } |
||
| 361 | |||
| 362 |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.