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 | * Constructor. |
||
105 | */ |
||
106 | 7 | public function __construct($options = array()) |
|
107 | { |
||
108 | 7 | foreach ($options as $name=>$value) { |
|
109 | switch ($name) { |
||
110 | 6 | case 'project_id': $this->setProjectId($value); break; |
|
111 | 6 | View Code Duplication | case 'changes': { |
112 | 6 | $changes = array(); |
|
113 | 6 | foreach ($value as $changeInfo) { |
|
114 | 6 | $changes[] = new Change($changeInfo); |
|
115 | } |
||
116 | 6 | $this->setChanges($changes); break; |
|
117 | } |
||
118 | 6 | case 'created': $this->setCreated($value); break; |
|
119 | 6 | case 'earliest': $this->setEarliest($value); break; |
|
120 | 6 | case 'experiment_ids': $this->setExperimentIds($value); break; |
|
121 | 6 | case 'holdback': $this->setHoldback($value); break; |
|
122 | 6 | case 'last_modified': $this->setLastModified($value); break; |
|
123 | 6 | case 'latest': $this->setLatest($value); break; |
|
124 | 6 | case 'metrics': { |
|
125 | 6 | $metrics = array(); |
|
126 | 6 | foreach ($value as $metricInfo) { |
|
127 | 6 | $metrics[] = new Metric($metricInfo); |
|
128 | } |
||
129 | 6 | $this->setMetrics($metrics); break; |
|
130 | } |
||
131 | 6 | case 'name': $this->setName($value); break; |
|
132 | 6 | case 'page_ids': $this->setPageIds($value); break; |
|
133 | 6 | case 'status': $this->setStatus($value); break; |
|
134 | 6 | case 'type': $this->setType($value); break; |
|
135 | 4 | case 'id': $this->setId($value); break; |
|
136 | default: |
||
137 | 6 | throw new Exception('Unknown option: ' . $name); |
|
138 | } |
||
139 | } |
||
140 | 7 | } |
|
141 | |||
142 | /** |
||
143 | * Returns this object as array. |
||
144 | */ |
||
145 | 3 | public function toArray() |
|
146 | { |
||
147 | $options = array( |
||
148 | 3 | 'project_id' => $this->getProjectId(), |
|
149 | 'changes' => array(), |
||
150 | 3 | 'created' => $this->getCreated(), |
|
151 | 3 | 'earliest' => $this->getEarliest(), |
|
152 | 3 | 'experiment_ids' => $this->getExperimentIds(), |
|
153 | 3 | 'holdback' => $this->getHoldback(), |
|
154 | 3 | 'last_modified' => $this->getLastModified(), |
|
155 | 3 | 'latest' => $this->getLatest(), |
|
156 | 'metrics' => array(), |
||
157 | 3 | 'name' => $this->getName(), |
|
158 | 3 | 'page_ids' => $this->getPageIds(), |
|
159 | 3 | 'status' => $this->getStatus(), |
|
160 | 3 | 'type' => $this->getType(), |
|
161 | 3 | 'id' => $this->getId() |
|
162 | ); |
||
163 | |||
164 | 3 | foreach ($this->getChanges() as $change) { |
|
165 | 3 | $options['changes'][] = $change->toArray(); |
|
166 | } |
||
167 | |||
168 | 3 | foreach ($this->getMetrics() as $metric) { |
|
169 | 3 | $options['metrics'][] = $metric->toArray(); |
|
170 | } |
||
171 | |||
172 | // Remove options with empty values |
||
173 | 3 | $cleanedOptions = array(); |
|
174 | 3 | foreach ($options as $name=>$value) { |
|
175 | 3 | if ($value!==null) |
|
176 | 3 | $cleanedOptions[$name] = $value; |
|
177 | } |
||
178 | |||
179 | 3 | return $cleanedOptions; |
|
180 | } |
||
181 | |||
182 | 5 | public function getProjectId() |
|
186 | |||
187 | 7 | public function setProjectId($projectId) |
|
191 | |||
192 | 5 | public function getChanges() |
|
196 | |||
197 | 7 | public function setChanges($changes) |
|
201 | |||
202 | 4 | public function getCreated() |
|
206 | |||
207 | 7 | public function setCreated($created) |
|
211 | |||
212 | 3 | public function getEarliest() |
|
216 | |||
217 | 7 | public function setEarliest($earliest) |
|
221 | |||
222 | 3 | public function getExperimentIds() |
|
226 | |||
227 | 7 | public function setExperimentIds($experimentIds) |
|
231 | |||
232 | 3 | public function getHoldback() |
|
236 | |||
237 | 7 | public function setHoldback($holdback) |
|
241 | |||
242 | 3 | public function getLastModified() |
|
246 | |||
247 | 7 | public function setLastModified($lastModified) |
|
251 | |||
252 | 3 | public function getLatest() |
|
256 | |||
257 | 7 | public function setLatest($latest) |
|
261 | |||
262 | 5 | public function getMetrics() |
|
266 | |||
267 | 7 | public function setMetrics($metrics) |
|
271 | |||
272 | 6 | public function getName() |
|
276 | |||
277 | 7 | public function setName($name) |
|
281 | |||
282 | 3 | public function getPageIds() |
|
286 | |||
287 | 7 | public function setPageIds($pageIds) |
|
291 | |||
292 | 3 | public function getStatus() |
|
296 | |||
297 | 7 | public function setStatus($status) |
|
301 | |||
302 | 4 | public function getType() |
|
306 | |||
307 | 7 | public function setType($type) |
|
311 | |||
312 | 4 | public function getId() |
|
316 | |||
317 | 5 | public function setId($id) |
|
321 | } |
||
322 | |||
323 | |||
326 |
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.