Total Complexity | 60 |
Total Lines | 389 |
Duplicated Lines | 10.28 % |
Changes | 0 |
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 HourglassController 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 HourglassController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class HourglassController extends ChartController { |
||
29 | /** @var int Whether to show spouse details. */ |
||
30 | public $show_spouse; |
||
31 | |||
32 | /** @var int Number of ascendancy generations to show. */ |
||
33 | public $generations; |
||
34 | |||
35 | /** @var int Number of descendancy generations that exist. */ |
||
36 | public $dgenerations; |
||
37 | |||
38 | /** @var int Half height of personbox. */ |
||
39 | public $bhalfheight; |
||
40 | |||
41 | const SWITCH_LINK = "<a href='hourglass.php?rootid=%s&show_spouse=%s&generations=%s' class='name1'>%s</a>"; |
||
42 | |||
43 | /** |
||
44 | * Create the hourglass controller. |
||
45 | * |
||
46 | * @param string $rootid |
||
47 | */ |
||
48 | public function __construct($rootid = '') { |
||
|
|||
49 | parent::__construct(); |
||
50 | |||
51 | // Extract parameters from |
||
52 | $this->show_spouse = Filter::getInteger('show_spouse', 0, 1, 0); |
||
53 | $this->generations = Filter::getInteger('generations', 2, $this->tree()->getPreference('MAX_DESCENDANCY_GENERATIONS'), 3); |
||
54 | |||
55 | $this->bhalfheight = (int) ($this->getBoxDimensions()->height / 2); |
||
56 | |||
57 | //Checks how many generations of descendency is for the person for formatting purposes |
||
58 | $this->dgenerations = $this->maxDescendencyGenerations($this->root, 0); |
||
59 | if ($this->dgenerations < 1) { |
||
60 | $this->dgenerations = 1; |
||
61 | } |
||
62 | |||
63 | $this->setPageTitle(/* I18N: %s is an individual’s name */ I18N::translate('Hourglass chart of %s', $this->root->getFullName())); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Prints pedigree of the person passed in. Which is the descendancy |
||
68 | * |
||
69 | * @param Individual $person ID of person to print the pedigree for |
||
70 | * @param int $count generation count, so it recursively calls itself |
||
71 | */ |
||
72 | public function printPersonPedigree(Individual $person, $count) { |
||
73 | if ($count >= $this->generations) { |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | $genoffset = $this->generations; // handle pedigree n generations lines |
||
78 | |||
79 | // |
||
80 | //Prints empty table columns for children w/o parents up to the max generation |
||
81 | //This allows vertical line spacing to be consistent |
||
82 | // |
||
83 | if (count($person->getChildFamilies()) == 0) { |
||
84 | echo '<table><tr><td> ' . $this->printEmptyBox() . '</td>'; |
||
85 | echo '<td> '; |
||
86 | // Recursively get the father’s family |
||
87 | $this->printPersonPedigree($person, $count + 1); |
||
88 | echo '</td></tr>'; |
||
89 | echo '<tr><td> ' . $this->printEmptyBox() . '</td>'; |
||
90 | echo '<td> '; |
||
91 | // Recursively get the mother’s family |
||
92 | $this->printPersonPedigree($person, $count + 1); |
||
93 | echo '</td><td> </tr></table>'; |
||
94 | } |
||
95 | foreach ($person->getChildFamilies() as $family) { |
||
96 | echo '<table cellspacing="0" cellpadding="0" border="0" class="hourglassChart">'; |
||
97 | echo '<tr>'; |
||
98 | echo '<td style="vertical-align:bottom"><img class="line3 pvline" src="' . Theme::theme()->parameter('image-vline') . '" width="3"></td>'; |
||
99 | echo '<td> <img class="lineh2" src="' . Theme::theme()->parameter('image-hline') . '" width="7" height="3"></td>'; |
||
100 | echo '<td class="myCharts"> '; |
||
101 | //-- print the father box |
||
102 | FunctionsPrint::printPedigreePerson($family->getHusband()); |
||
103 | echo '</td>'; |
||
104 | if ($family->getHusband()) { |
||
105 | $ARID = $family->getHusband()->getXref(); |
||
106 | echo '<td id="td_' . $ARID . '">'; |
||
107 | |||
108 | //-- print an Ajax arrow on the last generation of the adult male |
||
109 | View Code Duplication | if ($count == $this->generations - 1 && $family->getHusband()->getChildFamilies()) { |
|
110 | echo FontAwesome::linkIcon('arrow-end', I18N::translate('Parents'), [ |
||
111 | 'href' => '#', |
||
112 | 'data-direction' => 'asc', |
||
113 | 'data-xref' => $ARID, |
||
114 | 'data-spouses' => $this->show_spouse, |
||
115 | ]); |
||
116 | } |
||
117 | //-- recursively get the father’s family |
||
118 | $this->printPersonPedigree($family->getHusband(), $count + 1); |
||
119 | echo '</td>'; |
||
120 | View Code Duplication | } else { |
|
121 | echo '<td> '; |
||
122 | if ($count < $genoffset - 1) { |
||
123 | echo '<table>'; |
||
124 | for ($i = $count; $i < (pow(2, ($genoffset - 1) - $count) / 2) + 2; $i++) { |
||
125 | $this->printEmptyBox(); |
||
126 | echo '</tr>'; |
||
127 | $this->printEmptyBox(); |
||
128 | echo '</tr>'; |
||
129 | } |
||
130 | echo '</table>'; |
||
131 | } |
||
132 | } |
||
133 | echo |
||
134 | '</tr><tr>', |
||
135 | "<td style='vertical-align:top'><img class='pvline' src='" . Theme::theme()->parameter('image-vline') . "' width='3' alt=''></td>", |
||
136 | '<td> <img class="lineh3" src="' . Theme::theme()->parameter('image-hline') . '" width="7" height="3"></td>', |
||
137 | '<td class="myCharts"> '; |
||
138 | //-- print the mother box |
||
139 | FunctionsPrint::printPedigreePerson($family->getWife()); |
||
140 | echo '</td>'; |
||
141 | if ($family->getWife()) { |
||
142 | $ARID = $family->getWife()->getXref(); |
||
143 | echo '<td id="td_' . $ARID . '">'; |
||
144 | |||
145 | //-- print an ajax arrow on the last generation of the adult female |
||
146 | View Code Duplication | if ($count == $this->generations - 1 && $family->getWife()->getChildFamilies()) { |
|
147 | echo FontAwesome::linkIcon('arrow-end', I18N::translate('Parents'), [ |
||
148 | 'href' => '#', |
||
149 | 'data-direction' => 'asc', |
||
150 | 'data-xref' => $ARID, |
||
151 | 'data-spouses' => $this->show_spouse, |
||
152 | ]); |
||
153 | } |
||
154 | //-- recursively print the mother’s family |
||
155 | $this->printPersonPedigree($family->getWife(), $count + 1); |
||
156 | echo '</td>'; |
||
157 | } |
||
158 | echo '</tr></table>'; |
||
159 | break; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Print empty box |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | |||
169 | private function printEmptyBox() { |
||
170 | return Theme::theme()->individualBoxEmpty(); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Prints descendency of passed in person |
||
175 | * |
||
176 | * @param Individual $person person to print descendency for |
||
177 | * @param int $count count of generations to print |
||
178 | * @param bool $showNav |
||
179 | * |
||
180 | * @return int |
||
181 | */ |
||
182 | public function printDescendency($person, $count, $showNav = true) { |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Calculates number of generations a person has |
||
369 | * |
||
370 | * @param Individual $individual Start individual |
||
371 | * @param int $depth Pass in 0 and it calculates how far down descendency goes |
||
372 | * |
||
373 | * @return int Number of generations the descendency actually goes |
||
374 | */ |
||
375 | private function maxDescendencyGenerations(Individual $individual, $depth) { |
||
376 | if ($depth > $this->generations) { |
||
377 | return $depth; |
||
378 | } |
||
379 | $maxdc = $depth; |
||
380 | View Code Duplication | foreach ($individual->getSpouseFamilies() as $family) { |
|
381 | foreach ($family->getChildren() as $child) { |
||
382 | $dc = $this->maxDescendencyGenerations($child, $depth + 1); |
||
383 | if ($dc >= $this->generations) { |
||
384 | return $dc; |
||
385 | } |
||
386 | if ($dc > $maxdc) { |
||
387 | $maxdc = $dc; |
||
388 | } |
||
389 | } |
||
390 | } |
||
391 | |||
392 | $maxdc++; |
||
393 | if ($maxdc == 1) { |
||
394 | $maxdc++; |
||
395 | } |
||
396 | |||
397 | return $maxdc; |
||
398 | } |
||
399 | |||
400 | /** |
||
401 | * setup all of the javascript that is needed for the hourglass chart |
||
402 | * |
||
403 | * @return string |
||
404 | */ |
||
405 | public function setupJavascript() { |
||
417 | }); |
||
418 | |||
419 | $('.pvline').each(function(i,e) { |
||
420 | var el = $(e); |
||
421 | el.height(Math.floor(el.parent().height()/2)); |
||
447 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.