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 Experiment 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 Experiment, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Experiment |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The project the Experiment is in |
||
| 22 | * @var integer |
||
| 23 | */ |
||
| 24 | private $projectId; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * List of IDs of all audiences the Experiment is targeted at |
||
| 28 | * @var array[integer] |
||
| 29 | */ |
||
| 30 | private $audienceIds; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The ID for the Campaign that the Experiment is in |
||
| 34 | * @var integer |
||
| 35 | */ |
||
| 36 | private $campaignId; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Experiment-level changes that will run before all Variations. Typically |
||
| 40 | * this is custom CSS or custom JavaScript. |
||
| 41 | * @var array[Change] |
||
| 42 | */ |
||
| 43 | private $changes; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The time the experiment was initially created |
||
| 47 | * @var string |
||
| 48 | */ |
||
| 49 | private $created; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The description or hypothesis for an Experiment |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $description; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Percentage expressed as a number from 0-10000 to hold back from being |
||
| 59 | * included in the experiment |
||
| 60 | * @var integer |
||
| 61 | */ |
||
| 62 | private $holdback; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Unique string identifier for this experiment within the project. |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | private $key; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * The last time the experiment was modified |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $lastModified; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * An ordered list of metrics to track for the experiment |
||
| 78 | * @var array[Metric] |
||
| 79 | */ |
||
| 80 | private $metrics; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Name of the Experiment |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $name; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * The last time the experiment was modified |
||
| 90 | * @var Schedule |
||
| 91 | */ |
||
| 92 | private $schedule; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Current state of the experiment. Can be 'active', 'paused' or 'archived'. |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | private $status; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * A list of Variations that each define an experience to show in the context |
||
| 102 | * of the Experiment for the purpose of comparison against each other |
||
| 103 | * @var array[Variation] |
||
| 104 | */ |
||
| 105 | private $variations = array(); |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The unique identifier for the Experiment |
||
| 109 | * @var integer |
||
| 110 | */ |
||
| 111 | private $id; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Whether or not the Experiment is a classic Experiment |
||
| 115 | * @var boolean |
||
| 116 | */ |
||
| 117 | private $isClassic; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Indicates whether this is a standalone a/b experiment or an experience within a personalization campaign |
||
| 121 | * Can be a/b or personalization |
||
| 122 | * @var string |
||
| 123 | */ |
||
| 124 | private $type; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The audiences that should see this experiment. If the field is null or |
||
| 128 | * omitted, the experiment will target everyone. Multiple audiences can be |
||
| 129 | * combined with "and" or "or" using the same structure as audience conditions |
||
| 130 | * @var string |
||
| 131 | */ |
||
| 132 | private $audienceConditions; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Traffic allocation policy across variations in this experiment |
||
| 136 | * @var string |
||
| 137 | */ |
||
| 138 | private $allocationPolicy; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The first time the Experiment was activated |
||
| 142 | * @var type |
||
| 143 | */ |
||
| 144 | private $earliest; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * A list of Page IDs used in the Experiment. url_targeting or page_ids, but not both. |
||
| 148 | */ |
||
| 149 | private $pageIds; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * The last time the Experiment was activated (not present if it is still activated) |
||
| 153 | * @var type |
||
| 154 | */ |
||
| 155 | private $latest; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Constructor. |
||
| 159 | */ |
||
| 160 | 7 | public function __construct($options = array()) |
|
| 212 | |||
| 213 | /** |
||
| 214 | * Returns this object as array. |
||
| 215 | */ |
||
| 216 | 3 | public function toArray() |
|
| 276 | |||
| 277 | 5 | public function getProjectId() |
|
| 281 | |||
| 282 | 7 | public function setProjectId($projectId) |
|
| 286 | |||
| 287 | 4 | public function getAudienceIds() |
|
| 291 | |||
| 292 | 7 | public function setAudienceIds($audienceIds) |
|
| 296 | |||
| 297 | 4 | public function getCampaignId() |
|
| 301 | |||
| 302 | 7 | public function setCampaignId($campaignId) |
|
| 306 | |||
| 307 | 4 | public function getChanges() |
|
| 311 | |||
| 312 | 7 | public function setChanges($changes) |
|
| 316 | |||
| 317 | 4 | public function getCreated() |
|
| 321 | |||
| 322 | 5 | public function setCreated($created) |
|
| 326 | |||
| 327 | 4 | public function getDescription() |
|
| 331 | |||
| 332 | 7 | public function setDescription($description) |
|
| 336 | |||
| 337 | 4 | public function getHoldback() |
|
| 341 | |||
| 342 | 7 | public function setHoldback($holdback) |
|
| 346 | |||
| 347 | 4 | public function getKey() |
|
| 351 | |||
| 352 | 7 | public function setKey($key) |
|
| 356 | |||
| 357 | 4 | public function getLastModified() |
|
| 361 | |||
| 362 | 5 | public function setLastModified($lastModified) |
|
| 366 | |||
| 367 | 4 | public function getMetrics() |
|
| 371 | |||
| 372 | 7 | public function setMetrics($metrics) |
|
| 376 | |||
| 377 | 7 | public function getName() |
|
| 381 | |||
| 382 | 7 | public function setName($name) |
|
| 386 | |||
| 387 | 4 | public function getSchedule() |
|
| 391 | |||
| 392 | 7 | public function setSchedule($schedule) |
|
| 396 | |||
| 397 | 4 | public function getStatus() |
|
| 401 | |||
| 402 | 7 | public function setStatus($status) |
|
| 406 | |||
| 407 | 4 | public function getVariations() |
|
| 411 | |||
| 412 | 7 | public function setVariations($variations) |
|
| 416 | |||
| 417 | 4 | public function getId() |
|
| 421 | |||
| 422 | 5 | public function setId($id) |
|
| 426 | |||
| 427 | 4 | public function getIsClassic() |
|
| 431 | |||
| 432 | 5 | public function setIsClassic($isClassic) |
|
| 436 | |||
| 437 | 3 | public function getType() |
|
| 441 | |||
| 442 | 2 | public function setType($type) |
|
| 446 | |||
| 447 | 3 | public function getAudienceConditions() |
|
| 451 | |||
| 452 | 2 | public function setAudienceConditions($audienceConditions) |
|
| 456 | |||
| 457 | public function getConfig() |
||
| 461 | |||
| 462 | public function setConfig($config) |
||
| 466 | |||
| 467 | 3 | public function getAllocationPolicy() |
|
| 471 | |||
| 472 | public function setAllocationPolicy($allocationPolicy) |
||
| 476 | |||
| 477 | 3 | public function getEarliest() |
|
| 481 | |||
| 482 | public function setEarliest($earliest) |
||
| 486 | |||
| 487 | 3 | public function getPageIds() |
|
| 491 | |||
| 492 | public function setPageIds($pageIds) |
||
| 496 | |||
| 497 | 3 | public function getLatest() |
|
| 501 | |||
| 502 | public function setLatest($latest) |
||
| 506 | } |
||
| 507 | |||
| 508 |
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.