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 | |||
30 | const WEEKDAY_SUNDAY = 'SU'; |
||
31 | const WEEKDAY_MONDAY = 'MO'; |
||
32 | const WEEKDAY_TUESDAY = 'TU'; |
||
33 | const WEEKDAY_WEDNESDAY = 'WE'; |
||
34 | const WEEKDAY_THURSDAY = 'TH'; |
||
35 | const WEEKDAY_FRIDAY = 'FR'; |
||
36 | const WEEKDAY_SATURDAY = 'SA'; |
||
37 | |||
38 | /** |
||
39 | * The frequency of an Event. |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $freq = self::FREQ_YEARLY; |
||
44 | |||
45 | /** |
||
46 | * @var null|int |
||
47 | */ |
||
48 | protected $interval = 1; |
||
49 | |||
50 | /** |
||
51 | * @var null|int |
||
52 | */ |
||
53 | protected $count = null; |
||
54 | |||
55 | /** |
||
56 | * @var null|\DateTime |
||
57 | */ |
||
58 | protected $until = null; |
||
59 | |||
60 | /** |
||
61 | * @var null|string |
||
62 | */ |
||
63 | protected $wkst; |
||
64 | |||
65 | /** |
||
66 | * @var null|string |
||
67 | */ |
||
68 | protected $byMonth; |
||
69 | |||
70 | /** |
||
71 | * @var null|string |
||
72 | */ |
||
73 | protected $byWeekNo; |
||
74 | |||
75 | /** |
||
76 | * @var null|string |
||
77 | */ |
||
78 | protected $byYearDay; |
||
79 | |||
80 | /** |
||
81 | * @var null|string |
||
82 | */ |
||
83 | protected $byMonthDay; |
||
84 | |||
85 | /** |
||
86 | * @var null|string |
||
87 | */ |
||
88 | protected $byDay; |
||
89 | |||
90 | /** |
||
91 | * @var null|string |
||
92 | */ |
||
93 | protected $byHour; |
||
94 | |||
95 | /** |
||
96 | * @var null|string |
||
97 | */ |
||
98 | protected $byMinute; |
||
99 | |||
100 | /** |
||
101 | * @var null|string |
||
102 | */ |
||
103 | protected $bySecond; |
||
104 | |||
105 | /** |
||
106 | * Return the value of the Property as an escaped string. |
||
107 | * |
||
108 | * Escape values as per RFC 2445. See http://www.kanzaki.com/docs/ical/text.html |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 1 | public function getEscapedValue() |
|
116 | |||
117 | /** |
||
118 | * @return ParameterBag |
||
119 | */ |
||
120 | 1 | protected function buildParameterBag() |
|
121 | { |
||
122 | 1 | $parameterBag = new ParameterBag(); |
|
123 | |||
124 | 1 | $parameterBag->setParam('FREQ', $this->freq); |
|
125 | |||
126 | 1 | if (null !== $this->interval) { |
|
127 | $parameterBag->setParam('INTERVAL', $this->interval); |
||
128 | } |
||
129 | |||
130 | 1 | if (null !== $this->count) { |
|
131 | $parameterBag->setParam('COUNT', $this->count); |
||
132 | } |
||
133 | |||
134 | 1 | if (null != $this->until) { |
|
135 | 1 | $parameterBag->setParam('UNTIL', $this->until->format('Ymd\THis\Z')); |
|
136 | } |
||
137 | |||
138 | 1 | if (null !== $this->wkst) { |
|
139 | $parameterBag->setParam('WKST', $this->wkst); |
||
140 | } |
||
141 | |||
142 | 1 | if (null !== $this->byMonth) { |
|
143 | $parameterBag->setParam('BYMONTH', $this->byMonth); |
||
144 | } |
||
145 | |||
146 | 1 | if (null !== $this->byWeekNo) { |
|
147 | $parameterBag->setParam('BYWEEKNO', $this->byWeekNo); |
||
148 | } |
||
149 | |||
150 | 1 | if (null !== $this->byYearDay) { |
|
151 | $parameterBag->setParam('BYYEARDAY', $this->byYearDay); |
||
152 | } |
||
153 | |||
154 | 1 | if (null !== $this->byMonthDay) { |
|
155 | $parameterBag->setParam('BYMONTHDAY', $this->byMonthDay); |
||
156 | } |
||
157 | |||
158 | 1 | if (null !== $this->byDay) { |
|
159 | $parameterBag->setParam('BYDAY', $this->byDay); |
||
160 | } |
||
161 | |||
162 | 1 | if (null !== $this->byHour) { |
|
163 | $parameterBag->setParam('BYHOUR', $this->byHour); |
||
164 | } |
||
165 | |||
166 | 1 | if (null !== $this->byMinute) { |
|
167 | $parameterBag->setParam('BYMINUTE', $this->byMinute); |
||
168 | } |
||
169 | |||
170 | 1 | if (null !== $this->bySecond) { |
|
171 | $parameterBag->setParam('BYSECOND', $this->bySecond); |
||
172 | } |
||
173 | |||
174 | 1 | return $parameterBag; |
|
175 | } |
||
176 | |||
177 | /** |
||
178 | * @param int|null $count |
||
179 | * |
||
180 | * @return $this |
||
181 | */ |
||
182 | public function setCount($count) |
||
188 | |||
189 | /** |
||
190 | * @return int|null |
||
191 | */ |
||
192 | public function getCount() |
||
196 | |||
197 | /** |
||
198 | * @param \DateTime|null $until |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | 1 | public function setUntil(\DateTime $until = null) |
|
208 | |||
209 | /** |
||
210 | * @return \DateTime|null |
||
211 | */ |
||
212 | public function getUntil() |
||
216 | |||
217 | /** |
||
218 | * The FREQ rule part identifies the type of recurrence rule. This |
||
219 | * rule part MUST be specified in the recurrence rule. Valid values |
||
220 | * include. |
||
221 | * |
||
222 | * SECONDLY, to specify repeating events based on an interval of a second or more; |
||
223 | * MINUTELY, to specify repeating events based on an interval of a minute or more; |
||
224 | * HOURLY, to specify repeating events based on an interval of an hour or more; |
||
225 | * DAILY, to specify repeating events based on an interval of a day or more; |
||
226 | * WEEKLY, to specify repeating events based on an interval of a week or more; |
||
227 | * MONTHLY, to specify repeating events based on an interval of a month or more; |
||
228 | * YEARLY, to specify repeating events based on an interval of a year or more. |
||
229 | * |
||
230 | * @param string $freq |
||
231 | * |
||
232 | * @return $this |
||
233 | * |
||
234 | * @throws \InvalidArgumentException |
||
235 | */ |
||
236 | 1 | public function setFreq($freq) |
|
237 | { |
||
238 | 1 | if (self::FREQ_YEARLY === $freq || self::FREQ_MONTHLY === $freq |
|
239 | 1 | || self::FREQ_WEEKLY === $freq |
|
240 | 1 | || self::FREQ_DAILY === $freq |
|
241 | ) { |
||
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..