Total Complexity | 42 |
Total Lines | 274 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like IndividualController 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 IndividualController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class IndividualController extends BaseController { |
||
44 | /** |
||
45 | * Show a individual's page. |
||
46 | * |
||
47 | * @param Request $request |
||
48 | * |
||
49 | * @return Response |
||
50 | */ |
||
51 | public function show(Request $request): Response { |
||
52 | /** @var Tree $tree */ |
||
53 | $tree = $request->attributes->get('tree'); |
||
54 | $xref = $request->get('xref'); |
||
55 | $record = Individual::getInstance($xref, $tree); |
||
56 | |||
57 | if ($record === null) { |
||
58 | return $this->individualNotFound(); |
||
59 | } elseif (!$record->canShow()) { |
||
60 | return $this->individualNotAllowed(); |
||
61 | } else { |
||
62 | $individual_media = []; |
||
63 | // What is (was) the age of the individual |
||
64 | $bdate = $record->getBirthDate(); |
||
65 | $ddate = $record->getDeathDate(); |
||
66 | if ($bdate->isOK() && !$record->isDead()) { |
||
67 | // If living display age |
||
68 | $age = ' (' . I18N::translate('age') . ' ' . FunctionsDate::getAgeAtEvent(Date::getAgeGedcom($bdate, new Date(strtoupper(date('d M Y'))))) . ')'; |
||
69 | } elseif ($bdate->isOK() && $ddate->isOK()) { |
||
70 | // If dead, show age at death |
||
71 | $age = ' (' . I18N::translate('age') . ' ' . FunctionsDate::getAgeAtEvent(Date::getAgeGedcom($bdate, $ddate)) . ')'; |
||
72 | } else { |
||
73 | $age = ''; |
||
74 | } |
||
75 | |||
76 | foreach ($record->getFacts() as $fact) { |
||
77 | $media_object = $fact->getTarget(); |
||
78 | if ($media_object instanceof Media) { |
||
79 | $individual_media[] = $media_object->firstImageFile(); |
||
80 | } |
||
81 | } |
||
82 | $individual_media = array_filter($individual_media); |
||
83 | |||
84 | $name_records = []; |
||
85 | foreach ($record->getFacts('NAME') as $n => $name_fact) { |
||
86 | $name_records[] = $this->formatNameRecord($n, $name_fact); |
||
87 | } |
||
88 | |||
89 | $sex_records = []; |
||
90 | foreach ($record->getFacts('SEX') as $n => $sex_fact) { |
||
91 | $sex_records[] = $this->formatSexRecord($sex_fact); |
||
92 | } |
||
93 | |||
94 | // If this individual is linked to a user account, show the link |
||
95 | $user_link = ''; |
||
96 | if (Auth::isAdmin()) { |
||
97 | $user = User::findByIndividual($record); |
||
98 | if ($user) { |
||
99 | $user_link = ' — <a href="admin_users.php?filter=' . e($user->getUserName()) . '">' . e($user->getUserName()) . '</a>'; |
||
100 | }; |
||
101 | } |
||
102 | |||
103 | return $this->viewResponse('individual-page', [ |
||
104 | 'age' => $age, |
||
105 | 'individual' => $record, |
||
106 | 'individual_media' => $individual_media, |
||
107 | 'name_records' => $name_records, |
||
108 | 'sex_records' => $sex_records, |
||
109 | 'sidebars' => $this->getSidebars($record), |
||
110 | 'tabs' => $this->getTabs($record), |
||
111 | 'user_link' => $user_link, |
||
112 | ]); |
||
113 | } |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @param Request $request |
||
118 | * |
||
119 | * @return Response |
||
120 | */ |
||
121 | public function tab(Request $request): Response { |
||
122 | /** @var Tree $tree */ |
||
123 | $tree = $request->attributes->get('tree'); |
||
124 | $xref = $request->get('xref'); |
||
125 | $record = Individual::getInstance($xref, $tree); |
||
126 | $tab = $request->get('module'); |
||
127 | $tabs = Module::getActiveTabs($tree); |
||
128 | |||
129 | if ($record === null || !array_key_exists($tab, $tabs)) { |
||
130 | return new Response('', Response::HTTP_NOT_FOUND); |
||
131 | } elseif (!$record->canShow()) { |
||
132 | return new Response('', Response::HTTP_FORBIDDEN); |
||
133 | } else { |
||
134 | $tab = $tabs[$tab]; |
||
135 | |||
136 | $layout = view('layouts/ajax', [ |
||
137 | 'content' => $tab->getTabContent($record), |
||
138 | ]); |
||
139 | |||
140 | return new Response($layout); |
||
141 | } |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Format a name record |
||
146 | * |
||
147 | * @param int $n |
||
148 | * @param Fact $fact |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | private function formatNameRecord($n, Fact $fact) { |
||
153 | $individual = $fact->getParent(); |
||
154 | |||
155 | // Create a dummy record, so we can extract the formatted NAME value from it. |
||
156 | $dummy = new Individual( |
||
157 | 'xref', |
||
158 | "0 @xref@ INDI\n1 DEAT Y\n" . $fact->getGedcom(), |
||
159 | null, |
||
160 | $individual->getTree() |
||
161 | ); |
||
162 | $dummy->setPrimaryName(0); // Make sure we use the name from "1 NAME" |
||
163 | |||
164 | $container_class = 'card'; |
||
165 | $content_class = 'collapse'; |
||
166 | $aria = 'false'; |
||
167 | |||
168 | if ($n === 0) { |
||
169 | $content_class = 'collapse show'; |
||
170 | $aria = 'true'; |
||
171 | } |
||
172 | if ($fact->isPendingDeletion()) { |
||
173 | $container_class .= ' old'; |
||
174 | } elseif ($fact->isPendingAddition()) { |
||
175 | $container_class .= ' new'; |
||
176 | } |
||
177 | |||
178 | ob_start(); |
||
179 | echo '<dl><dt class="label">', I18N::translate('Name'), '</dt>'; |
||
180 | echo '<dd class="field">', $dummy->getFullName(), '</dd>'; |
||
181 | $ct = preg_match_all('/\n2 (\w+) (.*)/', $fact->getGedcom(), $nmatch, PREG_SET_ORDER); |
||
182 | for ($i = 0; $i < $ct; $i++) { |
||
183 | $tag = $nmatch[$i][1]; |
||
184 | if ($tag != 'SOUR' && $tag != 'NOTE' && $tag != 'SPFX') { |
||
185 | echo '<dt class="label">', GedcomTag::getLabel($tag, $individual), '</dt>'; |
||
186 | echo '<dd class="field">'; // Before using dir="auto" on this field, note that Gecko treats this as an inline element but WebKit treats it as a block element |
||
187 | if (isset($nmatch[$i][2])) { |
||
188 | $name = e($nmatch[$i][2]); |
||
189 | $name = str_replace('/', '', $name); |
||
190 | $name = preg_replace('/(\S*)\*/', '<span class="starredname">\\1</span>', $name); |
||
191 | switch ($tag) { |
||
192 | case 'TYPE': |
||
193 | echo GedcomCodeName::getValue($name, $individual); |
||
194 | break; |
||
195 | case 'SURN': |
||
196 | // The SURN field is not necessarily the surname. |
||
197 | // Where it is not a substring of the real surname, show it after the real surname. |
||
198 | $surname = e($dummy->getAllNames()[0]['surname']); |
||
199 | $surns = preg_replace('/, */', ' ', $nmatch[$i][2]); |
||
200 | if (strpos($dummy->getAllNames()[0]['surname'], $surns) !== false) { |
||
201 | echo '<span dir="auto">' . $surname . '</span>'; |
||
202 | } else { |
||
203 | echo I18N::translate('%1$s (%2$s)', '<span dir="auto">' . $surname . '</span>', '<span dir="auto">' . $name . '</span>'); |
||
204 | } |
||
205 | break; |
||
206 | default: |
||
207 | echo '<span dir="auto">' . $name . '</span>'; |
||
208 | break; |
||
209 | } |
||
210 | } |
||
211 | echo '</dd>'; |
||
212 | echo '</dl>'; |
||
213 | } |
||
214 | } |
||
215 | if (preg_match("/\n2 SOUR/", $fact->getGedcom())) { |
||
216 | echo '<div id="indi_sour" class="clearfloat">', FunctionsPrintFacts::printFactSources($fact->getGedcom(), 2), '</div>'; |
||
217 | } |
||
218 | if (preg_match("/\n2 NOTE/", $fact->getGedcom())) { |
||
219 | echo '<div id="indi_note" class="clearfloat">', FunctionsPrint::printFactNotes($fact->getGedcom(), 2), '</div>'; |
||
220 | } |
||
221 | $content = ob_get_clean(); |
||
222 | |||
223 | if ($individual->canEdit() && !$fact->isPendingDeletion()) { |
||
224 | $edit_links = |
||
225 | FontAwesome::linkIcon('delete', I18N::translate('Delete this name'), ['class' => 'btn btn-link', 'href' => '#', 'onclick' => 'return delete_fact("' . I18N::translate('Are you sure you want to delete this fact?') . '", "' . $individual->getXref() . '", "' . $fact->getFactId() . '");']) . |
||
226 | FontAwesome::linkIcon('edit', I18N::translate('Edit the name'), ['class' => 'btn btn-link', 'href' => 'edit_interface.php?action=editname&xref=' . $individual->getXref() . '&fact_id=' . $fact->getFactId() . '&ged=' . $individual->getTree()->getNameHtml()]); |
||
227 | } else { |
||
228 | $edit_links = ''; |
||
229 | } |
||
230 | |||
231 | return ' |
||
232 | <div class="' . $container_class . '"> |
||
233 | <div class="card-header" role="tab" id="name-header-' . $n . '"> |
||
234 | <a data-toggle="collapse" data-parent="#individual-names" href="#name-content-' . $n . '" aria-expanded="' . $aria . '" aria-controls="name-content-' . $n . '">' . $dummy->getFullName() . '</a> |
||
235 | ' . $edit_links . ' |
||
236 | </div> |
||
237 | <div id="name-content-' . $n . '" class="' . $content_class . '" role="tabpanel" aria-labelledby="name-header-' . $n . '"> |
||
238 | <div class="card-body">' . $content . '</div> |
||
239 | </div> |
||
240 | </div>'; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * print information for a sex record |
||
245 | * |
||
246 | * @param Fact $fact |
||
247 | * |
||
248 | * @return string |
||
249 | */ |
||
250 | private function formatSexRecord(Fact $fact) { |
||
283 | </div> |
||
284 | </div> |
||
285 | </div>'; |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * Which tabs should we show on this individual's page. |
||
290 | * We don't show empty tabs. |
||
291 | * |
||
292 | * @param Individual $individual |
||
293 | * |
||
294 | * @return ModuleTabInterface[] |
||
295 | */ |
||
296 | public function getSidebars(Individual $individual) { |
||
297 | $sidebars = Module::getActiveSidebars($individual->getTree()); |
||
298 | |||
299 | return array_filter($sidebars, function (ModuleSidebarInterface $sidebar) use ($individual) { |
||
300 | return $sidebar->hasSidebarContent($individual); |
||
301 | }); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * Which tabs should we show on this individual's page. |
||
306 | * We don't show empty tabs. |
||
307 | * |
||
308 | * @param Individual $individual |
||
309 | * |
||
310 | * @return ModuleTabInterface[] |
||
311 | */ |
||
312 | public function getTabs(Individual $individual) { |
||
317 | }); |
||
318 | } |
||
319 | } |
||
320 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.