Total Complexity | 44 |
Total Lines | 459 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Event 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.
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 Event, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class Event |
||
6 | { |
||
7 | /** |
||
8 | * @var int |
||
9 | */ |
||
10 | protected $id; |
||
11 | |||
12 | /** |
||
13 | * @var string |
||
14 | */ |
||
15 | protected $title; |
||
16 | |||
17 | /** |
||
18 | * @var bool |
||
19 | */ |
||
20 | protected $allDay = true; |
||
21 | |||
22 | /** |
||
23 | * @var \DateTimeInterface |
||
24 | */ |
||
25 | protected $startDate; |
||
26 | |||
27 | /** |
||
28 | * @var \DateTimeInterface |
||
29 | */ |
||
30 | protected $endDate = null; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $url; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $className; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $editable = false; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $startEditable = false; |
||
51 | |||
52 | /** |
||
53 | * @var bool |
||
54 | */ |
||
55 | protected $durationEditable = false; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $rendering; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | protected $overlap = true; |
||
66 | |||
67 | /** |
||
68 | * @var int |
||
69 | */ |
||
70 | protected $constraint; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $source; |
||
76 | |||
77 | /** |
||
78 | * @var string |
||
79 | */ |
||
80 | protected $color; |
||
81 | |||
82 | /** |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $backgroundColor; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | protected $textColor; |
||
91 | |||
92 | /** |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $customFields = []; |
||
96 | |||
97 | /** |
||
98 | * @param string $title |
||
99 | * @param \DateTimeInterface $start |
||
100 | * @param \DateTimeInterface $end |
||
101 | */ |
||
102 | public function __construct( |
||
103 | ?string $title, |
||
104 | \DateTimeInterface $start, |
||
105 | ?\DateTimeInterface $end = null |
||
106 | ) { |
||
107 | $this->setTitle($title); |
||
108 | $this->setStartDate($start); |
||
109 | $this->setEndDate($end); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return int |
||
114 | */ |
||
115 | public function getId(): ?int |
||
116 | { |
||
117 | return $this->id; |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param int $id |
||
122 | */ |
||
123 | public function setId(?int $id): void |
||
124 | { |
||
125 | $this->id = $id; |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @return string |
||
130 | */ |
||
131 | public function getTitle(): ?string |
||
132 | { |
||
133 | return $this->title; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * @param string $title |
||
138 | */ |
||
139 | public function setTitle(?string $title): void |
||
140 | { |
||
141 | $this->title = $title; |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * @return bool |
||
146 | */ |
||
147 | public function isAllDay(): bool |
||
148 | { |
||
149 | return $this->allDay; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param bool $allDay |
||
154 | */ |
||
155 | public function setAllDay(bool $allDay): void |
||
156 | { |
||
157 | $this->allDay = $allDay; |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @return \DateTimeInterface |
||
162 | */ |
||
163 | public function getStartDate(): ?\DateTimeInterface |
||
164 | { |
||
165 | return $this->startDate; |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param \DateTimeInterface $startDate |
||
170 | */ |
||
171 | public function setStartDate(?\DateTimeInterface $startDate): void |
||
172 | { |
||
173 | $this->startDate = $startDate; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return \DateTimeInterface |
||
178 | */ |
||
179 | public function getEndDate(): ?\DateTimeInterface |
||
180 | { |
||
181 | return $this->endDate; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param \DateTimeInterface $endDate |
||
186 | */ |
||
187 | public function setEndDate(?\DateTimeInterface $endDate = null): void |
||
188 | { |
||
189 | if (null !== $endDate) { |
||
190 | $this->allDay = false; |
||
191 | } |
||
192 | $this->endDate = $endDate; |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * @return string |
||
197 | */ |
||
198 | public function getUrl(): ?string |
||
199 | { |
||
200 | return $this->url; |
||
201 | } |
||
202 | |||
203 | /** |
||
204 | * @param string $url |
||
205 | */ |
||
206 | public function setUrl(?string $url): void |
||
207 | { |
||
208 | $this->url = $url; |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * @return string |
||
213 | */ |
||
214 | public function getClassName(): ?string |
||
215 | { |
||
216 | return $this->className; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @param string $className |
||
221 | */ |
||
222 | public function setClassName(?string $className): void |
||
223 | { |
||
224 | $this->className = $className; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return bool |
||
229 | */ |
||
230 | public function isEditable(): bool |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @param bool $editable |
||
237 | */ |
||
238 | public function setEditable(bool $editable): void |
||
239 | { |
||
240 | $this->editable = $editable; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return bool |
||
245 | */ |
||
246 | public function isStartEditable(): bool |
||
247 | { |
||
248 | return $this->startEditable; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param bool $startEditable |
||
253 | */ |
||
254 | public function setStartEditable(bool $startEditable): void |
||
255 | { |
||
256 | $this->startEditable = $startEditable; |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @return bool |
||
261 | */ |
||
262 | public function isDurationEditable(): bool |
||
263 | { |
||
264 | return $this->durationEditable; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param bool $durationEditable |
||
269 | */ |
||
270 | public function setDurationEditable(bool $durationEditable): void |
||
271 | { |
||
272 | $this->durationEditable = $durationEditable; |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getRendering(): ?string |
||
279 | { |
||
280 | return $this->rendering; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param string $rendering |
||
285 | */ |
||
286 | public function setRendering(?string $rendering): void |
||
287 | { |
||
288 | $this->rendering = $rendering; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @return bool |
||
293 | */ |
||
294 | public function isOverlap(): bool |
||
295 | { |
||
296 | return $this->overlap; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @param bool $overlap |
||
301 | */ |
||
302 | public function setOverlap(bool $overlap): void |
||
303 | { |
||
304 | $this->overlap = $overlap; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * @return int |
||
309 | */ |
||
310 | public function getConstraint(): ?int |
||
311 | { |
||
312 | return $this->constraint; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * @param int $constraint |
||
317 | */ |
||
318 | public function setConstraint($constraint): void |
||
319 | { |
||
320 | $this->constraint = $constraint; |
||
321 | } |
||
322 | |||
323 | /** |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getSource(): ?string |
||
327 | { |
||
328 | return $this->source; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * @param string $source |
||
333 | */ |
||
334 | public function setSource(?string $source): void |
||
335 | { |
||
336 | $this->source = $source; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @return string |
||
341 | */ |
||
342 | public function getColor(): ?string |
||
343 | { |
||
344 | return $this->color; |
||
345 | } |
||
346 | |||
347 | /** |
||
348 | * @param string $color |
||
349 | */ |
||
350 | public function setColor(?string $color): void |
||
351 | { |
||
352 | $this->color = $color; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @return string |
||
357 | */ |
||
358 | public function getBackgroundColor(): ?string |
||
359 | { |
||
360 | return $this->backgroundColor; |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * @param string $backgroundColor |
||
365 | */ |
||
366 | public function setBackgroundColor(?string $backgroundColor): void |
||
367 | { |
||
368 | $this->backgroundColor = $backgroundColor; |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return string |
||
373 | */ |
||
374 | public function getTextColor(): ?string |
||
375 | { |
||
376 | return $this->textColor; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param string $textColor |
||
381 | */ |
||
382 | public function setTextColor(?string $textColor): void |
||
383 | { |
||
384 | $this->textColor = $textColor; |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * @param $name |
||
389 | * @param $value |
||
390 | * |
||
391 | * @return mixed |
||
392 | */ |
||
393 | public function setCustomField($name, $value): void |
||
394 | { |
||
395 | $this->customFields[$name] = $value; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * @param $name |
||
400 | * |
||
401 | * @return mixed |
||
402 | */ |
||
403 | public function getCustomFieldValue($name) |
||
404 | { |
||
405 | return $this->customFields[$name]; |
||
406 | } |
||
407 | |||
408 | /** |
||
409 | * @return array |
||
410 | */ |
||
411 | public function getCustomFields(): array |
||
412 | { |
||
413 | return $this->customFields; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param $name |
||
418 | * |
||
419 | * @return mixed |
||
420 | */ |
||
421 | public function removeCustomField($name) |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @return array |
||
435 | */ |
||
436 | public function toArray(): array |
||
437 | { |
||
438 | $event = []; |
||
439 | |||
440 | $event['title'] = $this->getTitle(); |
||
441 | $event['start'] = $this->getStartDate()->format('Y-m-d\\TH:i:sP'); |
||
442 | $event['allDay'] = $this->isAllDay(); |
||
443 | $event['editable'] = $this->isEditable(); |
||
444 | $event['startEditable'] = $this->isStartEditable(); |
||
445 | $event['durationEditable'] = $this->isDurationEditable(); |
||
446 | $event['overlap'] = $this->isOverlap(); |
||
447 | |||
448 | $event['id'] = $this->getId(); |
||
449 | $event['url'] = $this->getUrl(); |
||
450 | $event['backgroundColor'] = $this->getBackgroundColor(); |
||
451 | $event['textColor'] = $this->getTextColor(); |
||
452 | $event['className'] = $this->getClassName(); |
||
453 | $event['end'] = $this->getEndDate()->format('Y-m-d\\TH:i:sP'); |
||
454 | $event['rendering'] = $this->getRendering(); |
||
455 | $event['constraint'] = $this->getConstraint(); |
||
456 | $event['source'] = $this->getSource(); |
||
457 | $event['color'] = $this->getColor(); |
||
458 | |||
459 | foreach ($this->getCustomFields() as $field => $value) { |
||
460 | $event[$field] = $value; |
||
461 | } |
||
462 | |||
463 | return $event; |
||
464 | } |
||
465 | } |
||
466 |