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 |
||
13 | class Agenda extends ContentType |
||
14 | { |
||
15 | const BASE_CACHE_TIME = 7200; // 2 hours |
||
16 | const DAYS = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']; |
||
17 | const HOUR_MIN = 8; |
||
18 | const HOUR_MAX = 18; |
||
19 | |||
20 | public $html = '<div class="agenda">%data%</div>'; |
||
21 | public $css = <<<'EO1' |
||
22 | %field% .agenda { width: 100%; height: 100%; text-align: center; background-color: white; } |
||
23 | %field% .agenda-header { font-weight: bold; } |
||
24 | %field% .agenda-contents { width: 100%; height: calc(100% - 1.3em); display: table; table-layout: fixed; border-collapse: collapse; } |
||
25 | %field% .agenda-time { display: table-cell; width: 2.2em; border: solid 1px black; } |
||
26 | %field% .agenda-time-header { } |
||
27 | %field% .agenda-time-contents { width: 100%; height: calc(100% - 1.3em); display: table; position: relative; } |
||
28 | %field% .agenda-time-h { position: absolute; border-top: solid 1px black; width: 100%; } |
||
29 | %field% .agenda-time-m { position: absolute; border-top: dotted 1px black; right: 0; } |
||
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; } |
||
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 $preview = '@web/images/agenda.preview.jpg'; |
||
42 | |||
43 | private static $translit; |
||
44 | private $color = []; |
||
45 | private $opts; |
||
46 | |||
47 | /** |
||
48 | * {@inheritdoc} |
||
49 | */ |
||
50 | public function __construct($config = []) |
||
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | public function processData($data) |
||
72 | |||
73 | /** |
||
74 | * Read .ical data and parse to day-based array. |
||
75 | * |
||
76 | * @param string $data ical raw data |
||
77 | * |
||
78 | * @return array events |
||
79 | */ |
||
80 | public function parseIcal($data) |
||
149 | |||
150 | /** |
||
151 | * Use agenda events data to build blocks for rendering. |
||
152 | * |
||
153 | * @param array $agenda |
||
154 | * |
||
155 | * @return array blocks |
||
156 | */ |
||
157 | public function blockize($agenda) |
||
235 | |||
236 | /** |
||
237 | * Scan agenda events for distinct locations, count them and sort desc. |
||
238 | * |
||
239 | * @param array $agenda |
||
240 | * |
||
241 | * @return array locations |
||
242 | */ |
||
243 | private static function locations($agenda) |
||
265 | |||
266 | /** |
||
267 | * Scan agenda events for distinct descriptions, count them (with overlap weight) and sort desc. |
||
268 | * |
||
269 | * @param array $agenda |
||
270 | * |
||
271 | * @return array descriptions |
||
272 | */ |
||
273 | private static function descriptions($agenda) |
||
296 | |||
297 | /** |
||
298 | * Scan agenda events for overlaps. |
||
299 | * |
||
300 | * @param array $agenda |
||
301 | * |
||
302 | * @return bool has overlaps |
||
303 | */ |
||
304 | private static function hasOverlaps($agenda) |
||
316 | |||
317 | /** |
||
318 | * Scan agenda and guess best title based on locations and descriptions. |
||
319 | * |
||
320 | * @param array $agenda |
||
321 | * |
||
322 | * @return string title |
||
323 | */ |
||
324 | private static function genTitle($agenda) |
||
340 | |||
341 | /** |
||
342 | * Last processing before render |
||
343 | * Generate title. |
||
344 | * |
||
345 | * @param array $agenda |
||
346 | * |
||
347 | * @return array agenga info |
||
348 | */ |
||
349 | public function finalize($agenda) |
||
357 | |||
358 | /** |
||
359 | * Render agenda events block to HTML. |
||
360 | * |
||
361 | * @param array $agenda |
||
362 | * |
||
363 | * @return string HTML result |
||
364 | */ |
||
365 | public function render($agenda) |
||
421 | |||
422 | /** |
||
423 | * Generate agenda HTML from .ical url. |
||
424 | * |
||
425 | * @param string $url ical url |
||
426 | * |
||
427 | * @return string HTML agenda |
||
428 | */ |
||
429 | public function genAgenda($url) |
||
446 | |||
447 | /** |
||
448 | * Apply self::filter() to each array member. |
||
449 | * |
||
450 | * @param array $arr input |
||
451 | * @param string $type array type |
||
452 | * |
||
453 | * @return array filtered output |
||
454 | */ |
||
455 | private static function arrayFilter(array $arr, $type) |
||
464 | |||
465 | /** |
||
466 | * Filter string from feed. |
||
467 | * |
||
468 | * @param string $str input string |
||
469 | * @param string $type string type |
||
470 | * |
||
471 | * @return string filtered string |
||
472 | */ |
||
473 | private static function filter($str, $type) |
||
516 | |||
517 | /** |
||
518 | * Generate color based on string |
||
519 | * Using MD5 to always get the same color for a given string. |
||
520 | * |
||
521 | * @param string $str |
||
522 | * |
||
523 | * @return string color hexcode |
||
524 | */ |
||
525 | private function getColor($str) |
||
542 | } |
||
543 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: