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 namespace Myth\Cron; |
||
46 | class CronTask { |
||
47 | |||
48 | /** |
||
49 | * The original scheduled string. |
||
50 | * Any valid relative time string. |
||
51 | * http://php.net/manual/en/datetime.formats.relative.php |
||
52 | * |
||
53 | * @var |
||
54 | */ |
||
55 | protected $schedule; |
||
56 | |||
57 | /** |
||
58 | * Stores the callable or library name:method to run. |
||
59 | * |
||
60 | * @var |
||
61 | */ |
||
62 | protected $task; |
||
63 | |||
64 | //-------------------------------------------------------------------- |
||
65 | |||
66 | /** |
||
67 | * Stores our scheduled string and actual task. |
||
68 | * |
||
69 | * @param $schedule |
||
70 | * @param callable $task |
||
71 | */ |
||
72 | public function __construct($schedule, $task) |
||
85 | |||
86 | //-------------------------------------------------------------------- |
||
87 | |||
88 | /** |
||
89 | * Calculates the next date this task is supposed to run. |
||
90 | * |
||
91 | * @param int|'now' $current_time |
||
92 | * |
||
93 | * @return timestamp|null |
||
94 | */ |
||
95 | View Code Duplication | public function nextRunDate($current_time='now') |
|
116 | |||
117 | //-------------------------------------------------------------------- |
||
118 | |||
119 | /** |
||
120 | * Calculates the last time the task should have ran. |
||
121 | * |
||
122 | * @param int|'now' $current_time |
||
123 | * |
||
124 | * @return timestamp|null |
||
125 | */ |
||
126 | View Code Duplication | public function previousRunDate($current_time='now') |
|
147 | |||
148 | //-------------------------------------------------------------------- |
||
149 | |||
150 | /** |
||
151 | * Determines if the task is due to be run now. |
||
152 | * |
||
153 | * @param string $current_time |
||
154 | * @internal param $ int|'now' $current_time |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function isDue($current_time='now') |
||
166 | |||
167 | //-------------------------------------------------------------------- |
||
168 | |||
169 | /** |
||
170 | * Formats the timestamp produced by nextRunDate and previousRunDate |
||
171 | * into any format available to date. |
||
172 | * |
||
173 | * @param $format_string |
||
174 | * @return bool|string |
||
175 | */ |
||
176 | public function format($format_string) |
||
180 | |||
181 | //-------------------------------------------------------------------- |
||
182 | |||
183 | /** |
||
184 | * Gets the associated task. |
||
185 | * |
||
186 | * return callable|string |
||
187 | */ |
||
188 | public function task() |
||
192 | |||
193 | //-------------------------------------------------------------------- |
||
194 | |||
195 | /** |
||
196 | * Gets the original schedule string. |
||
197 | */ |
||
198 | public function schedule() |
||
202 | |||
203 | //-------------------------------------------------------------------- |
||
204 | |||
205 | /** |
||
206 | * Checks the schedule text and determines how we have to treat |
||
207 | * the schedule when determining next and previous values. |
||
208 | * |
||
209 | * Potential Types are: |
||
210 | * |
||
211 | * - increment Can simply add a +x/-x to the front to get the value. |
||
212 | * - time Something like "every 5 minutes" |
||
213 | * - ordinal Like "first", "second", etc. |
||
214 | * |
||
215 | * @param $schedule |
||
216 | * @return null|string |
||
217 | */ |
||
218 | public function determineScheduleType($schedule) |
||
260 | |||
261 | //-------------------------------------------------------------------- |
||
262 | |||
263 | /** |
||
264 | * Determines the correct time for 'time' type intervals where |
||
265 | * the timestamp is expected to happen every 'x period', like |
||
266 | * 'every 5 minutes', every 3 days, etc. |
||
267 | * |
||
268 | * @param $schedule |
||
269 | * @param $type |
||
270 | * @return float|int|null |
||
271 | */ |
||
272 | public function findDateInterval($schedule, $type, $current_time='now') |
||
309 | |||
310 | //-------------------------------------------------------------------- |
||
311 | |||
312 | /** |
||
313 | * Determines the timestamp of the previous ordinal-based time, like |
||
314 | * 'second Monday'. |
||
315 | * |
||
316 | * @param $schedule |
||
317 | * @param string $current_time |
||
318 | * @return int|null |
||
319 | */ |
||
320 | public function findPreviousOrdinal($schedule, $current_time='now') |
||
342 | |||
343 | //-------------------------------------------------------------------- |
||
344 | |||
345 | } |
||
346 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.