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 Event 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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Event |
||
15 | { |
||
16 | /** |
||
17 | * A machine readable name for this Event |
||
18 | * @var string |
||
19 | */ |
||
20 | private $apiName; |
||
21 | |||
22 | /** |
||
23 | * Whether or not this Event has been archived |
||
24 | * @var boolean |
||
25 | */ |
||
26 | private $archived; |
||
27 | |||
28 | /** |
||
29 | * A category for this Event. Can be 'add_to_cart', 'save', 'search', 'share', |
||
30 | * 'purchase', 'convert', 'sign_up', 'subscribe' or 'other'. |
||
31 | * @var type |
||
32 | */ |
||
33 | private $category; |
||
34 | |||
35 | /** |
||
36 | * Creation date for this Event |
||
37 | * @var string |
||
38 | */ |
||
39 | private $created; |
||
40 | |||
41 | /** |
||
42 | * A description for this Event |
||
43 | * @var string |
||
44 | */ |
||
45 | private $description; |
||
46 | |||
47 | /** |
||
48 | * A filter object for this Event |
||
49 | * @var EventFilter |
||
50 | */ |
||
51 | private $eventFilter; |
||
52 | |||
53 | /** |
||
54 | * The type of this Event. Can be custom, click, pageview, classic_custom, |
||
55 | * classic_click, classic_pageview, classic_engagement, classic_revenue, |
||
56 | * classic_mobile_tap, classic_mobile_view, classic_mobile_session, |
||
57 | * classic_mobile_session_length or classic_mobile_num_session |
||
58 | * @var string |
||
59 | */ |
||
60 | private $eventType; |
||
61 | |||
62 | /** |
||
63 | * Unique string identifier for this event within the project. |
||
64 | * @var string |
||
65 | */ |
||
66 | private $key; |
||
67 | |||
68 | /** |
||
69 | * A human readable name for this Event |
||
70 | * @var string |
||
71 | */ |
||
72 | private $name; |
||
73 | |||
74 | /** |
||
75 | * The Page ID associated with this Event |
||
76 | * @var integer |
||
77 | */ |
||
78 | private $pageId; |
||
79 | |||
80 | /** |
||
81 | * The parent Project ID of this Event |
||
82 | * @var integer |
||
83 | */ |
||
84 | private $projectId; |
||
85 | |||
86 | /** |
||
87 | * The unique identifier of the Event |
||
88 | * @var integer |
||
89 | */ |
||
90 | private $id; |
||
91 | |||
92 | /** |
||
93 | * Whether or not this Event is a classic Event |
||
94 | * @var boolean |
||
95 | */ |
||
96 | private $isClassic; |
||
97 | |||
98 | /** |
||
99 | * Whether this Event may be edited |
||
100 | * @var boolean |
||
101 | */ |
||
102 | private $isEditable; |
||
103 | |||
104 | /** |
||
105 | * Constructor. |
||
106 | */ |
||
107 | 9 | View Code Duplication | public function __construct($options = array()) |
130 | |||
131 | /** |
||
132 | * Returns this object as array. |
||
133 | */ |
||
134 | 5 | View Code Duplication | public function toArray() |
162 | |||
163 | 5 | public function getApiName() |
|
167 | |||
168 | public function setApiName($apiName) |
||
172 | |||
173 | 7 | public function getArchived() |
|
177 | |||
178 | 9 | public function setArchived($archived) |
|
182 | |||
183 | 7 | public function getCategory() |
|
187 | |||
188 | 9 | public function setCategory($category) |
|
192 | |||
193 | 5 | public function getCreated() |
|
197 | |||
198 | 9 | public function setCreated($created) |
|
202 | |||
203 | 5 | public function getDescription() |
|
207 | |||
208 | 9 | public function setDescription($description) |
|
212 | |||
213 | 7 | public function getEventFilter() |
|
217 | |||
218 | 7 | public function setEventFilter($eventFilter) |
|
222 | |||
223 | 5 | public function getEventType() |
|
227 | |||
228 | 9 | public function setEventType($eventType) |
|
232 | |||
233 | 5 | public function getKey() |
|
237 | |||
238 | 9 | public function setKey($key) |
|
242 | |||
243 | 9 | public function getName() |
|
247 | |||
248 | 9 | public function setName($name) |
|
252 | |||
253 | 5 | public function getPageId() |
|
257 | |||
258 | 9 | public function setPageId($pageId) |
|
262 | |||
263 | 5 | public function getProjectId() |
|
267 | |||
268 | 9 | public function setProjectId($projectId) |
|
272 | |||
273 | 5 | public function getId() |
|
277 | |||
278 | 9 | public function setId($id) |
|
282 | |||
283 | 5 | public function getIsClassic() |
|
287 | |||
288 | 9 | public function setIsClassic($isClassic) |
|
292 | |||
293 | 7 | public function getIsEditable() |
|
297 | |||
298 | 9 | public function setIsEditable($isEditable) |
|
302 | } |
||
303 | |||
309 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.