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 Segment 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 Segment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Segment |
||
8 | { |
||
9 | /** @var int */ |
||
10 | protected $segmentId; |
||
11 | |||
12 | /** @var string */ |
||
13 | protected $segmentName; |
||
14 | |||
15 | /** @var string */ |
||
16 | protected $segmentStatus; |
||
17 | |||
18 | /** @var null|string */ |
||
19 | protected $description; |
||
20 | |||
21 | /** @var null|string */ |
||
22 | protected $integrationCode; |
||
23 | |||
24 | /** @var null|string */ |
||
25 | protected $accountUserListStatus; |
||
26 | |||
27 | /** @var null */ |
||
28 | protected $accessReason; |
||
29 | |||
30 | /** @var null */ |
||
31 | protected $isEligibleForSearch; |
||
32 | |||
33 | /** @var null */ |
||
34 | protected $membershipLifeSpan; |
||
35 | |||
36 | /** @var string */ |
||
37 | protected $listType; |
||
38 | |||
39 | /** @var string */ |
||
40 | protected $size; |
||
41 | |||
42 | public function __construct($segmentId, $segmentName, $segmentStatus, $description = null, $integrationCode = null, $accountUserListStatus = null, $accessReason = null, $isEligibleForSearch = null, $membershipLifeSpan = null) |
||
54 | |||
55 | /** |
||
56 | * @return int |
||
57 | */ |
||
58 | public function getSegmentId() |
||
62 | |||
63 | /** |
||
64 | * @return string |
||
65 | */ |
||
66 | public function getSegmentName() |
||
70 | |||
71 | /** |
||
72 | * @param $name |
||
73 | */ |
||
74 | public function setSegmentName($name) |
||
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | public function getSegmentStatus() |
||
86 | |||
87 | /** |
||
88 | * @return string |
||
89 | */ |
||
90 | public function getDescription() |
||
94 | |||
95 | /** |
||
96 | * @param string $description |
||
97 | */ |
||
98 | public function setDescription($description) |
||
102 | |||
103 | /** |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getIntegrationCode() |
||
110 | |||
111 | /** |
||
112 | * @param string $integrationCode |
||
113 | */ |
||
114 | public function setIntegrationCode($integrationCode) |
||
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | public function getAccountUserListStatus() |
||
126 | |||
127 | /** |
||
128 | * @param string $accountUserListStatus |
||
129 | */ |
||
130 | public function setAccountUserListStatus($accountUserListStatus) |
||
134 | |||
135 | /** |
||
136 | * @return mixed |
||
137 | */ |
||
138 | public function getAccessReason() |
||
142 | |||
143 | /** |
||
144 | * @param mixed $accessReason |
||
145 | */ |
||
146 | public function setAccessReason($accessReason) |
||
150 | |||
151 | /** |
||
152 | * @return mixed |
||
153 | */ |
||
154 | public function getisEligibleForSearch() |
||
158 | |||
159 | /** |
||
160 | * @param mixed $isEligibleForSearch |
||
161 | */ |
||
162 | public function setIsEligibleForSearch($isEligibleForSearch) |
||
166 | |||
167 | /** |
||
168 | * @return mixed |
||
169 | */ |
||
170 | public function getMembershipLifeSpan() |
||
174 | |||
175 | /** |
||
176 | * @param mixed $membershipLifeSpan |
||
177 | */ |
||
178 | public function setMembershipLifeSpan($membershipLifeSpan) |
||
182 | |||
183 | /** |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function getListType() |
||
190 | |||
191 | /** |
||
192 | * @param $listType |
||
193 | */ |
||
194 | public function setListType($listType) |
||
198 | |||
199 | /** |
||
200 | * @return mixed |
||
201 | */ |
||
202 | public function getSize() |
||
206 | |||
207 | /** |
||
208 | * @param mixed $size |
||
209 | */ |
||
210 | public function setSize($size) |
||
214 | |||
215 | public static function fromArray(array $array) |
||
267 | } |
||
268 |
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.