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:
1 | <?php |
||
8 | class Sprint |
||
9 | { |
||
10 | /** |
||
11 | * @var \DateTime |
||
12 | */ |
||
13 | private $start; |
||
14 | |||
15 | /** |
||
16 | * @var \DateInterval |
||
17 | */ |
||
18 | private $duration; |
||
19 | |||
20 | /** |
||
21 | * @const String |
||
22 | */ |
||
23 | const INTERVAL = 'P1D'; |
||
24 | |||
25 | /** |
||
26 | * @return \DateTime |
||
27 | */ |
||
28 | 7 | public function getStart() |
|
32 | |||
33 | /** |
||
34 | * @return \DateInterval |
||
35 | */ |
||
36 | 1 | public function getDuration() |
|
40 | |||
41 | /** |
||
42 | * @param \DateTime $start |
||
43 | */ |
||
44 | 12 | public function setStart(\DateTime $start) |
|
48 | |||
49 | /** |
||
50 | * @param \DateInterval $duration |
||
51 | */ |
||
52 | 12 | public function setDuration(\DateInterval $duration) |
|
56 | |||
57 | /** |
||
58 | * Calculate the end of sprint from start and duration. |
||
59 | * |
||
60 | * @return \DateTime|null |
||
61 | */ |
||
62 | 8 | public function getEnd() |
|
72 | |||
73 | /** |
||
74 | * Get all days in the sprint from start date |
||
75 | * and during duration. |
||
76 | * |
||
77 | * @return \DatePeriod|null |
||
78 | */ |
||
79 | 7 | public function getSprintDays() |
|
97 | |||
98 | /** |
||
99 | * Calculate the next day in the sprint. |
||
100 | * |
||
101 | * @return \DateTime |
||
102 | */ |
||
103 | 5 | public function getNextDayInSprint() |
|
118 | |||
119 | /** |
||
120 | * Calculate the total work days in the sprint. |
||
121 | * This function does not return week-end days but |
||
122 | * return non-work-days such as christmas. |
||
123 | * |
||
124 | * @return int | null |
||
|
|||
125 | */ |
||
126 | 3 | public function getTotalWorkDays() |
|
144 | } |
||
145 |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.