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 |
||
| 14 | class RecurrenceRule implements ValueInterface |
||
| 15 | { |
||
| 16 | const FREQ_YEARLY = 'YEARLY'; |
||
| 17 | const FREQ_MONTHLY = 'MONTHLY'; |
||
| 18 | const FREQ_WEEKLY = 'WEEKLY'; |
||
| 19 | const FREQ_DAILY = 'DAILY'; |
||
| 20 | |||
| 21 | const WEEKDAY_SUNDAY = 'SU'; |
||
| 22 | const WEEKDAY_MONDAY = 'MO'; |
||
| 23 | const WEEKDAY_TUESDAY = 'TU'; |
||
| 24 | const WEEKDAY_WEDNESDAY = 'WE'; |
||
| 25 | const WEEKDAY_THURSDAY = 'TH'; |
||
| 26 | const WEEKDAY_FRIDAY = 'FR'; |
||
| 27 | const WEEKDAY_SATURDAY = 'SA'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The frequency of an Event. |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $freq = self::FREQ_YEARLY; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var null|int |
||
| 38 | */ |
||
| 39 | protected $interval = 1; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var null|int |
||
| 43 | */ |
||
| 44 | protected $count = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var null|\DateTime |
||
| 48 | */ |
||
| 49 | protected $until = null; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var null|string |
||
| 53 | */ |
||
| 54 | protected $wkst; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var null|string |
||
| 58 | */ |
||
| 59 | protected $byMonth; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var null|string |
||
| 63 | */ |
||
| 64 | protected $byWeekNo; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var null|string |
||
| 68 | */ |
||
| 69 | protected $byYearDay; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var null|string |
||
| 73 | */ |
||
| 74 | protected $byMonthDay; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var null|string |
||
| 78 | */ |
||
| 79 | protected $byDay; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var null|string |
||
| 83 | */ |
||
| 84 | protected $byHour; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var null|string |
||
| 88 | */ |
||
| 89 | protected $byMinute; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var null|string |
||
| 93 | */ |
||
| 94 | protected $bySecond; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Return the value of the Property as an escaped string. |
||
| 98 | * |
||
| 99 | * Escape values as per RFC 2445. See http://www.kanzaki.com/docs/ical/text.html |
||
| 100 | * |
||
| 101 | * @return string |
||
| 102 | */ |
||
| 103 | 1 | public function getEscapedValue() |
|
| 107 | |||
| 108 | /** |
||
| 109 | * @return ParameterBag |
||
| 110 | */ |
||
| 111 | 1 | protected function buildParameterBag() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @param int|null $count |
||
| 170 | * |
||
| 171 | * @return $this |
||
| 172 | */ |
||
| 173 | public function setCount($count) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @return int|null |
||
| 182 | */ |
||
| 183 | public function getCount() |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param \DateTime|null $until |
||
| 190 | * |
||
| 191 | * @return $this |
||
| 192 | */ |
||
| 193 | 1 | public function setUntil(\DateTime $until = null) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @return \DateTime|null |
||
| 202 | */ |
||
| 203 | public function getUntil() |
||
| 207 | |||
| 208 | /** |
||
| 209 | * The FREQ rule part identifies the type of recurrence rule. This |
||
| 210 | * rule part MUST be specified in the recurrence rule. Valid values |
||
| 211 | * include. |
||
| 212 | * |
||
| 213 | * SECONDLY, to specify repeating events based on an interval of a second or more; |
||
| 214 | * MINUTELY, to specify repeating events based on an interval of a minute or more; |
||
| 215 | * HOURLY, to specify repeating events based on an interval of an hour or more; |
||
| 216 | * DAILY, to specify repeating events based on an interval of a day or more; |
||
| 217 | * WEEKLY, to specify repeating events based on an interval of a week or more; |
||
| 218 | * MONTHLY, to specify repeating events based on an interval of a month or more; |
||
| 219 | * YEARLY, to specify repeating events based on an interval of a year or more. |
||
| 220 | * |
||
| 221 | * @param string $freq |
||
| 222 | * |
||
| 223 | * @return $this |
||
| 224 | * |
||
| 225 | * @throws \InvalidArgumentException |
||
| 226 | */ |
||
| 227 | 1 | public function setFreq($freq) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | public function getFreq() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * The INTERVAL rule part contains a positive integer representing at |
||
| 251 | * which intervals the recurrence rule repeats. |
||
| 252 | * |
||
| 253 | * @param int|null $interval |
||
| 254 | * |
||
| 255 | * @return $this |
||
| 256 | */ |
||
| 257 | 1 | public function setInterval($interval) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return int|null |
||
| 266 | */ |
||
| 267 | public function getInterval() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * The WKST rule part specifies the day on which the workweek starts. |
||
| 274 | * Valid values are MO, TU, WE, TH, FR, SA, and SU. |
||
| 275 | * |
||
| 276 | * @param string $value |
||
| 277 | * |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function setWkst($value) |
||
| 286 | |||
| 287 | /** |
||
| 288 | * The BYMONTH rule part specifies a COMMA-separated list of months of the year. |
||
| 289 | * Valid values are 1 to 12. |
||
| 290 | * |
||
| 291 | * @param int $month |
||
| 292 | * |
||
| 293 | * @throws InvalidArgumentException |
||
| 294 | * |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function setByMonth($month) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year. |
||
| 310 | * Valid values are 1 to 53 or -53 to -1. |
||
| 311 | * |
||
| 312 | * @param int $value |
||
| 313 | * |
||
| 314 | * @return $this |
||
| 315 | */ |
||
| 316 | public function setByWeekNo($value) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year. |
||
| 325 | * Valid values are 1 to 366 or -366 to -1. |
||
| 326 | * |
||
| 327 | * @param int $day |
||
| 328 | * |
||
| 329 | * @return $this |
||
| 330 | */ |
||
| 331 | public function setByYearDay($day) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month. |
||
| 340 | * Valid values are 1 to 31 or -31 to -1. |
||
| 341 | * |
||
| 342 | * @param int $day |
||
| 343 | * |
||
| 344 | * @return $this |
||
| 345 | */ |
||
| 346 | public function setByMonthDay($day) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * The BYDAY rule part specifies a COMMA-separated list of days of the week;. |
||
| 355 | * |
||
| 356 | * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday; |
||
| 357 | * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday. |
||
| 358 | * |
||
| 359 | * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer. |
||
| 360 | * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE". |
||
| 361 | * |
||
| 362 | * @param string $day |
||
| 363 | * |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function setByDay($day) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * The BYHOUR rule part specifies a COMMA-separated list of hours of the day. |
||
| 375 | * Valid values are 0 to 23. |
||
| 376 | * |
||
| 377 | * @param int $value |
||
| 378 | * |
||
| 379 | * @return $this |
||
| 380 | * |
||
| 381 | * @throws \InvalidArgumentException |
||
| 382 | */ |
||
| 383 | View Code Duplication | public function setByHour($value) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour. |
||
| 396 | * Valid values are 0 to 59. |
||
| 397 | * |
||
| 398 | * @param int $value |
||
| 399 | * |
||
| 400 | * @return $this |
||
| 401 | * |
||
| 402 | * @throws \InvalidArgumentException |
||
| 403 | */ |
||
| 404 | View Code Duplication | public function setByMinute($value) |
|
| 414 | |||
| 415 | /** |
||
| 416 | * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute. |
||
| 417 | * Valid values are 0 to 60. |
||
| 418 | * |
||
| 419 | * @param int $value |
||
| 420 | * |
||
| 421 | * @return $this |
||
| 422 | * |
||
| 423 | * @throws \InvalidArgumentException |
||
| 424 | */ |
||
| 425 | View Code Duplication | public function setBySecond($value) |
|
| 435 | } |
||
| 436 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..