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 |
||
30 | class Task |
||
31 | { |
||
32 | |||
33 | /** |
||
34 | * First booking of this task |
||
35 | * |
||
36 | * @var \Wicked\Timely\Entities\Booking $startBooking |
||
37 | */ |
||
38 | protected $startBooking; |
||
39 | |||
40 | /** |
||
41 | * Last booking of this task |
||
42 | * |
||
43 | * @var \Wicked\Timely\Entities\Booking $endBooking |
||
44 | */ |
||
45 | protected $endBooking; |
||
46 | |||
47 | /** |
||
48 | * Bookings within this task |
||
49 | * |
||
50 | * @var \Wicked\Timely\Entities\Booking[] $intermediateBookings |
||
51 | */ |
||
52 | protected $intermediateBookings; |
||
53 | |||
54 | /** |
||
55 | * Intermediate tasks, done within this task instance |
||
56 | * |
||
57 | * @var \Wicked\Timely\Entities\Task[] $intermediateTasks |
||
58 | */ |
||
59 | protected $intermediateTasks; |
||
60 | |||
61 | /** |
||
62 | * The task's duration |
||
63 | * |
||
64 | * @var integer $duration |
||
65 | */ |
||
66 | protected $duration; |
||
67 | |||
68 | /** |
||
69 | * Whether or not the task is being clipped due to filtering |
||
70 | * |
||
71 | * @var boolean $isClipped |
||
72 | */ |
||
73 | protected $isClipped = false; |
||
74 | |||
75 | /** |
||
76 | * Default constructor |
||
77 | * |
||
78 | * @param \Wicked\Timely\Entities\Booking $startBooking The first booking of the task |
||
79 | * @param \Wicked\Timely\Entities\Booking $endBooking The last booking of the task |
||
80 | * @param \Wicked\Timely\Entities\Booking[] $intermediateBookings Bookings within this task |
||
81 | */ |
||
82 | 9 | public function __construct($startBooking, $endBooking, $intermediateBookings) |
|
98 | |||
99 | /** |
||
100 | * Getter for the first booking of the task instance |
||
101 | * |
||
102 | * @return \Wicked\Timely\Entities\Booking |
||
103 | */ |
||
104 | 4 | public function getStartBooking() |
|
108 | |||
109 | /** |
||
110 | * Getter for the first booking of the task instance |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | View Code Duplication | public function getStartTime() |
|
122 | |||
123 | /** |
||
124 | * Getter for the first booking of the task instance |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | public function getComment() |
||
132 | |||
133 | /** |
||
134 | * Getter for the first booking of the task instance |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | View Code Duplication | public function isPushed() |
|
146 | |||
147 | /** |
||
148 | * Getter for the last booking of the task instance |
||
149 | * |
||
150 | * @return \Wicked\Timely\Entities\Booking |
||
151 | */ |
||
152 | public function getEndBooking() |
||
156 | |||
157 | /** |
||
158 | * Getter for the first booking of the task instance |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getEndTime() |
||
166 | |||
167 | /** |
||
168 | * Getter for the intermediate bookings |
||
169 | * |
||
170 | * @return \Wicked\Timely\Entities\Booking[] |
||
171 | */ |
||
172 | public function getIntermediateBookings() |
||
176 | |||
177 | /** |
||
178 | * Getter for the task's ticket id |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | public function getTicketId() |
||
186 | |||
187 | /** |
||
188 | * Getter for the task duration |
||
189 | * |
||
190 | * @return integer |
||
191 | */ |
||
192 | 8 | public function getDuration() |
|
196 | |||
197 | /** |
||
198 | * Getter for the task duration |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | protected function isClipped() |
||
206 | |||
207 | /** |
||
208 | * Whether or not the task is still ongoing |
||
209 | * |
||
210 | * @return bool |
||
211 | */ |
||
212 | public function isOngoing() |
||
216 | |||
217 | /** |
||
218 | * Whether or not the task is currently paused |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function isPaused() |
||
235 | |||
236 | /** |
||
237 | * Calculates the duration of a task by given bookings |
||
238 | * |
||
239 | * @param \Wicked\Timely\Entities\Booking $startBooking The first booking of the task |
||
240 | * @param \Wicked\Timely\Entities\Booking $endBooking The last booking of the task |
||
241 | * @param \Wicked\Timely\Entities\Booking[] $intermediateBookings Bookings within this task |
||
242 | * |
||
243 | * @return integer |
||
244 | */ |
||
245 | 9 | protected function calculateDuration($startBooking, $endBooking, array $intermediateBookings) |
|
269 | } |
||
270 |
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.