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 TimelineController 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 TimelineController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class TimelineController extends PageController { |
||
34 | /** @var int Height of the age box */ |
||
35 | public $bheight = 30; |
||
36 | |||
37 | /** @var Fact[] The facts to display on the chart */ |
||
38 | public $indifacts = []; // array to store the fact records in for sorting and displaying |
||
39 | |||
40 | /** @var int[] Numeric birth years of each individual */ |
||
41 | public $birthyears = []; |
||
42 | |||
43 | /** @var int[] Numeric birth months of each individual */ |
||
44 | public $birthmonths = []; |
||
45 | |||
46 | /** @var int[] Numeric birth days of each individual */ |
||
47 | public $birthdays = []; |
||
48 | |||
49 | /** @var int Lowest year to display */ |
||
50 | public $baseyear = 0; |
||
51 | |||
52 | /** @var int Highest year to display */ |
||
53 | public $topyear = 0; |
||
54 | |||
55 | /** @var Individual[] List of individuals to display */ |
||
56 | public $people = []; |
||
57 | |||
58 | /** @var string URL-encoded list of XREFs */ |
||
59 | public $pidlinks = ''; |
||
60 | |||
61 | /** @var int Vertical scale */ |
||
62 | public $scale = 2; |
||
63 | |||
64 | /** @var string[] GEDCOM elements that may have DATE data, but should not be displayed */ |
||
65 | private $nonfacts = ['BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN']; |
||
66 | |||
67 | /** |
||
68 | * Startup activity |
||
69 | */ |
||
70 | public function __construct() { |
||
71 | parent::__construct(); |
||
72 | |||
73 | $this->setPageTitle(I18N::translate('Timeline')); |
||
74 | |||
75 | $this->baseyear = (int) date('Y'); |
||
76 | |||
77 | $remove = Filter::get('remove', WT_REGEX_XREF); |
||
78 | $newpid = Filter::get('newpid', WT_REGEX_XREF); |
||
79 | $pids = Filter::getArray('pids', WT_REGEX_XREF); |
||
80 | $pids[] = $newpid; |
||
81 | |||
82 | foreach (array_unique(array_filter($pids)) as $pid) { |
||
83 | if ($pid !== $remove) { |
||
84 | $person = Individual::getInstance($pid, $this->tree()); |
||
85 | if ($person && $person->canShow()) { |
||
86 | $this->people[] = $person; |
||
87 | } |
||
88 | } |
||
89 | } |
||
90 | $this->pidlinks = ''; |
||
91 | |||
92 | foreach ($this->people as $indi) { |
||
93 | // setup string of valid pids for links |
||
94 | $this->pidlinks .= 'pids%5B%5D=' . $indi->getXref() . '&'; |
||
95 | $bdate = $indi->getBirthDate(); |
||
|
|||
96 | if ($bdate->isOK()) { |
||
97 | $date = new GregorianDate($bdate->minimumJulianDay()); |
||
98 | $this->birthyears [$indi->getXref()] = $date->y; |
||
99 | $this->birthmonths[$indi->getXref()] = max(1, $date->m); |
||
100 | $this->birthdays [$indi->getXref()] = max(1, $date->d); |
||
101 | } |
||
102 | // find all the fact information |
||
103 | $facts = $indi->getFacts(); |
||
104 | foreach ($indi->getSpouseFamilies() as $family) { |
||
105 | foreach ($family->getFacts() as $fact) { |
||
106 | $facts[] = $fact; |
||
107 | } |
||
108 | } |
||
109 | foreach ($facts as $event) { |
||
110 | // get the fact type |
||
111 | $fact = $event->getTag(); |
||
112 | if (!in_array($fact, $this->nonfacts)) { |
||
113 | // check for a date |
||
114 | $date = $event->getDate(); |
||
115 | if ($date->isOK()) { |
||
116 | $date = new GregorianDate($date->minimumJulianDay()); |
||
117 | $this->baseyear = min($this->baseyear, $date->y); |
||
118 | $this->topyear = max($this->topyear, $date->y); |
||
119 | |||
120 | if (!$indi->isDead()) { |
||
121 | $this->topyear = max($this->topyear, (int) date('Y')); |
||
122 | } |
||
123 | |||
124 | // do not add the same fact twice (prevents marriages from being added multiple times) |
||
125 | if (!in_array($event, $this->indifacts, true)) { |
||
126 | $this->indifacts[] = $event; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | $scale = Filter::getInteger('scale', 0, 200); |
||
133 | if ($scale === 0) { |
||
134 | $this->scale = (int) (($this->topyear - $this->baseyear) / 20 * count($this->indifacts) / 4); |
||
135 | if ($this->scale < 6) { |
||
136 | $this->scale = 6; |
||
137 | } |
||
138 | } else { |
||
139 | $this->scale = $scale; |
||
140 | } |
||
141 | if ($this->scale < 2) { |
||
142 | $this->scale = 2; |
||
143 | } |
||
144 | $this->baseyear -= 5; |
||
145 | $this->topyear += 5; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Print a fact for an individual. |
||
150 | * |
||
151 | * @param Fact $event |
||
152 | */ |
||
153 | public function printTimeFact(Fact $event) { |
||
270 | |||
271 | /** |
||
272 | * Get significant information from this page, to allow other pages such as |
||
273 | * charts and reports to initialise with the same records |
||
274 | * |
||
275 | * @return Individual |
||
276 | */ |
||
277 | public function getSignificantIndividual() { |
||
284 | } |
||
285 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: