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 Agenda 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 Agenda, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class Agenda extends ContentType |
||
13 | { |
||
14 | const BASE_CACHE_TIME = 7200; // 2 hours |
||
15 | const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; |
||
16 | const HOUR_MIN = 8; |
||
17 | const HOUR_MAX = 18; |
||
18 | |||
19 | public $html = '<div class="agenda">%data%</div>'; |
||
20 | public $css = <<<'EO1' |
||
21 | %field% .agenda { width: 100%; height: 100%; text-align: center; background-color: white; } |
||
22 | %field% .agenda-header { font-weight: bold; } |
||
23 | %field% .agenda-contents { width: 100%; height: calc(100% - 1.3em); display: table; table-layout: fixed; border-collapse: collapse; } |
||
24 | %field% .agenda-time { display: table-cell; width: 2.2em; border: solid 1px black; } |
||
25 | %field% .agenda-time-header { } |
||
26 | %field% .agenda-time-contents { width: 100%; height: calc(100% - 1.3em); display: table; position: relative; } |
||
27 | %field% .agenda-time-h { position: absolute; border-top: solid 1px black; width: 100%; } |
||
28 | %field% .agenda-time-m { position: absolute; border-top: dotted 1px black; right: 0; } |
||
29 | %field% .agenda-time-trace { position: absolute; border-top: dotted 1px gray; width: 100%; } |
||
30 | %field% .agenda-day { display: table-cell; border: solid 1px black; } |
||
31 | %field% .agenda-day-header { border-bottom: solid 1px black; } |
||
32 | %field% .agenda-day-contents { width: 100%; height: calc(100% - 1.3em); display: table; position: relative; } |
||
33 | %field% .agenda-event { position: absolute; overflow: hidden; border-bottom: solid 1px black; z-index: 10; } |
||
34 | %field% .agenda-event-desc { font-weight: bold; font-size: 1.1em; } |
||
35 | %field% .agenda-event-location { font-size: 1.1em; white-space: nowrap; } |
||
36 | %field% .agenda-event-name { word-break: break-all; display: block; } |
||
37 | EO1; |
||
38 | public $input = 'url'; |
||
39 | public $output = 'raw'; |
||
40 | public $usable = true; |
||
41 | public $exemple = '@web/images/agenda.preview.jpg'; |
||
42 | public $canPreview = true; |
||
43 | |||
44 | private static $translit; |
||
45 | private $color = []; |
||
46 | private $opts; |
||
47 | private $overlapScanOffset = 0.1; |
||
48 | private $tz; |
||
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | public function __construct($config = []) |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | */ |
||
63 | public function processData($data) |
||
75 | |||
76 | /** |
||
77 | * Extract data from ical event. |
||
78 | * |
||
79 | * @param array $e ical event |
||
80 | * |
||
81 | * @return array parsed event |
||
82 | */ |
||
83 | private function parseEvent($e) |
||
104 | |||
105 | private function parseEvents($events) |
||
109 | |||
110 | /** |
||
111 | * Read .ical data and parse to day-based array. |
||
112 | * |
||
113 | * @param string $data ical raw data |
||
114 | * |
||
115 | * @return array agenda |
||
116 | */ |
||
117 | public function parseIcal($data) |
||
169 | |||
170 | /** |
||
171 | * Loop through day events and tag with overlaps. |
||
172 | * |
||
173 | * @param array $events |
||
174 | * @param int $from start hour |
||
175 | * @param int $to end hour |
||
176 | * |
||
177 | * @return array tagged events |
||
178 | */ |
||
179 | private function tagOverlaps($events, $from, $to) |
||
216 | |||
217 | /** |
||
218 | * Loop through day events and scan for open position. |
||
219 | * |
||
220 | * @param array $events |
||
221 | * |
||
222 | * @return array positionned blocks |
||
223 | */ |
||
224 | private function positionBlocks($events) |
||
257 | |||
258 | /** |
||
259 | * Use agenda events data to build blocks for rendering. |
||
260 | * |
||
261 | * @param array $agenda |
||
262 | * |
||
263 | * @return array blocks |
||
264 | */ |
||
265 | public function blockize($agenda) |
||
282 | |||
283 | /** |
||
284 | * Render agenda left column with hours. |
||
285 | * |
||
286 | * @param array $agenda |
||
287 | * |
||
288 | * @return string HTML column |
||
289 | */ |
||
290 | private function renderHoursColumn($agenda) |
||
310 | |||
311 | /** |
||
312 | * Render agenda events blocks columns. |
||
313 | * |
||
314 | * @param array $agenda |
||
315 | * |
||
316 | * @return string HTML blocks columns |
||
317 | */ |
||
318 | private function renderEvents($agenda) |
||
372 | |||
373 | /** |
||
374 | * Render agenda to HTML. |
||
375 | * |
||
376 | * @param array $agenda |
||
377 | * |
||
378 | * @return string HTML result |
||
379 | */ |
||
380 | public function render($agenda) |
||
392 | |||
393 | /** |
||
394 | * Generate agenda HTML from .ical url. |
||
395 | * |
||
396 | * @param string $url ical url |
||
397 | * |
||
398 | * @return string|null HTML agenda |
||
399 | */ |
||
400 | public function genAgenda($url) |
||
415 | |||
416 | /** |
||
417 | * Apply self::filter() to each array member. |
||
418 | * |
||
419 | * @param array $arr input |
||
420 | * @param string $type array type |
||
421 | * |
||
422 | * @return array filtered output |
||
423 | */ |
||
424 | private static function arrayFilter(array $arr, $type) |
||
433 | |||
434 | /** |
||
435 | * Filter string from feed. |
||
436 | * |
||
437 | * @param string $str input string |
||
438 | * @param string $type string type |
||
439 | * |
||
440 | * @return string filtered string |
||
441 | */ |
||
442 | private static function filter($str, $type) |
||
485 | |||
486 | /** |
||
487 | * Generate color based on string |
||
488 | * Using MD5 to always get the same color for a given string. |
||
489 | * |
||
490 | * @param string $str |
||
491 | * |
||
492 | * @return string color hexcode |
||
493 | */ |
||
494 | private function getColor($str) |
||
511 | } |
||
512 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.