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 UserStatistics 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 UserStatistics, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class UserStatistics extends Model |
||
12 | { |
||
13 | /** |
||
14 | * Get the user's statistics. |
||
15 | * |
||
16 | * @return Array of Statistics. |
||
17 | */ |
||
18 | public function getStatistics() |
||
26 | |||
27 | /** |
||
28 | * Get the user's statistics. |
||
29 | * |
||
30 | * @return array of statistics. |
||
31 | */ |
||
32 | public function get() |
||
40 | |||
41 | /** |
||
42 | * Get a statistic. |
||
43 | * |
||
44 | * @param $typeCode code of the category type. |
||
45 | * @param $categoryCode code of the statistical category. |
||
46 | * |
||
47 | * @return Statistic. |
||
48 | */ |
||
49 | public function getStatistic($typeCode,$categoryCode) |
||
60 | |||
61 | /** |
||
62 | * Add a user statistic with no regard to existing Types and Categories. |
||
63 | * |
||
64 | * @param string $typeCode code of the category type. |
||
65 | * @param string $typeDesc description of the category type. |
||
66 | * @param string $categoryCode code of the statistical category. |
||
67 | * @param string $categoryDesc description of the statistical category. |
||
68 | * @param string $note free text description of the statistic. |
||
69 | * @param string $segment_type (Internal|External) |
||
70 | * |
||
71 | * @return Statistic |
||
72 | */ |
||
73 | public function addStatisticRaw($typeCode,$typeDesc,$categoryCode,$categoryDesc,$segment_type,$note) |
||
96 | |||
97 | /** |
||
98 | * Add a user statistic based upon existing Types and Categories. |
||
99 | * |
||
100 | * @param string $typeCode code of the category type. |
||
101 | * @param string $categoryCode code of the statistical category. |
||
102 | * |
||
103 | * @return Statistic |
||
104 | */ |
||
105 | public function addStatistic($typeCode,$categoryCode,$segmentType,$note) |
||
138 | |||
139 | /** |
||
140 | * Delete a user statistic. |
||
141 | * |
||
142 | * @param string $typeCode code of the category type. |
||
143 | * @param string $categoryCode code of the statistical category. |
||
144 | */ |
||
145 | public function removeStatistic($typeCode,$categoryCode) |
||
164 | |||
165 | /** |
||
166 | * Update a user statistic. |
||
167 | * |
||
168 | */ |
||
169 | public function updateStatistic($fromTypeCode,$fromCategoryCode,$toTypeCode,$toCategoryCode,$segmentType,$note) |
||
175 | |||
176 | /** |
||
177 | * Get Stats By Type Code. |
||
178 | * |
||
179 | * @return Array of Statistics. |
||
180 | */ |
||
181 | View Code Duplication | public function getStatsByTypeCode($typeCode) |
|
192 | |||
193 | /** |
||
194 | * Get Stats by Type Desc. |
||
195 | * |
||
196 | * @return Array of statistics. |
||
197 | */ |
||
198 | View Code Duplication | public function getStatsByTypeDesc($typeDesc) |
|
208 | |||
209 | /** |
||
210 | * Get Stats by Category Code. |
||
211 | * |
||
212 | * @return Array of Statistics. |
||
213 | */ |
||
214 | View Code Duplication | public function getStatsByCategoryCode($categoryCode) |
|
224 | |||
225 | /** |
||
226 | * Get Stats by Category Desc. |
||
227 | * |
||
228 | * @return Array of Statistics. |
||
229 | */ |
||
230 | View Code Duplication | public function getStatsByCategoryDesc($categoryDesc) |
|
240 | |||
241 | /** |
||
242 | * Get Stats by Segment Type. |
||
243 | * |
||
244 | * @return Array of Statistics. |
||
245 | */ |
||
246 | public function getStatsBySegmentType($segmentType) |
||
256 | |||
257 | /** |
||
258 | * Search Stats by Note. |
||
259 | * |
||
260 | * @return Array of Statistics. |
||
261 | */ |
||
262 | public function searchStatsByNote($note) |
||
272 | |||
273 | /** |
||
274 | * Search Stats by Type Code. |
||
275 | * |
||
276 | * @return Array of Statistics. |
||
277 | */ |
||
278 | View Code Duplication | public function searchStatsByTypeCode($typeCode) |
|
288 | |||
289 | /** |
||
290 | * Search Stats by Type Code. |
||
291 | * |
||
292 | * @return Array of Statistics. |
||
293 | */ |
||
294 | View Code Duplication | public function searchStatsByTypeDesc($typeDesc) |
|
304 | |||
305 | /** |
||
306 | * Search Stats by Type Code. |
||
307 | * |
||
308 | * @return Array of Statistics. |
||
309 | */ |
||
310 | View Code Duplication | public function searchStatsByCategoryCode($categoryCode) |
|
320 | |||
321 | /** |
||
322 | * Search Stats by Type Code. |
||
323 | * |
||
324 | * @return Array of Statistics. |
||
325 | */ |
||
326 | View Code Duplication | public function searchStatsByCategoryDesc($categoryDesc) |
|
336 | |||
337 | } |
||
338 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.