Conditions | 52 |
Paths | > 20000 |
Total Lines | 294 |
Code Lines | 227 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
103 | public function individualOrFamilyList(ServerRequestInterface $request, bool $families, ?ModuleListInterface $moduleListInterface): ResponseInterface |
||
104 | { |
||
105 | $tree = $request->getAttribute('tree'); |
||
106 | assert($tree instanceof Tree); |
||
107 | |||
108 | $user = $request->getAttribute('user'); |
||
109 | |||
110 | // This action can show lists of both families and individuals. |
||
111 | //route is assumed to be 'module' |
||
112 | $module = $request->getAttribute('module'); |
||
113 | $action = $request->getAttribute('action'); |
||
114 | |||
115 | ob_start(); |
||
116 | |||
117 | // We show three different lists: initials, surnames and individuals |
||
118 | |||
119 | // All surnames beginning with this letter where "@"=unknown and ","=none |
||
120 | $alpha = $request->getQueryParams()['alpha'] ?? ''; |
||
121 | |||
122 | // All individuals with this surname |
||
123 | $surname = $request->getQueryParams()['surname'] ?? ''; |
||
124 | |||
125 | // All individuals |
||
126 | $show_all = $request->getQueryParams()['show_all'] ?? 'no'; |
||
127 | |||
128 | // Long lists can be broken down by given name |
||
129 | $show_all_firstnames = $request->getQueryParams()['show_all_firstnames'] ?? 'no'; |
||
130 | if ($show_all_firstnames === 'yes') { |
||
131 | $falpha = ''; |
||
132 | } else { |
||
133 | // All first names beginning with this letter |
||
134 | $falpha = $request->getQueryParams()['falpha'] ?? ''; |
||
135 | } |
||
136 | |||
137 | $show_marnm = $request->getQueryParams()['show_marnm'] ?? ''; |
||
138 | switch ($show_marnm) { |
||
139 | case 'no': |
||
140 | case 'yes': |
||
141 | $user->setPreference($families ? 'family-list-marnm' : 'individual-list-marnm', $show_marnm); |
||
142 | break; |
||
143 | default: |
||
144 | $show_marnm = $user->getPreference($families ? 'family-list-marnm' : 'individual-list-marnm'); |
||
145 | } |
||
146 | |||
147 | // Make sure selections are consistent. |
||
148 | // i.e. can’t specify show_all and surname at the same time. |
||
149 | if ($show_all === 'yes') { |
||
150 | if ($show_all_firstnames === 'yes') { |
||
151 | $alpha = ''; |
||
152 | $surname = ''; |
||
153 | $legend = I18N::translate('All'); |
||
154 | $params = [ |
||
155 | 'tree' => $tree->name(), |
||
156 | 'show_all' => 'yes', |
||
157 | ]; |
||
158 | $show = 'indi'; |
||
159 | } elseif ($falpha !== '') { |
||
160 | $alpha = ''; |
||
161 | $surname = ''; |
||
162 | $legend = I18N::translate('All') . ', ' . e($falpha) . '…'; |
||
163 | $params = [ |
||
164 | 'tree' => $tree->name(), |
||
165 | 'show_all' => 'yes', |
||
166 | ]; |
||
167 | $show = 'indi'; |
||
168 | } else { |
||
169 | $alpha = ''; |
||
170 | $surname = ''; |
||
171 | $legend = I18N::translate('All'); |
||
172 | $params = [ |
||
173 | 'tree' => $tree->name(), |
||
174 | 'show_all' => 'yes', |
||
175 | ]; |
||
176 | $show = $request->getQueryParams()['show'] ?? 'surn'; |
||
177 | } |
||
178 | } elseif ($surname !== '') { |
||
179 | $alpha = $this->localization_service->initialLetter($surname, I18N::locale()); // so we can highlight the initial letter |
||
180 | $show_all = 'no'; |
||
181 | if ($surname === '@N.N.') { |
||
182 | $legend = I18N::translateContext('Unknown surname', '…'); |
||
183 | } else { |
||
184 | // The surname parameter is a root/canonical form. |
||
185 | // Display it as the actual surname |
||
186 | $legend = implode('/', array_keys($this->individual_list_service->surnames($surname, $alpha, $show_marnm === 'yes', $families, I18N::locale()))); |
||
187 | } |
||
188 | $params = [ |
||
189 | 'tree' => $tree->name(), |
||
190 | 'surname' => $surname, |
||
191 | 'falpha' => $falpha, |
||
192 | ]; |
||
193 | switch ($falpha) { |
||
194 | case '': |
||
195 | break; |
||
196 | case '@': |
||
197 | $legend .= ', ' . I18N::translateContext('Unknown given name', '…'); |
||
198 | break; |
||
199 | default: |
||
200 | $legend .= ', ' . e($falpha) . '…'; |
||
201 | break; |
||
202 | } |
||
203 | $show = 'indi'; // SURN list makes no sense here |
||
204 | } elseif ($alpha === '@') { |
||
205 | $show_all = 'no'; |
||
206 | $legend = I18N::translateContext('Unknown surname', '…'); |
||
207 | $params = [ |
||
208 | 'alpha' => $alpha, |
||
209 | 'tree' => $tree->name(), |
||
210 | ]; |
||
211 | $show = 'indi'; // SURN list makes no sense here |
||
212 | } elseif ($alpha === ',') { |
||
213 | $show_all = 'no'; |
||
214 | $legend = I18N::translate('None'); |
||
215 | $params = [ |
||
216 | 'alpha' => $alpha, |
||
217 | 'tree' => $tree->name(), |
||
218 | ]; |
||
219 | $show = 'indi'; // SURN list makes no sense here |
||
220 | } elseif ($alpha !== '') { |
||
221 | $show_all = 'no'; |
||
222 | $legend = e($alpha) . '…'; |
||
223 | $params = [ |
||
224 | 'alpha' => $alpha, |
||
225 | 'tree' => $tree->name(), |
||
226 | ]; |
||
227 | $show = $request->getQueryParams()['show'] ?? 'surn'; |
||
228 | } else { |
||
229 | $show_all = 'no'; |
||
230 | $legend = '…'; |
||
231 | $params = [ |
||
232 | 'tree' => $tree->name(), |
||
233 | ]; |
||
234 | $show = 'none'; // Don't show lists until something is chosen |
||
235 | } |
||
236 | $legend = '<span dir="auto">' . $legend . '</span>'; |
||
237 | |||
238 | if ($families) { |
||
239 | $title = I18N::translate('Families') . ' — ' . $legend; |
||
240 | } else { |
||
241 | $title = I18N::translate('Individuals') . ' — ' . $legend; |
||
242 | } ?> |
||
243 | <div class="d-flex flex-column wt-page-options wt-page-options-individual-list d-print-none"> |
||
244 | <ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-surname"> |
||
245 | |||
246 | <?php foreach ($this->individual_list_service->surnameAlpha($show_marnm === 'yes', $families, I18N::locale()) as $letter => $count) : ?> |
||
247 | <li class="wt-initials-list-item d-flex"> |
||
248 | <?php if ($count > 0) : ?> |
||
249 | <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'alpha' => $letter, 'tree' => $tree->name()])) ?>" class="wt-initial px-1<?= $letter === $alpha ? ' active' : '' ?> '" title="<?= I18N::number($count) ?>"><?= $this->surnameInitial((string) $letter) ?></a> |
||
250 | <?php else : ?> |
||
251 | <span class="wt-initial px-1 text-muted"><?= $this->surnameInitial((string) $letter) ?></span> |
||
252 | |||
253 | <?php endif ?> |
||
254 | </li> |
||
255 | <?php endforeach ?> |
||
256 | |||
257 | <?php if (Session::has('initiated')) : ?> |
||
258 | <!-- Search spiders don't get the "show all" option as the other links give them everything. --> |
||
259 | <li class="wt-initials-list-item d-flex"> |
||
260 | <a class="wt-initial px-1<?= $show_all === 'yes' ? ' active' : '' ?>" href="<?= e(route('module', ['module' => $module, 'action' => $action, 'show_all' => 'yes'] + $params)) ?>"><?= I18N::translate('All') ?></a> |
||
261 | </li> |
||
262 | <?php endif ?> |
||
263 | </ul> |
||
264 | |||
265 | <!-- Search spiders don't get an option to show/hide the surname sublists, nor does it make sense on the all/unknown/surname views --> |
||
266 | <?php if ($show !== 'none' && Session::has('initiated')) : ?> |
||
267 | <?php if ($show_marnm === 'yes') : ?> |
||
268 | <p> |
||
269 | <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'show' => $show, 'show_marnm' => 'no'] + $params)) ?>"> |
||
270 | <?= I18N::translate('Exclude individuals with “%s” as a married name', $legend) ?> |
||
271 | </a> |
||
272 | </p> |
||
273 | <?php else : ?> |
||
274 | <p> |
||
275 | <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'show' => $show, 'show_marnm' => 'yes'] + $params)) ?>"> |
||
276 | <?= I18N::translate('Include individuals with “%s” as a married name', $legend) ?> |
||
277 | </a> |
||
278 | </p> |
||
279 | <?php endif ?> |
||
280 | |||
281 | <?php if ($alpha !== '@' && $alpha !== ',' && $surname === '') : ?> |
||
282 | <?php if ($show === 'surn') : ?> |
||
283 | <p> |
||
284 | <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'show' => 'indi', 'show_marnm' => 'no'] + $params)) ?>"> |
||
285 | <?= I18N::translate('Show the list of individuals') ?> |
||
286 | </a> |
||
287 | </p> |
||
288 | <?php else : ?> |
||
289 | <p> |
||
290 | <a href="<?= e(route('module', ['module' => $module, 'action' => $action, 'show' => 'surn', 'show_marnm' => 'no'] + $params)) ?>"> |
||
291 | <?= I18N::translate('Show the list of surnames') ?> |
||
292 | </a> |
||
293 | </p> |
||
294 | <?php endif ?> |
||
295 | <?php endif ?> |
||
296 | <?php endif ?> |
||
297 | </div> |
||
298 | |||
299 | <div class="wt-page-content"> |
||
300 | <?php |
||
301 | |||
302 | if ($show === 'indi' || $show === 'surn') { |
||
303 | $surns = $this->individual_list_service->surnames($surname, $alpha, $show_marnm === 'yes', $families, I18N::locale()); |
||
304 | if ($show === 'surn') { |
||
305 | // Show the surname list |
||
306 | switch ($tree->getPreference('SURNAME_LIST_STYLE')) { |
||
307 | case 'style1': |
||
308 | echo FunctionsPrintLists::surnameList($surns, 3, true, $moduleListInterface, $tree); |
||
309 | break; |
||
310 | case 'style3': |
||
311 | echo FunctionsPrintLists::surnameTagCloud($surns, $moduleListInterface, true, $tree); |
||
312 | break; |
||
313 | case 'style2': |
||
314 | default: |
||
315 | echo view('lists/surnames-table', [ |
||
316 | 'surnames' => $surns, |
||
317 | 'families' => $families, |
||
318 | 'module' => $moduleListInterface, |
||
319 | 'tree' => $tree, |
||
320 | ]); |
||
321 | break; |
||
322 | } |
||
323 | } else { |
||
324 | // Show the list |
||
325 | $count = 0; |
||
326 | foreach ($surns as $surnames) { |
||
327 | foreach ($surnames as $total) { |
||
328 | $count += $total; |
||
329 | } |
||
330 | } |
||
331 | // Don't sublists short lists. |
||
332 | if ($count < $tree->getPreference('SUBLIST_TRIGGER_I')) { |
||
333 | $falpha = ''; |
||
334 | } else { |
||
335 | $givn_initials = $this->individual_list_service->givenAlpha($surname, $alpha, $show_marnm === 'yes', $families, I18N::locale()); |
||
336 | // Break long lists by initial letter of given name |
||
337 | if ($surname !== '' || $show_all === 'yes') { |
||
338 | if ($show_all === 'no') { |
||
339 | echo '<h2 class="wt-page-title">', I18N::translate('Individuals with surname %s', $legend), '</h2>'; |
||
340 | } |
||
341 | // Don't show the list until we have some filter criteria |
||
342 | $show = ($falpha !== '' || $show_all_firstnames === 'yes') ? 'indi' : 'none'; |
||
343 | $list = []; |
||
344 | echo '<ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-given-names">'; |
||
345 | foreach ($givn_initials as $givn_initial => $given_count) { |
||
346 | echo '<li class="wt-initials-list-item d-flex">'; |
||
347 | if ($given_count > 0) { |
||
348 | if ($show === 'indi' && $givn_initial === $falpha && $show_all_firstnames === 'no') { |
||
349 | echo '<a class="wt-initial px-1 active" href="' . e(route('module', ['module' => $module, 'action' => $action, 'falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->givenNameInitial((string) $givn_initial) . '</a>'; |
||
350 | } else { |
||
351 | echo '<a class="wt-initial px-1" href="' . e(route('module', ['module' => $module, 'action' => $action, 'falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->givenNameInitial((string) $givn_initial) . '</a>'; |
||
352 | } |
||
353 | } else { |
||
354 | echo '<span class="wt-initial px-1 text-muted">' . $this->givenNameInitial((string) $givn_initial) . '</span>'; |
||
355 | } |
||
356 | echo '</li>'; |
||
357 | } |
||
358 | // Search spiders don't get the "show all" option as the other links give them everything. |
||
359 | if (Session::has('initiated')) { |
||
360 | echo '<li class="wt-initials-list-item d-flex">'; |
||
361 | if ($show_all_firstnames === 'yes') { |
||
362 | echo '<span class="wt-initial px-1 warning">' . I18N::translate('All') . '</span>'; |
||
363 | } else { |
||
364 | echo '<a class="wt-initial px-1" href="' . e(route('module', ['module' => $module, 'action' => $action, 'show_all_firstnames' => 'yes'] + $params)) . '" title="' . I18N::number($count) . '">' . I18N::translate('All') . '</a>'; |
||
365 | } |
||
366 | echo '</li>'; |
||
367 | } |
||
368 | echo '</ul>'; |
||
369 | echo '<p class="text-center alpha_index">', implode(' | ', $list), '</p>'; |
||
370 | } |
||
371 | } |
||
372 | if ($show === 'indi') { |
||
373 | if (!$families) { |
||
374 | echo view('lists/individuals-table', [ |
||
375 | 'individuals' => $this->individual_list_service->individuals($surname, $alpha, $falpha, $show_marnm === 'yes', false, I18N::locale()), |
||
376 | 'sosa' => false, |
||
377 | 'tree' => $tree, |
||
378 | ]); |
||
379 | } else { |
||
380 | echo view('lists/families-table', [ |
||
381 | 'families' => $this->individual_list_service->families($surname, $alpha, $falpha, $show_marnm === 'yes', I18N::locale()), |
||
382 | 'tree' => $tree, |
||
383 | ]); |
||
384 | } |
||
385 | } |
||
386 | } |
||
387 | } ?> |
||
388 | </div> |
||
389 | <?php |
||
390 | |||
391 | $html = ob_get_clean(); |
||
392 | |||
393 | return $this->viewResponse('modules/individual-list/page', [ |
||
394 | 'content' => $html, |
||
395 | 'title' => $title, |
||
396 | 'tree' => $tree, |
||
397 | ]); |
||
749 |