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 |
||
15 | class Campaign |
||
16 | { |
||
17 | /** |
||
18 | * The Project ID the Campaign is in |
||
19 | * @var integer |
||
20 | */ |
||
21 | private $projectId; |
||
22 | |||
23 | /** |
||
24 | * A list of changes to apply to the Campaign |
||
25 | * @var array |
||
26 | */ |
||
27 | private $changes; |
||
28 | |||
29 | /** |
||
30 | * The time the Experiment was initially created |
||
31 | * @var string |
||
32 | */ |
||
33 | private $created; |
||
34 | |||
35 | /** |
||
36 | * The first time the Experiment was activated |
||
37 | * @var string |
||
38 | */ |
||
39 | private $earliest; |
||
40 | |||
41 | /** |
||
42 | * An ordered list of Experiment IDs used by the Campaign |
||
43 | * @var array |
||
44 | */ |
||
45 | private $experimentIds; |
||
46 | |||
47 | /** |
||
48 | * Percentage expressed as a number from 0-10000 to holdback from being |
||
49 | * included in the experiment. |
||
50 | * @var integer |
||
51 | */ |
||
52 | private $holdback; |
||
53 | |||
54 | /** |
||
55 | * The last time the Experiment was modified |
||
56 | * @var string |
||
57 | */ |
||
58 | private $lastModified; |
||
59 | |||
60 | /** |
||
61 | * The last time the Experiment was activated (not present if it is still activated) |
||
62 | * @var string |
||
63 | */ |
||
64 | private $latest; |
||
65 | |||
66 | /** |
||
67 | * An ordered list of Metrics to track for the Campaign |
||
68 | * @var array |
||
69 | */ |
||
70 | private $metrics; |
||
71 | |||
72 | /** |
||
73 | * The name of the Campaign |
||
74 | * @var string |
||
75 | */ |
||
76 | private $name; |
||
77 | |||
78 | /** |
||
79 | * A list of Page IDs used in the Campaign. |
||
80 | * @var array |
||
81 | */ |
||
82 | private $pageIds; |
||
83 | |||
84 | /** |
||
85 | * Current state of the Campaign. Can be 'active', 'paused' or 'archived' |
||
86 | * @var string |
||
87 | */ |
||
88 | private $status; |
||
89 | |||
90 | /** |
||
91 | * The type of the Campaign. Can be a/b or personalization. |
||
92 | * @var string |
||
93 | */ |
||
94 | private $type; |
||
95 | |||
96 | /** |
||
97 | * The unique identifier for the Campaign |
||
98 | * @var integer |
||
99 | */ |
||
100 | private $id; |
||
101 | |||
102 | /** |
||
103 | * Constructor. |
||
104 | */ |
||
105 | 7 | public function __construct($options = array()) |
|
140 | |||
141 | /** |
||
142 | * Returns this object as array. |
||
143 | */ |
||
144 | 3 | public function toArray() |
|
180 | |||
181 | 5 | public function getProjectId() |
|
185 | |||
186 | 7 | public function setProjectId($projectId) |
|
190 | |||
191 | 5 | public function getChanges() |
|
195 | |||
196 | 7 | public function setChanges($changes) |
|
200 | |||
201 | 4 | public function getCreated() |
|
205 | |||
206 | 7 | public function setCreated($created) |
|
210 | |||
211 | 3 | public function getEarliest() |
|
215 | |||
216 | 7 | public function setEarliest($earliest) |
|
220 | |||
221 | 3 | public function getExperimentIds() |
|
225 | |||
226 | 7 | public function setExperimentIds($experimentIds) |
|
230 | |||
231 | 3 | public function getHoldback() |
|
235 | |||
236 | 7 | public function setHoldback($holdback) |
|
240 | |||
241 | 3 | public function getLastModified() |
|
245 | |||
246 | 7 | public function setLastModified($lastModified) |
|
250 | |||
251 | 3 | public function getLatest() |
|
255 | |||
256 | 7 | public function setLatest($latest) |
|
260 | |||
261 | 5 | public function getMetrics() |
|
265 | |||
266 | 7 | public function setMetrics($metrics) |
|
270 | |||
271 | 6 | public function getName() |
|
275 | |||
276 | 7 | public function setName($name) |
|
280 | |||
281 | 3 | public function getPageIds() |
|
285 | |||
286 | 7 | public function setPageIds($pageIds) |
|
290 | |||
291 | 3 | public function getStatus() |
|
295 | |||
296 | 7 | public function setStatus($status) |
|
300 | |||
301 | 4 | public function getType() |
|
305 | |||
306 | 7 | public function setType($type) |
|
310 | |||
311 | 4 | public function getId() |
|
315 | |||
316 | 5 | public function setId($id) |
|
320 | } |
||
321 | |||
325 |
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.