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|\DateTimeInterface |
||
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 | 11 | public function getEscapedValue(): string |
|
112 | |||
113 | /** |
||
114 | * @return ParameterBag |
||
115 | */ |
||
116 | 11 | protected function buildParameterBag() |
|
172 | |||
173 | /** |
||
174 | * @param int|null $count |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | 1 | public function setCount($count) |
|
184 | |||
185 | /** |
||
186 | * @return int|null |
||
187 | */ |
||
188 | 2 | public function getCount() |
|
192 | |||
193 | /** |
||
194 | * @param \DateTimeInterface|null $until |
||
195 | * |
||
196 | * @return $this |
||
197 | */ |
||
198 | 2 | public function setUntil(\DateTimeInterface $until = null) |
|
204 | |||
205 | /** |
||
206 | * @return \DateTimeInterface|null |
||
207 | */ |
||
208 | 1 | public function getUntil() |
|
212 | |||
213 | /** |
||
214 | * The FREQ rule part identifies the type of recurrence rule. This |
||
215 | * rule part MUST be specified in the recurrence rule. Valid values |
||
216 | * include. |
||
217 | * |
||
218 | * SECONDLY, to specify repeating events based on an interval of a second or more; |
||
219 | * MINUTELY, to specify repeating events based on an interval of a minute or more; |
||
220 | * HOURLY, to specify repeating events based on an interval of an hour or more; |
||
221 | * DAILY, to specify repeating events based on an interval of a day or more; |
||
222 | * WEEKLY, to specify repeating events based on an interval of a week or more; |
||
223 | * MONTHLY, to specify repeating events based on an interval of a month or more; |
||
224 | * YEARLY, to specify repeating events based on an interval of a year or more. |
||
225 | * |
||
226 | * @param string $freq |
||
227 | * |
||
228 | * @return $this |
||
229 | * |
||
230 | * @throws \InvalidArgumentException |
||
231 | */ |
||
232 | 4 | public function setFreq($freq) |
|
242 | |||
243 | /** |
||
244 | * @return string |
||
245 | */ |
||
246 | 1 | public function getFreq() |
|
250 | |||
251 | /** |
||
252 | * The INTERVAL rule part contains a positive integer representing at |
||
253 | * which intervals the recurrence rule repeats. |
||
254 | * |
||
255 | * @param int|null $interval |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | 3 | public function setInterval($interval) |
|
265 | |||
266 | /** |
||
267 | * @return int|null |
||
268 | */ |
||
269 | 1 | public function getInterval() |
|
273 | |||
274 | /** |
||
275 | * The WKST rule part specifies the day on which the workweek starts. |
||
276 | * Valid values are MO, TU, WE, TH, FR, SA, and SU. |
||
277 | * |
||
278 | * @param string $value |
||
279 | * |
||
280 | * @return $this |
||
281 | */ |
||
282 | 1 | public function setWkst($value) |
|
288 | |||
289 | /** |
||
290 | * The BYMONTH rule part specifies a COMMA-separated list of months of the year. |
||
291 | * Valid values are 1 to 12. |
||
292 | * |
||
293 | * @param int $month |
||
294 | * |
||
295 | * @throws \InvalidArgumentException |
||
296 | * |
||
297 | * @return $this |
||
298 | */ |
||
299 | 6 | public function setByMonth($month) |
|
309 | |||
310 | /** |
||
311 | * The BYWEEKNO rule part specifies a COMMA-separated list of ordinals specifying weeks of the year. |
||
312 | * Valid values are 1 to 53 or -53 to -1. |
||
313 | * |
||
314 | * @param int $value |
||
315 | * |
||
316 | * @throws \InvalidArgumentException |
||
317 | * |
||
318 | * @return $this |
||
319 | */ |
||
320 | 5 | View Code Duplication | public function setByWeekNo($value) |
330 | |||
331 | /** |
||
332 | * The BYYEARDAY rule part specifies a COMMA-separated list of days of the year. |
||
333 | * Valid values are 1 to 366 or -366 to -1. |
||
334 | * |
||
335 | * @param int $day |
||
336 | * |
||
337 | * @throws \InvalidArgumentException |
||
338 | * |
||
339 | * @return $this |
||
340 | */ |
||
341 | 5 | View Code Duplication | public function setByYearDay($day) |
351 | |||
352 | /** |
||
353 | * The BYMONTHDAY rule part specifies a COMMA-separated list of days of the month. |
||
354 | * Valid values are 1 to 31 or -31 to -1. |
||
355 | * |
||
356 | * @param int $day |
||
357 | * |
||
358 | * @return $this |
||
359 | * |
||
360 | * @throws \InvalidArgumentException |
||
361 | */ |
||
362 | 5 | View Code Duplication | public function setByMonthDay($day) |
372 | |||
373 | /** |
||
374 | * The BYDAY rule part specifies a COMMA-separated list of days of the week;. |
||
375 | * |
||
376 | * SU indicates Sunday; MO indicates Monday; TU indicates Tuesday; |
||
377 | * WE indicates Wednesday; TH indicates Thursday; FR indicates Friday; and SA indicates Saturday. |
||
378 | * |
||
379 | * Each BYDAY value can also be preceded by a positive (+n) or negative (-n) integer. |
||
380 | * If present, this indicates the nth occurrence of a specific day within the MONTHLY or YEARLY "RRULE". |
||
381 | * |
||
382 | * @param string $day |
||
383 | * |
||
384 | * @return $this |
||
385 | */ |
||
386 | 1 | public function setByDay(string $day) |
|
392 | |||
393 | /** |
||
394 | * The BYHOUR rule part specifies a COMMA-separated list of hours of the day. |
||
395 | * Valid values are 0 to 23. |
||
396 | * |
||
397 | * @param int $value |
||
398 | * |
||
399 | * @return $this |
||
400 | * |
||
401 | * @throws \InvalidArgumentException |
||
402 | */ |
||
403 | 4 | View Code Duplication | public function setByHour($value) |
413 | |||
414 | /** |
||
415 | * The BYMINUTE rule part specifies a COMMA-separated list of minutes within an hour. |
||
416 | * Valid values are 0 to 59. |
||
417 | * |
||
418 | * @param int $value |
||
419 | * |
||
420 | * @return $this |
||
421 | * |
||
422 | * @throws \InvalidArgumentException |
||
423 | */ |
||
424 | 4 | View Code Duplication | public function setByMinute($value) |
434 | |||
435 | /** |
||
436 | * The BYSECOND rule part specifies a COMMA-separated list of seconds within a minute. |
||
437 | * Valid values are 0 to 60. |
||
438 | * |
||
439 | * @param int $value |
||
440 | * |
||
441 | * @return $this |
||
442 | * |
||
443 | * @throws \InvalidArgumentException |
||
444 | */ |
||
445 | 4 | View Code Duplication | public function setBySecond($value) |
455 | } |
||
456 |
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..