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 null|int |
||
| 57 | */ |
||
| 58 | protected $interval = 1; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var null|int |
||
| 62 | */ |
||
| 63 | protected $count = null; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var null|\DateTimeInterface |
||
| 67 | */ |
||
| 68 | protected $until = null; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var null|string |
||
| 72 | */ |
||
| 73 | protected $wkst; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var null|array |
||
| 77 | */ |
||
| 78 | protected $bySetPos = null; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var null|string |
||
| 82 | */ |
||
| 83 | protected $byMonth; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @var null|string |
||
| 87 | */ |
||
| 88 | protected $byWeekNo; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var null|string |
||
| 92 | */ |
||
| 93 | protected $byYearDay; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var null|string |
||
| 97 | */ |
||
| 98 | protected $byMonthDay; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var null|string |
||
| 102 | */ |
||
| 103 | protected $byDay; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var null|string |
||
| 107 | */ |
||
| 108 | protected $byHour; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @var null|string |
||
| 112 | */ |
||
| 113 | protected $byMinute; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @var null|string |
||
| 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 | * @param \DateTimeInterface|null $until |
||
| 211 | * |
||
| 212 | * @return $this |
||
| 213 | */ |
||
| 214 | 2 | public function setUntil(\DateTimeInterface $until = null) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return \DateTimeInterface|null |
||
| 223 | */ |
||
| 224 | 1 | public function getUntil() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * The FREQ rule part identifies the type of recurrence rule. This |
||
| 231 | * rule part MUST be specified in the recurrence rule. Valid values |
||
| 232 | * include. |
||
| 233 | * |
||
| 234 | * SECONDLY, to specify repeating events based on an interval of a second or more; |
||
| 235 | * MINUTELY, to specify repeating events based on an interval of a minute or more; |
||
| 236 | * HOURLY, to specify repeating events based on an interval of an hour or more; |
||
| 237 | * DAILY, to specify repeating events based on an interval of a day or more; |
||
| 238 | * WEEKLY, to specify repeating events based on an interval of a week or more; |
||
| 239 | * MONTHLY, to specify repeating events based on an interval of a month or more; |
||
| 240 | * YEARLY, to specify repeating events based on an interval of a year or more. |
||
| 241 | * |
||
| 242 | * @param string $freq |
||
| 243 | * |
||
| 244 | * @return $this |
||
| 245 | * |
||
| 246 | * @throws \InvalidArgumentException |
||
| 247 | */ |
||
| 248 | 4 | public function setFreq($freq) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 1 | public function getFreq() |
|
| 266 | |||
| 267 | /** |
||
| 268 | * The INTERVAL rule part contains a positive integer representing at |
||
| 269 | * which intervals the recurrence rule repeats. |
||
| 270 | * |
||
| 271 | * @param int|null $interval |
||
| 272 | * |
||
| 273 | * @return $this |
||
| 274 | */ |
||
| 275 | 3 | public function setInterval($interval) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return int|null |
||
| 284 | */ |
||
| 285 | 1 | public function getInterval() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * The WKST rule part specifies the day on which the workweek starts. |
||
| 292 | * Valid values are MO, TU, WE, TH, FR, SA, and SU. |
||
| 293 | * |
||
| 294 | * @param string $value |
||
| 295 | * |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | 1 | public function setWkst($value) |
|
| 304 | |||
| 305 | /** |
||
| 306 | * The BYSETPOS filters one interval of events by the specified position. |
||
| 307 | * A positive position will start from the beginning and go forward while |
||
| 308 | * a negative position will start at the end and move backward. |
||
| 309 | * |
||
| 310 | * Valid values are a comma separated string or an array of integers |
||
| 311 | * from 1 to 366 or negative integers from -1 to -366. |
||
| 312 | * |
||
| 313 | * @param null|int|string|array $value |
||
| 314 | * |
||
| 315 | * @throws InvalidArgumentException |
||
| 316 | * |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function setBySetPos($value) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * The BYMONTH rule part specifies a COMMA-separated list of months of the year. |
||
| 370 | * Valid values are 1 to 12. |
||
| 371 | * |
||
| 372 | * @param int $month |
||
| 373 | * |
||
| 374 | * @throws \InvalidArgumentException |
||
| 375 | * |
||
| 376 | * @return $this |
||
| 377 | */ |
||
| 378 | 6 | public function setByMonth($month) |
|
| 390 | |||
| 391 | /** |
||
| 392 | * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year. |
||
| 393 | * Valid values are 1 to 53 or -53 to -1. |
||
| 394 | * |
||
| 395 | * @param int $value |
||
| 396 | * |
||
| 397 | * @throws \InvalidArgumentException |
||
| 398 | * |
||
| 399 | * @return $this |
||
| 400 | */ |
||
| 401 | 5 | View Code Duplication | public function setByWeekNo($value) |
| 413 | |||
| 414 | /** |
||
| 415 | * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year. |
||
| 416 | * Valid values are 1 to 366 or -366 to -1. |
||
| 417 | * |
||
| 418 | * @param int $day |
||
| 419 | * |
||
| 420 | * @throws \InvalidArgumentException |
||
| 421 | * |
||
| 422 | * @return $this |
||
| 423 | */ |
||
| 424 | 5 | View Code Duplication | public function setByYearDay($day) |
| 436 | |||
| 437 | /** |
||
| 438 | * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month. |
||
| 439 | * Valid values are 1 to 31 or -31 to -1. |
||
| 440 | * |
||
| 441 | * @param int $day |
||
| 442 | * |
||
| 443 | * @return $this |
||
| 444 | * |
||
| 445 | * @throws \InvalidArgumentException |
||
| 446 | */ |
||
| 447 | 5 | View Code Duplication | public function setByMonthDay($day) |
| 459 | |||
| 460 | /** |
||
| 461 | * The BYDAY rule part specifies a COMMA-separated list of days of the week;. |
||
| 462 | * |
||
| 463 | * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday; |
||
| 464 | * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday. |
||
| 465 | * |
||
| 466 | * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer. |
||
| 467 | * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE". |
||
| 468 | * |
||
| 469 | * @param string $day |
||
| 470 | * |
||
| 471 | * @return $this |
||
| 472 | */ |
||
| 473 | 1 | public function setByDay(string $day) |
|
| 481 | |||
| 482 | /** |
||
| 483 | * The BYHOUR rule part specifies a COMMA-separated list of hours of the day. |
||
| 484 | * Valid values are 0 to 23. |
||
| 485 | * |
||
| 486 | * @param int $value |
||
| 487 | * |
||
| 488 | * @return $this |
||
| 489 | * |
||
| 490 | * @throws \InvalidArgumentException |
||
| 491 | */ |
||
| 492 | 4 | public function setByHour($value) |
|
| 504 | |||
| 505 | /** |
||
| 506 | * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour. |
||
| 507 | * Valid values are 0 to 59. |
||
| 508 | * |
||
| 509 | * @param int $value |
||
| 510 | * |
||
| 511 | * @return $this |
||
| 512 | * |
||
| 513 | * @throws \InvalidArgumentException |
||
| 514 | */ |
||
| 515 | 4 | public function setByMinute($value) |
|
| 527 | |||
| 528 | /** |
||
| 529 | * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute. |
||
| 530 | * Valid values are 0 to 60. |
||
| 531 | * |
||
| 532 | * @param int $value |
||
| 533 | * |
||
| 534 | * @return $this |
||
| 535 | * |
||
| 536 | * @throws \InvalidArgumentException |
||
| 537 | */ |
||
| 538 | 4 | public function setBySecond($value) |
|
| 550 | } |
||
| 551 |
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.