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 RecurrenceRule 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 RecurrenceRule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class RecurrenceRule implements ValueInterface |
||
24 | { |
||
25 | const FREQ_YEARLY = 'YEARLY'; |
||
26 | const FREQ_MONTHLY = 'MONTHLY'; |
||
27 | const FREQ_WEEKLY = 'WEEKLY'; |
||
28 | const FREQ_DAILY = 'DAILY'; |
||
29 | const FREQ_HOURLY = 'HOURLY'; |
||
30 | const FREQ_MINUTELY = 'MINUTELY'; |
||
31 | const FREQ_SECONDLY = 'SECONDLY'; |
||
32 | |||
33 | const WEEKDAY_SUNDAY = 'SU'; |
||
34 | const WEEKDAY_MONDAY = 'MO'; |
||
35 | const WEEKDAY_TUESDAY = 'TU'; |
||
36 | const WEEKDAY_WEDNESDAY = 'WE'; |
||
37 | const WEEKDAY_THURSDAY = 'TH'; |
||
38 | const WEEKDAY_FRIDAY = 'FR'; |
||
39 | const WEEKDAY_SATURDAY = 'SA'; |
||
40 | |||
41 | /** |
||
42 | * The frequency of an Event. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $freq = self::FREQ_YEARLY; |
||
47 | |||
48 | /** |
||
49 | * BYSETPOS must require use of other BY*. |
||
50 | * |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $canUseBySetPos = false; |
||
54 | |||
55 | /** |
||
56 | * @var int|null |
||
57 | */ |
||
58 | protected $interval = 1; |
||
59 | |||
60 | /** |
||
61 | * @var int|null |
||
62 | */ |
||
63 | protected $count = null; |
||
64 | |||
65 | /** |
||
66 | * @var \DateTimeInterface|null |
||
67 | */ |
||
68 | protected $until = null; |
||
69 | |||
70 | /** |
||
71 | * @var string|null |
||
72 | */ |
||
73 | protected $wkst; |
||
74 | |||
75 | /** |
||
76 | * @var array|null |
||
77 | */ |
||
78 | protected $bySetPos = null; |
||
79 | |||
80 | /** |
||
81 | * @var string|null |
||
82 | */ |
||
83 | protected $byMonth; |
||
84 | |||
85 | /** |
||
86 | * @var string|null |
||
87 | */ |
||
88 | protected $byWeekNo; |
||
89 | |||
90 | /** |
||
91 | * @var string|null |
||
92 | */ |
||
93 | protected $byYearDay; |
||
94 | |||
95 | /** |
||
96 | * @var string|null |
||
97 | */ |
||
98 | protected $byMonthDay; |
||
99 | |||
100 | /** |
||
101 | * @var string|null |
||
102 | */ |
||
103 | protected $byDay; |
||
104 | |||
105 | /** |
||
106 | * @var string|null |
||
107 | */ |
||
108 | protected $byHour; |
||
109 | |||
110 | /** |
||
111 | * @var string|null |
||
112 | */ |
||
113 | protected $byMinute; |
||
114 | |||
115 | /** |
||
116 | * @var string|null |
||
117 | */ |
||
118 | protected $bySecond; |
||
119 | |||
120 | 11 | public function getEscapedValue(): string |
|
124 | |||
125 | /** |
||
126 | * @return ParameterBag |
||
127 | */ |
||
128 | 11 | protected function buildParameterBag() |
|
188 | |||
189 | /** |
||
190 | * @param int|null $count |
||
191 | * |
||
192 | * @return $this |
||
193 | */ |
||
194 | 1 | public function setCount($count) |
|
200 | |||
201 | /** |
||
202 | * @return int|null |
||
203 | */ |
||
204 | 2 | public function getCount() |
|
208 | |||
209 | /** |
||
210 | * @return $this |
||
211 | */ |
||
212 | 2 | public function setUntil(\DateTimeInterface $until = null) |
|
218 | |||
219 | /** |
||
220 | * @return \DateTimeInterface|null |
||
221 | */ |
||
222 | 1 | public function getUntil() |
|
226 | |||
227 | /** |
||
228 | * The FREQ rule part identifies the type of recurrence rule. This |
||
229 | * rule part MUST be specified in the recurrence rule. Valid values |
||
230 | * include. |
||
231 | * |
||
232 | * SECONDLY, to specify repeating events based on an interval of a second or more; |
||
233 | * MINUTELY, to specify repeating events based on an interval of a minute or more; |
||
234 | * HOURLY, to specify repeating events based on an interval of an hour or more; |
||
235 | * DAILY, to specify repeating events based on an interval of a day or more; |
||
236 | * WEEKLY, to specify repeating events based on an interval of a week or more; |
||
237 | * MONTHLY, to specify repeating events based on an interval of a month or more; |
||
238 | * YEARLY, to specify repeating events based on an interval of a year or more. |
||
239 | * |
||
240 | * @param string $freq |
||
241 | * |
||
242 | * @return $this |
||
243 | * |
||
244 | * @throws \InvalidArgumentException |
||
245 | */ |
||
246 | 4 | public function setFreq($freq) |
|
256 | |||
257 | /** |
||
258 | * @return string |
||
259 | */ |
||
260 | 1 | public function getFreq() |
|
264 | |||
265 | /** |
||
266 | * The INTERVAL rule part contains a positive integer representing at |
||
267 | * which intervals the recurrence rule repeats. |
||
268 | * |
||
269 | * @param int|null $interval |
||
270 | * |
||
271 | * @return $this |
||
272 | */ |
||
273 | 3 | public function setInterval($interval) |
|
279 | |||
280 | /** |
||
281 | * @return int|null |
||
282 | */ |
||
283 | 1 | public function getInterval() |
|
287 | |||
288 | /** |
||
289 | * The WKST rule part specifies the day on which the workweek starts. |
||
290 | * Valid values are MO, TU, WE, TH, FR, SA, and SU. |
||
291 | * |
||
292 | * @param string $value |
||
293 | * |
||
294 | * @return $this |
||
295 | */ |
||
296 | 1 | public function setWkst($value) |
|
302 | |||
303 | /** |
||
304 | * The BYSETPOS filters one interval of events by the specified position. |
||
305 | * A positive position will start from the beginning and go forward while |
||
306 | * a negative position will start at the end and move backward. |
||
307 | * |
||
308 | * Valid values are a comma separated string or an array of integers |
||
309 | * from 1 to 366 or negative integers from -1 to -366. |
||
310 | * |
||
311 | * @param int|string|array|null $value |
||
312 | * |
||
313 | * @throws InvalidArgumentException |
||
314 | * |
||
315 | * @return $this |
||
316 | */ |
||
317 | public function setBySetPos($value) |
||
365 | |||
366 | /** |
||
367 | * The BYMONTH rule part specifies a COMMA-separated list of months of the year. |
||
368 | * Valid values are 1 to 12. |
||
369 | * |
||
370 | * @param int $month |
||
371 | * |
||
372 | * @throws \InvalidArgumentException |
||
373 | * |
||
374 | * @return $this |
||
375 | */ |
||
376 | 6 | public function setByMonth($month) |
|
388 | |||
389 | /** |
||
390 | * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year. |
||
391 | * Valid values are 1 to 53 or -53 to -1. |
||
392 | * |
||
393 | * @param int $value |
||
394 | * |
||
395 | * @throws \InvalidArgumentException |
||
396 | * |
||
397 | * @return $this |
||
398 | */ |
||
399 | 5 | View Code Duplication | public function setByWeekNo($value) |
411 | |||
412 | /** |
||
413 | * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year. |
||
414 | * Valid values are 1 to 366 or -366 to -1. |
||
415 | * |
||
416 | * @param int $day |
||
417 | * |
||
418 | * @throws \InvalidArgumentException |
||
419 | * |
||
420 | * @return $this |
||
421 | */ |
||
422 | 5 | View Code Duplication | public function setByYearDay($day) |
434 | |||
435 | /** |
||
436 | * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month. |
||
437 | * Valid values are 1 to 31 or -31 to -1. |
||
438 | * |
||
439 | * @param int $day |
||
440 | * |
||
441 | * @return $this |
||
442 | * |
||
443 | * @throws \InvalidArgumentException |
||
444 | */ |
||
445 | 5 | View Code Duplication | public function setByMonthDay($day) |
457 | |||
458 | /** |
||
459 | * The BYDAY rule part specifies a COMMA-separated list of days of the week;. |
||
460 | * |
||
461 | * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday; |
||
462 | * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday. |
||
463 | * |
||
464 | * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer. |
||
465 | * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE". |
||
466 | * |
||
467 | * @return $this |
||
468 | */ |
||
469 | 1 | public function setByDay(string $day) |
|
477 | |||
478 | /** |
||
479 | * The BYHOUR rule part specifies a COMMA-separated list of hours of the day. |
||
480 | * Valid values are 0 to 23. |
||
481 | * |
||
482 | * @param int $value |
||
483 | * |
||
484 | * @return $this |
||
485 | * |
||
486 | * @throws \InvalidArgumentException |
||
487 | */ |
||
488 | 4 | public function setByHour($value) |
|
500 | |||
501 | /** |
||
502 | * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour. |
||
503 | * Valid values are 0 to 59. |
||
504 | * |
||
505 | * @param int $value |
||
506 | * |
||
507 | * @return $this |
||
508 | * |
||
509 | * @throws \InvalidArgumentException |
||
510 | */ |
||
511 | 4 | public function setByMinute($value) |
|
523 | |||
524 | /** |
||
525 | * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute. |
||
526 | * Valid values are 0 to 60. |
||
527 | * |
||
528 | * @param int $value |
||
529 | * |
||
530 | * @return $this |
||
531 | * |
||
532 | * @throws \InvalidArgumentException |
||
533 | */ |
||
534 | 4 | public function setBySecond($value) |
|
546 | } |
||
547 |
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.