Total Complexity | 50 |
Total Lines | 599 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like EditIndividualController 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 EditIndividualController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class EditIndividualController extends AbstractEditController |
||
38 | { |
||
39 | /** |
||
40 | * Add a child to an existing individual (creating a one-parent family). |
||
41 | * |
||
42 | * @param ServerRequestInterface $request |
||
43 | * |
||
44 | * @return ResponseInterface |
||
45 | */ |
||
46 | public function addChild(ServerRequestInterface $request): ResponseInterface |
||
47 | { |
||
48 | $tree = $request->getAttribute('tree'); |
||
49 | $xref = $request->getQueryParams()['xref']; |
||
50 | $individual = Individual::getInstance($xref, $tree); |
||
51 | |||
52 | Auth::checkIndividualAccess($individual, true); |
||
53 | |||
54 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a child to create a one-parent family'); |
||
55 | |||
56 | return $this->viewResponse('edit/new-individual', [ |
||
57 | 'next_action' => 'add-child-to-individual-action', |
||
58 | 'tree' => $tree, |
||
59 | 'title' => $title, |
||
60 | 'individual' => $individual, |
||
61 | 'family' => null, |
||
62 | 'name_fact' => null, |
||
63 | 'famtag' => 'CHIL', |
||
64 | 'gender' => 'U', |
||
65 | ]); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * @param ServerRequestInterface $request |
||
70 | * |
||
71 | * @return ResponseInterface |
||
72 | */ |
||
73 | public function addChildAction(ServerRequestInterface $request): ResponseInterface |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Add a parent to an existing individual (creating a one-parent family). |
||
132 | * |
||
133 | * @param ServerRequestInterface $request |
||
134 | * |
||
135 | * @return ResponseInterface |
||
136 | */ |
||
137 | public function addParent(ServerRequestInterface $request): ResponseInterface |
||
138 | { |
||
139 | $tree = $request->getAttribute('tree'); |
||
140 | $xref = $request->getQueryParams()['xref']; |
||
141 | $gender = $request->getQueryParams()['gender']; |
||
142 | |||
143 | $individual = Individual::getInstance($xref, $tree); |
||
144 | |||
145 | Auth::checkIndividualAccess($individual, true); |
||
146 | |||
147 | if ($gender === 'F') { |
||
148 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a mother'); |
||
149 | $famtag = 'WIFE'; |
||
150 | } else { |
||
151 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a father'); |
||
152 | $famtag = 'HUSB'; |
||
153 | } |
||
154 | |||
155 | return $this->viewResponse('edit/new-individual', [ |
||
156 | 'next_action' => 'add-parent-to-individual-action', |
||
157 | 'tree' => $tree, |
||
158 | 'title' => $title, |
||
159 | 'individual' => $individual, |
||
160 | 'family' => null, |
||
161 | 'name_fact' => null, |
||
162 | 'famtag' => $famtag, |
||
163 | 'gender' => $gender, |
||
164 | ]); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @param ServerRequestInterface $request |
||
169 | * |
||
170 | * @return ResponseInterface |
||
171 | */ |
||
172 | public function addParentAction(ServerRequestInterface $request): ResponseInterface |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Add a spouse to an existing individual (creating a new family). |
||
229 | * |
||
230 | * @param ServerRequestInterface $request |
||
231 | * |
||
232 | * @return ResponseInterface |
||
233 | */ |
||
234 | public function addSpouse(ServerRequestInterface $request): ResponseInterface |
||
235 | { |
||
236 | $tree = $request->getAttribute('tree'); |
||
237 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
238 | |||
239 | $xref = $request->getQueryParams()['xref']; |
||
240 | |||
241 | $individual = Individual::getInstance($xref, $tree); |
||
242 | |||
243 | Auth::checkIndividualAccess($individual, true); |
||
244 | |||
245 | if ($individual->sex() === 'F') { |
||
246 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a husband'); |
||
247 | $famtag = 'HUSB'; |
||
248 | $gender = 'M'; |
||
249 | } else { |
||
250 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a wife'); |
||
251 | $famtag = 'WIFE'; |
||
252 | $gender = 'F'; |
||
253 | } |
||
254 | |||
255 | return $this->viewResponse('edit/new-individual', [ |
||
256 | 'next_action' => 'add-spouse-to-individual-action', |
||
257 | 'tree' => $tree, |
||
258 | 'title' => $title, |
||
259 | 'individual' => $individual, |
||
260 | 'family' => null, |
||
261 | 'name_fact' => null, |
||
262 | 'famtag' => $famtag, |
||
263 | 'gender' => $gender, |
||
264 | ]); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @param ServerRequestInterface $request |
||
269 | * |
||
270 | * @return ResponseInterface |
||
271 | */ |
||
272 | public function addSpouseAction(ServerRequestInterface $request): ResponseInterface |
||
273 | { |
||
274 | $tree = $request->getAttribute('tree'); |
||
275 | $xref = $request->getParsedBody()['xref']; |
||
276 | |||
277 | $individual = Individual::getInstance($xref, $tree); |
||
278 | |||
279 | Auth::checkIndividualAccess($individual, true); |
||
280 | |||
281 | $sex = $request->getParsedBody()['SEX']; |
||
282 | |||
283 | $this->glevels = $request->getParsedBody()['glevels'] ?? []; |
||
284 | $this->tag = $request->getParsedBody()['tag'] ?? []; |
||
285 | $this->text = $request->getParsedBody()['text'] ?? []; |
||
286 | $this->islink = $request->getParsedBody()['islink'] ?? []; |
||
287 | |||
288 | $this->splitSource(); |
||
289 | $indi_gedcom = '0 @@ INDI'; |
||
290 | $indi_gedcom .= $this->addNewName($request, $tree); |
||
291 | $indi_gedcom .= $this->addNewSex($request); |
||
292 | if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) { |
||
293 | foreach ($matches[1] as $match) { |
||
294 | $indi_gedcom .= $this->addNewFact($request, $tree, $match); |
||
295 | } |
||
296 | } |
||
297 | if ($request->getParsedBody()['SOUR_INDI'] ?? false) { |
||
298 | $indi_gedcom = $this->handleUpdates($indi_gedcom); |
||
299 | } else { |
||
300 | $indi_gedcom = $this->updateRest($indi_gedcom); |
||
301 | } |
||
302 | |||
303 | $fam_gedcom = ''; |
||
304 | if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) { |
||
305 | foreach ($matches[1] as $match) { |
||
306 | $fam_gedcom .= $this->addNewFact($request, $tree, $match); |
||
307 | } |
||
308 | } |
||
309 | if ($request->getParsedBody()['SOUR_FAM'] ?? false) { |
||
310 | $fam_gedcom = $this->handleUpdates($fam_gedcom); |
||
311 | } else { |
||
312 | $fam_gedcom = $this->updateRest($fam_gedcom); |
||
313 | } |
||
314 | |||
315 | // Create the new spouse |
||
316 | $spouse = $tree->createIndividual($indi_gedcom); |
||
317 | // Create a new family |
||
318 | if ($sex === 'F') { |
||
319 | $family = $tree->createFamily("0 @@ FAM\n1 WIFE @" . $spouse->xref() . "@\n1 HUSB @" . $individual->xref() . '@' . $fam_gedcom); |
||
320 | } else { |
||
321 | $family = $tree->createFamily("0 @@ FAM\n1 HUSB @" . $spouse->xref() . "@\n1 WIFE @" . $individual->xref() . '@' . $fam_gedcom); |
||
322 | } |
||
323 | // Link the spouses to the family |
||
324 | $spouse->createFact('1 FAMS @' . $family->xref() . '@', true); |
||
325 | $individual->createFact('1 FAMS @' . $family->xref() . '@', true); |
||
326 | |||
327 | if (($request->getParsedBody()['goto'] ?? '') === 'new') { |
||
328 | return redirect($spouse->url()); |
||
329 | } |
||
330 | |||
331 | return redirect($individual->url()); |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Add an unlinked individual |
||
336 | * |
||
337 | * @param ServerRequestInterface $request |
||
338 | * |
||
339 | * @return ResponseInterface |
||
340 | */ |
||
341 | public function addUnlinked(ServerRequestInterface $request): ResponseInterface |
||
355 | ]); |
||
356 | } |
||
357 | |||
358 | /** |
||
359 | * @param ServerRequestInterface $request |
||
360 | * |
||
361 | * @return ResponseInterface |
||
362 | */ |
||
363 | public function addUnlinkedAction(ServerRequestInterface $request): ResponseInterface |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Edit a name record. |
||
397 | * |
||
398 | * @param ServerRequestInterface $request |
||
399 | * |
||
400 | * @return ResponseInterface |
||
401 | */ |
||
402 | public function editName(ServerRequestInterface $request): ResponseInterface |
||
403 | { |
||
404 | $tree = $request->getAttribute('tree'); |
||
405 | $fact_id = $request->getQueryParams()['fact_id']; |
||
406 | $xref = $request->getQueryParams()['xref']; |
||
407 | |||
408 | $individual = Individual::getInstance($xref, $tree); |
||
409 | |||
410 | Auth::checkIndividualAccess($individual, true); |
||
411 | |||
412 | // Find the fact to edit |
||
413 | foreach ($individual->facts() as $fact) { |
||
414 | if ($fact->id() === $fact_id && $fact->canEdit()) { |
||
415 | return $this->viewResponse('edit/new-individual', [ |
||
416 | 'next_action' => 'edit-name-action', |
||
417 | 'tree' => $tree, |
||
418 | 'title' => I18N::translate('Edit the name'), |
||
419 | 'individual' => $individual, |
||
420 | 'family' => null, |
||
421 | 'name_fact' => $fact, |
||
422 | 'famtag' => '', |
||
423 | 'gender' => $individual->sex(), |
||
424 | ]); |
||
425 | } |
||
426 | } |
||
427 | |||
428 | throw new NotFoundHttpException(); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * @param ServerRequestInterface $request |
||
433 | * |
||
434 | * @return ResponseInterface |
||
435 | */ |
||
436 | public function editNameAction(ServerRequestInterface $request): ResponseInterface |
||
437 | { |
||
438 | $tree = $request->getAttribute('tree'); |
||
439 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
440 | |||
441 | $xref = $request->getParsedBody()['xref']; |
||
442 | |||
443 | $individual = Individual::getInstance($xref, $tree); |
||
444 | |||
445 | Auth::checkIndividualAccess($individual, true); |
||
446 | |||
447 | // @TODO - Move the name-specific code to this function? |
||
448 | return app(EditGedcomRecordController::class)->updateFact($request, $tree); |
||
|
|||
449 | } |
||
450 | |||
451 | /** |
||
452 | * Add a new name record. |
||
453 | * |
||
454 | * @param ServerRequestInterface $request |
||
455 | * |
||
456 | * @return ResponseInterface |
||
457 | */ |
||
458 | public function addName(ServerRequestInterface $request): ResponseInterface |
||
459 | { |
||
460 | $tree = $request->getAttribute('tree'); |
||
461 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
462 | |||
463 | $xref = $request->getQueryParams()['xref']; |
||
464 | |||
465 | $individual = Individual::getInstance($xref, $tree); |
||
466 | |||
467 | Auth::checkIndividualAccess($individual, true); |
||
468 | |||
469 | $title = $individual->fullName() . ' — ' . I18N::translate('Add a name'); |
||
470 | |||
471 | return $this->viewResponse('edit/new-individual', [ |
||
472 | 'next_action' => 'add-name-action', |
||
473 | 'tree' => $tree, |
||
474 | 'title' => $title, |
||
475 | 'individual' => $individual, |
||
476 | 'family' => null, |
||
477 | 'name_fact' => null, |
||
478 | 'famtag' => '', |
||
479 | 'gender' => $individual->sex(), |
||
480 | ]); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * @param ServerRequestInterface $request |
||
485 | * |
||
486 | * @return ResponseInterface |
||
487 | */ |
||
488 | public function addNameAction(ServerRequestInterface $request): ResponseInterface |
||
489 | { |
||
490 | $tree = $request->getAttribute('tree'); |
||
491 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
492 | |||
493 | $xref = $request->getParsedBody()['xref']; |
||
494 | |||
495 | $individual = Individual::getInstance($xref, $tree); |
||
496 | |||
497 | Auth::checkIndividualAccess($individual, true); |
||
498 | |||
499 | // @TODO - Move the name-specific code to this function? |
||
500 | |||
501 | return app(EditGedcomRecordController::class)->updateFact($request, $tree); |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param ServerRequestInterface $request |
||
506 | * |
||
507 | * @return ResponseInterface |
||
508 | */ |
||
509 | public function linkChildToFamily(ServerRequestInterface $request): ResponseInterface |
||
510 | { |
||
511 | $tree = $request->getAttribute('tree'); |
||
512 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
513 | |||
514 | $xref = $request->getQueryParams()['xref']; |
||
515 | |||
516 | $individual = Individual::getInstance($xref, $tree); |
||
517 | |||
518 | Auth::checkIndividualAccess($individual, true); |
||
519 | |||
520 | $title = $individual->fullName() . ' - ' . I18N::translate('Link this individual to an existing family as a child'); |
||
521 | |||
522 | return $this->viewResponse('edit/link-child-to-family', [ |
||
523 | 'individual' => $individual, |
||
524 | 'title' => $title, |
||
525 | ]); |
||
526 | } |
||
527 | |||
528 | /** |
||
529 | * @param ServerRequestInterface $request |
||
530 | * |
||
531 | * @return ResponseInterface |
||
532 | */ |
||
533 | public function linkChildToFamilyAction(ServerRequestInterface $request): ResponseInterface |
||
534 | { |
||
535 | $tree = $request->getAttribute('tree'); |
||
536 | $xref = $request->getParsedBody()['xref']; |
||
537 | $famid = $request->getParsedBody()['famid']; |
||
538 | $PEDI = $request->getParsedBody()['PEDI']; |
||
539 | |||
540 | $individual = Individual::getInstance($xref, $tree); |
||
541 | Auth::checkIndividualAccess($individual, true); |
||
542 | |||
543 | $family = Family::getInstance($famid, $tree); |
||
544 | Auth::checkFamilyAccess($family, true); |
||
545 | |||
546 | // Replace any existing child->family link (we may be changing the PEDI); |
||
547 | $fact_id = ''; |
||
548 | foreach ($individual->facts(['FAMC']) as $fact) { |
||
549 | if ($family === $fact->target()) { |
||
550 | $fact_id = $fact->id(); |
||
551 | break; |
||
552 | } |
||
553 | } |
||
554 | |||
555 | $gedcom = GedcomCodePedi::createNewFamcPedi($PEDI, $famid); |
||
556 | $individual->updateFact($fact_id, $gedcom, true); |
||
557 | |||
558 | // Only set the family->child link if it does not already exist |
||
559 | $chil_link_exists = false; |
||
560 | foreach ($family->facts(['CHIL']) as $fact) { |
||
561 | if ($individual === $fact->target()) { |
||
562 | $chil_link_exists = true; |
||
563 | break; |
||
564 | } |
||
565 | } |
||
566 | |||
567 | if (!$chil_link_exists) { |
||
568 | $family->createFact('1 CHIL @' . $individual->xref() . '@', true); |
||
569 | } |
||
570 | |||
571 | return redirect($individual->url()); |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * @param ServerRequestInterface $request |
||
576 | * |
||
577 | * @return ResponseInterface |
||
578 | */ |
||
579 | public function linkSpouseToIndividual(ServerRequestInterface $request): ResponseInterface |
||
580 | { |
||
581 | $tree = $request->getAttribute('tree'); |
||
582 | assert($tree instanceof Tree, new InvalidArgumentException()); |
||
583 | |||
584 | $xref = $request->getQueryParams()['xref']; |
||
585 | |||
586 | $individual = Individual::getInstance($xref, $tree); |
||
587 | |||
588 | Auth::checkIndividualAccess($individual, true); |
||
589 | |||
590 | if ($individual->sex() === 'F') { |
||
591 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a husband using an existing individual'); |
||
592 | $label = I18N::translate('Husband'); |
||
593 | } else { |
||
594 | $title = $individual->fullName() . ' - ' . I18N::translate('Add a wife using an existing individual'); |
||
595 | $label = I18N::translate('Wife'); |
||
596 | } |
||
597 | |||
598 | return $this->viewResponse('edit/link-spouse-to-individual', [ |
||
599 | 'individual' => $individual, |
||
600 | 'label' => $label, |
||
601 | 'title' => $title, |
||
602 | ]); |
||
603 | } |
||
604 | |||
605 | /** |
||
606 | * @param ServerRequestInterface $request |
||
607 | * |
||
608 | * @return ResponseInterface |
||
609 | */ |
||
610 | public function linkSpouseToIndividualAction(ServerRequestInterface $request): ResponseInterface |
||
636 | } |
||
637 | } |
||
638 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.