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 Campaign 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 Campaign, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Campaign |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The Project ID the Campaign is in |
||
| 20 | * @var integer |
||
| 21 | */ |
||
| 22 | private $projectId; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * A list of changes to apply to the Campaign |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $changes; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The time the Experiment was initially created |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $created; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The first time the Experiment was activated |
||
| 38 | * @var string |
||
| 39 | */ |
||
| 40 | private $earliest; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * An ordered list of Experiment IDs used by the Campaign |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $experimentIds; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Percentage expressed as a number from 0-10000 to holdback from being |
||
| 50 | * included in the experiment. |
||
| 51 | * @var integer |
||
| 52 | */ |
||
| 53 | private $holdback; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The last time the Experiment was modified |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $lastModified; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The last time the Experiment was activated (not present if it is still activated) |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $latest; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * An ordered list of Metrics to track for the Campaign |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | private $metrics; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The name of the Campaign |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $name; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * A list of Page IDs used in the Campaign. |
||
| 81 | * @var array |
||
| 82 | */ |
||
| 83 | private $pageIds; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Current state of the Campaign. Can be 'active', 'paused' or 'archived' |
||
| 87 | * @var string |
||
| 88 | */ |
||
| 89 | private $status; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The type of the Campaign. Can be a/b or personalization. |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private $type; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The unique identifier for the Campaign |
||
| 99 | * @var integer |
||
| 100 | */ |
||
| 101 | private $id; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * |
||
| 105 | * @var type |
||
| 106 | */ |
||
| 107 | private $experimentPriorities; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Constructor. |
||
| 111 | */ |
||
| 112 | 7 | public function __construct($options = array()) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Returns this object as array. |
||
| 151 | */ |
||
| 152 | 3 | public function toArray() |
|
| 189 | |||
| 190 | 5 | public function getProjectId() |
|
| 194 | |||
| 195 | 7 | public function setProjectId($projectId) |
|
| 199 | |||
| 200 | 5 | public function getChanges() |
|
| 204 | |||
| 205 | 7 | public function setChanges($changes) |
|
| 209 | |||
| 210 | 4 | public function getCreated() |
|
| 214 | |||
| 215 | 7 | public function setCreated($created) |
|
| 219 | |||
| 220 | 3 | public function getEarliest() |
|
| 224 | |||
| 225 | 7 | public function setEarliest($earliest) |
|
| 229 | |||
| 230 | 3 | public function getExperimentIds() |
|
| 234 | |||
| 235 | 7 | public function setExperimentIds($experimentIds) |
|
| 239 | |||
| 240 | 3 | public function getHoldback() |
|
| 244 | |||
| 245 | 7 | public function setHoldback($holdback) |
|
| 249 | |||
| 250 | 3 | public function getLastModified() |
|
| 254 | |||
| 255 | 7 | public function setLastModified($lastModified) |
|
| 259 | |||
| 260 | 3 | public function getLatest() |
|
| 264 | |||
| 265 | 7 | public function setLatest($latest) |
|
| 269 | |||
| 270 | 5 | public function getMetrics() |
|
| 274 | |||
| 275 | 7 | public function setMetrics($metrics) |
|
| 279 | |||
| 280 | 6 | public function getName() |
|
| 284 | |||
| 285 | 7 | public function setName($name) |
|
| 289 | |||
| 290 | 3 | public function getPageIds() |
|
| 294 | |||
| 295 | 7 | public function setPageIds($pageIds) |
|
| 299 | |||
| 300 | 3 | public function getStatus() |
|
| 304 | |||
| 305 | 7 | public function setStatus($status) |
|
| 309 | |||
| 310 | 4 | public function getType() |
|
| 314 | |||
| 315 | 7 | public function setType($type) |
|
| 319 | |||
| 320 | 4 | public function getId() |
|
| 324 | |||
| 325 | 5 | public function setId($id) |
|
| 329 | |||
| 330 | 3 | public function getExperimentPriorities() |
|
| 334 | |||
| 335 | public function setExperimentPriorities($experimentPriorities) |
||
| 339 | } |
||
| 340 | |||
| 344 |
According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.
}
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.