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 | * @var null|int |
||
| 50 | */ |
||
| 51 | protected $interval = 1; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var null|int |
||
| 55 | */ |
||
| 56 | protected $count = null; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var null|\DateTime |
||
| 60 | */ |
||
| 61 | protected $until = null; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var null|string |
||
| 65 | */ |
||
| 66 | protected $wkst; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var null|string |
||
| 70 | */ |
||
| 71 | protected $byMonth; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var null|string |
||
| 75 | */ |
||
| 76 | protected $byWeekNo; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var null|string |
||
| 80 | */ |
||
| 81 | protected $byYearDay; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var null|string |
||
| 85 | */ |
||
| 86 | protected $byMonthDay; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var null|string |
||
| 90 | */ |
||
| 91 | protected $byDay; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var null|string |
||
| 95 | */ |
||
| 96 | protected $byHour; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var null|string |
||
| 100 | */ |
||
| 101 | protected $byMinute; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var null|string |
||
| 105 | */ |
||
| 106 | protected $bySecond; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Return the value of the Property as an escaped string. |
||
| 110 | * |
||
| 111 | * Escape values as per RFC 2445. See http://www.kanzaki.com/docs/ical/text.html |
||
| 112 | * |
||
| 113 | * @return string |
||
| 114 | */ |
||
| 115 | 1 | public function getEscapedValue() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @return ParameterBag |
||
| 122 | */ |
||
| 123 | 1 | protected function buildParameterBag() |
|
| 124 | { |
||
| 125 | 1 | $parameterBag = new ParameterBag(); |
|
| 126 | |||
| 127 | 1 | $parameterBag->setParam('FREQ', $this->freq); |
|
| 128 | |||
| 129 | 1 | if (null !== $this->interval) { |
|
| 130 | $parameterBag->setParam('INTERVAL', $this->interval); |
||
| 131 | } |
||
| 132 | |||
| 133 | 1 | if (null !== $this->count) { |
|
| 134 | $parameterBag->setParam('COUNT', $this->count); |
||
| 135 | } |
||
| 136 | |||
| 137 | 1 | if (null != $this->until) { |
|
| 138 | 1 | $parameterBag->setParam('UNTIL', $this->until->format('Ymd\THis\Z')); |
|
| 139 | } |
||
| 140 | |||
| 141 | 1 | if (null !== $this->wkst) { |
|
| 142 | $parameterBag->setParam('WKST', $this->wkst); |
||
| 143 | } |
||
| 144 | |||
| 145 | 1 | if (null !== $this->byMonth) { |
|
| 146 | $parameterBag->setParam('BYMONTH', $this->byMonth); |
||
| 147 | } |
||
| 148 | |||
| 149 | 1 | if (null !== $this->byWeekNo) { |
|
| 150 | $parameterBag->setParam('BYWEEKNO', $this->byWeekNo); |
||
| 151 | } |
||
| 152 | |||
| 153 | 1 | if (null !== $this->byYearDay) { |
|
| 154 | $parameterBag->setParam('BYYEARDAY', $this->byYearDay); |
||
| 155 | } |
||
| 156 | |||
| 157 | 1 | if (null !== $this->byMonthDay) { |
|
| 158 | $parameterBag->setParam('BYMONTHDAY', $this->byMonthDay); |
||
| 159 | } |
||
| 160 | |||
| 161 | 1 | if (null !== $this->byDay) { |
|
| 162 | $parameterBag->setParam('BYDAY', $this->byDay); |
||
| 163 | } |
||
| 164 | |||
| 165 | 1 | if (null !== $this->byHour) { |
|
| 166 | $parameterBag->setParam('BYHOUR', $this->byHour); |
||
| 167 | } |
||
| 168 | |||
| 169 | 1 | if (null !== $this->byMinute) { |
|
| 170 | $parameterBag->setParam('BYMINUTE', $this->byMinute); |
||
| 171 | } |
||
| 172 | |||
| 173 | 1 | if (null !== $this->bySecond) { |
|
| 174 | $parameterBag->setParam('BYSECOND', $this->bySecond); |
||
| 175 | } |
||
| 176 | |||
| 177 | 1 | return $parameterBag; |
|
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param int|null $count |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function setCount($count) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @return int|null |
||
| 194 | */ |
||
| 195 | public function getCount() |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param \DateTime|null $until |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | 1 | public function setUntil(\DateTime $until = null) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @return \DateTime|null |
||
| 214 | */ |
||
| 215 | public function getUntil() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * The FREQ rule part identifies the type of recurrence rule. This |
||
| 222 | * rule part MUST be specified in the recurrence rule. Valid values |
||
| 223 | * include. |
||
| 224 | * |
||
| 225 | * SECONDLY, to specify repeating events based on an interval of a second or more; |
||
| 226 | * MINUTELY, to specify repeating events based on an interval of a minute or more; |
||
| 227 | * HOURLY, to specify repeating events based on an interval of an hour or more; |
||
| 228 | * DAILY, to specify repeating events based on an interval of a day or more; |
||
| 229 | * WEEKLY, to specify repeating events based on an interval of a week or more; |
||
| 230 | * MONTHLY, to specify repeating events based on an interval of a month or more; |
||
| 231 | * YEARLY, to specify repeating events based on an interval of a year or more. |
||
| 232 | * |
||
| 233 | * @param string $freq |
||
| 234 | * |
||
| 235 | * @return $this |
||
| 236 | * |
||
| 237 | * @throws \InvalidArgumentException |
||
| 238 | */ |
||
| 239 | 1 | public function setFreq($freq) |
|
| 240 | { |
||
| 241 | 1 | if (@constant('static::FREQ_' . $freq) !== null) { |
|
| 242 | 1 | $this->freq = $freq; |
|
| 243 | } else { |
||
| 244 | throw new \InvalidArgumentException("The Frequency {$freq} is not supported."); |
||
| 245 | } |
||
| 246 | |||
| 247 | 1 | return $this; |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function getFreq() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * The INTERVAL rule part contains a positive integer representing at |
||
| 260 | * which intervals the recurrence rule repeats. |
||
| 261 | * |
||
| 262 | * @param int|null $interval |
||
| 263 | * |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | 1 | public function setInterval($interval) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @return int|null |
||
| 275 | */ |
||
| 276 | public function getInterval() |
||
| 280 | |||
| 281 | /** |
||
| 282 | * The WKST rule part specifies the day on which the workweek starts. |
||
| 283 | * Valid values are MO, TU, WE, TH, FR, SA, and SU. |
||
| 284 | * |
||
| 285 | * @param string $value |
||
| 286 | * |
||
| 287 | * @return $this |
||
| 288 | */ |
||
| 289 | public function setWkst($value) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * The BYMONTH rule part specifies a COMMA-separated list of months of the year. |
||
| 298 | * Valid values are 1 to 12. |
||
| 299 | * |
||
| 300 | * @param int $month |
||
| 301 | * |
||
| 302 | * @throws InvalidArgumentException |
||
| 303 | * |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function setByMonth($month) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year. |
||
| 319 | * Valid values are 1 to 53 or -53 to -1. |
||
| 320 | * |
||
| 321 | * @param int $value |
||
| 322 | * |
||
| 323 | * @return $this |
||
| 324 | */ |
||
| 325 | public function setByWeekNo($value) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year. |
||
| 334 | * Valid values are 1 to 366 or -366 to -1. |
||
| 335 | * |
||
| 336 | * @param int $day |
||
| 337 | * |
||
| 338 | * @return $this |
||
| 339 | */ |
||
| 340 | public function setByYearDay($day) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month. |
||
| 349 | * Valid values are 1 to 31 or -31 to -1. |
||
| 350 | * |
||
| 351 | * @param int $day |
||
| 352 | * |
||
| 353 | * @return $this |
||
| 354 | */ |
||
| 355 | public function setByMonthDay($day) |
||
| 361 | |||
| 362 | /** |
||
| 363 | * The BYDAY rule part specifies a COMMA-separated list of days of the week;. |
||
| 364 | * |
||
| 365 | * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday; |
||
| 366 | * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday. |
||
| 367 | * |
||
| 368 | * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer. |
||
| 369 | * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE". |
||
| 370 | * |
||
| 371 | * @param string $day |
||
| 372 | * |
||
| 373 | * @return $this |
||
| 374 | */ |
||
| 375 | public function setByDay($day) |
||
| 381 | |||
| 382 | /** |
||
| 383 | * The BYHOUR rule part specifies a COMMA-separated list of hours of the day. |
||
| 384 | * Valid values are 0 to 23. |
||
| 385 | * |
||
| 386 | * @param int $value |
||
| 387 | * |
||
| 388 | * @return $this |
||
| 389 | * |
||
| 390 | * @throws \InvalidArgumentException |
||
| 391 | */ |
||
| 392 | View Code Duplication | public function setByHour($value) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour. |
||
| 405 | * Valid values are 0 to 59. |
||
| 406 | * |
||
| 407 | * @param int $value |
||
| 408 | * |
||
| 409 | * @return $this |
||
| 410 | * |
||
| 411 | * @throws \InvalidArgumentException |
||
| 412 | */ |
||
| 413 | View Code Duplication | public function setByMinute($value) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute. |
||
| 426 | * Valid values are 0 to 60. |
||
| 427 | * |
||
| 428 | * @param int $value |
||
| 429 | * |
||
| 430 | * @return $this |
||
| 431 | * |
||
| 432 | * @throws \InvalidArgumentException |
||
| 433 | */ |
||
| 434 | View Code Duplication | public function setBySecond($value) |
|
| 444 | } |
||
| 445 |
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..