| Total Complexity | 47 |
| Total Lines | 319 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like EditFamilyController 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 EditFamilyController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class EditFamilyController extends AbstractEditController |
||
| 35 | { |
||
| 36 | /** |
||
| 37 | * @param ServerRequestInterface $request |
||
| 38 | * |
||
| 39 | * @return ResponseInterface |
||
| 40 | */ |
||
| 41 | public function addChild(ServerRequestInterface $request): ResponseInterface |
||
| 42 | { |
||
| 43 | $tree = $request->getAttribute('tree'); |
||
| 44 | $xref = $request->getQueryParams()['xref']; |
||
| 45 | $gender = $request->getQueryParams()['gender']; |
||
| 46 | $family = Family::getInstance($xref, $tree); |
||
| 47 | |||
| 48 | Auth::checkFamilyAccess($family, true); |
||
| 49 | |||
| 50 | $title = $family->fullName() . ' - ' . I18N::translate('Add a child'); |
||
| 51 | |||
| 52 | return $this->viewResponse('edit/new-individual', [ |
||
| 53 | 'next_action' => 'add-child-to-family-action', |
||
| 54 | 'tree' => $tree, |
||
| 55 | 'title' => $title, |
||
| 56 | 'individual' => null, |
||
| 57 | 'family' => $family, |
||
| 58 | 'name_fact' => null, |
||
| 59 | 'famtag' => 'CHIL', |
||
| 60 | 'gender' => $gender, |
||
| 61 | ]); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param ServerRequestInterface $request |
||
| 66 | * |
||
| 67 | * @return ResponseInterface |
||
| 68 | */ |
||
| 69 | public function addChildAction(ServerRequestInterface $request): ResponseInterface |
||
| 70 | { |
||
| 71 | $tree = $request->getAttribute('tree'); |
||
| 72 | $xref = $request->getParsedBody()['xref']; |
||
| 73 | |||
| 74 | $family = Family::getInstance($xref, $tree); |
||
| 75 | |||
| 76 | Auth::checkFamilyAccess($family, true); |
||
| 77 | |||
| 78 | $PEDI = $request->getParsedBody()['PEDI']; |
||
| 79 | $keep_chan = (bool) ($request->getParsedBody()['keep_chan'] ?? false); |
||
| 80 | |||
| 81 | $this->glevels = $request->getParsedBody()['glevels'] ?? []; |
||
| 82 | $this->tag = $request->getParsedBody()['tag'] ?? []; |
||
| 83 | $this->text = $request->getParsedBody()['text'] ?? []; |
||
| 84 | $this->islink = $request->getParsedBody()['islink'] ?? []; |
||
| 85 | |||
| 86 | $this->splitSource(); |
||
| 87 | $gedrec = '0 @@ INDI'; |
||
| 88 | $gedrec .= $this->addNewName($request, $tree); |
||
| 89 | $gedrec .= $this->addNewSex($request); |
||
| 90 | if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) { |
||
| 91 | foreach ($matches[1] as $match) { |
||
| 92 | $gedrec .= $this->addNewFact($request, $tree, $match); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | $gedrec .= "\n" . GedcomCodePedi::createNewFamcPedi($PEDI, $xref); |
||
| 96 | if ($request->getParsedBody()['SOUR_INDI'] ?? false) { |
||
| 97 | $gedrec = $this->handleUpdates($gedrec); |
||
| 98 | } else { |
||
| 99 | $gedrec = $this->updateRest($gedrec); |
||
| 100 | } |
||
| 101 | |||
| 102 | // Create the new child |
||
| 103 | $new_child = $tree->createIndividual($gedrec); |
||
| 104 | |||
| 105 | // Insert new child at the right place |
||
| 106 | $done = false; |
||
| 107 | foreach ($family->facts(['CHIL']) as $fact) { |
||
| 108 | $old_child = $fact->target(); |
||
| 109 | if ($old_child instanceof Individual && Date::compare($new_child->getEstimatedBirthDate(), $old_child->getEstimatedBirthDate()) < 0) { |
||
| 110 | // Insert before this child |
||
| 111 | $family->updateFact($fact->id(), '1 CHIL @' . $new_child->xref() . "@\n" . $fact->gedcom(), !$keep_chan); |
||
| 112 | $done = true; |
||
| 113 | break; |
||
| 114 | } |
||
| 115 | } |
||
| 116 | if (!$done) { |
||
| 117 | // Append child at end |
||
| 118 | $family->createFact('1 CHIL @' . $new_child->xref() . '@', !$keep_chan); |
||
| 119 | } |
||
| 120 | |||
| 121 | if (($request->getParsedBody()['goto'] ?? '') === 'new') { |
||
| 122 | return redirect($new_child->url()); |
||
| 123 | } |
||
| 124 | |||
| 125 | return redirect($family->url()); |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @param ServerRequestInterface $request |
||
| 130 | * |
||
| 131 | * @return ResponseInterface |
||
| 132 | */ |
||
| 133 | public function addSpouse(ServerRequestInterface $request): ResponseInterface |
||
| 159 | ]); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param ServerRequestInterface $request |
||
| 164 | * |
||
| 165 | * @return ResponseInterface |
||
| 166 | */ |
||
| 167 | public function addSpouseAction(ServerRequestInterface $request): ResponseInterface |
||
| 168 | { |
||
| 169 | $tree = $request->getAttribute('tree'); |
||
| 170 | $xref = $request->getParsedBody()['xref']; |
||
| 171 | |||
| 172 | $family = Family::getInstance($xref, $tree); |
||
| 173 | |||
| 174 | Auth::checkFamilyAccess($family, true); |
||
| 175 | |||
| 176 | $this->glevels = $request->getParsedBody()['glevels'] ?? []; |
||
| 177 | $this->tag = $request->getParsedBody()['tag'] ?? []; |
||
| 178 | $this->text = $request->getParsedBody()['text'] ?? []; |
||
| 179 | $this->islink = $request->getParsedBody()['islink'] ?? []; |
||
| 180 | |||
| 181 | // Create the new spouse |
||
| 182 | $this->splitSource(); // separate SOUR record from the rest |
||
| 183 | |||
| 184 | $gedrec = '0 @@ INDI'; |
||
| 185 | $gedrec .= $this->addNewName($request, $tree); |
||
| 186 | $gedrec .= $this->addNewSex($request); |
||
| 187 | if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) { |
||
| 188 | foreach ($matches[1] as $match) { |
||
| 189 | $gedrec .= $this->addNewFact($request, $tree, $match); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | if ($request->getParsedBody()['SOUR_INDI'] ?? false) { |
||
| 194 | $gedrec = $this->handleUpdates($gedrec); |
||
| 195 | } else { |
||
| 196 | $gedrec = $this->updateRest($gedrec); |
||
| 197 | } |
||
| 198 | $gedrec .= "\n1 FAMS @" . $family->xref() . '@'; |
||
| 199 | $spouse = $tree->createIndividual($gedrec); |
||
| 200 | |||
| 201 | // Update the existing family - add marriage, etc |
||
| 202 | if ($family->facts(['HUSB'])->first() instanceof Fact) { |
||
| 203 | $family->createFact('1 WIFE @' . $spouse->xref() . '@', true); |
||
| 204 | } else { |
||
| 205 | $family->createFact('1 HUSB @' . $spouse->xref() . '@', true); |
||
| 206 | } |
||
| 207 | $famrec = ''; |
||
| 208 | if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'), $matches)) { |
||
| 209 | foreach ($matches[1] as $match) { |
||
| 210 | $famrec .= $this->addNewFact($request, $tree, $match); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | if ($request->getParsedBody()['SOUR_FAM'] ?? false) { |
||
| 214 | $famrec = $this->handleUpdates($famrec); |
||
| 215 | } else { |
||
| 216 | $famrec = $this->updateRest($famrec); |
||
| 217 | } |
||
| 218 | $family->createFact(trim($famrec), true); // trim leading \n |
||
| 219 | |||
| 220 | if (($request->getParsedBody()['goto'] ?? '') === 'new') { |
||
| 221 | return redirect($spouse->url()); |
||
| 222 | } |
||
| 223 | |||
| 224 | return redirect($family->url()); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @param ServerRequestInterface $request |
||
| 229 | * |
||
| 230 | * @return ResponseInterface |
||
| 231 | */ |
||
| 232 | public function changeFamilyMembers(ServerRequestInterface $request): ResponseInterface |
||
| 233 | { |
||
| 234 | $tree = $request->getAttribute('tree'); |
||
| 235 | $xref = $request->getQueryParams()['xref']; |
||
| 236 | $family = Family::getInstance($xref, $tree); |
||
| 237 | Auth::checkFamilyAccess($family, true); |
||
| 238 | |||
| 239 | $title = I18N::translate('Change family members') . ' – ' . $family->fullName(); |
||
| 240 | |||
| 241 | return $this->viewResponse('edit/change-family-members', [ |
||
| 242 | 'tree' => $tree, |
||
| 243 | 'title' => $title, |
||
| 244 | 'family' => $family, |
||
| 245 | 'father' => $family->husband(), |
||
| 246 | 'mother' => $family->wife(), |
||
| 247 | 'children' => $family->children(), |
||
| 248 | ]); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param ServerRequestInterface $request |
||
| 253 | * |
||
| 254 | * @return ResponseInterface |
||
| 255 | */ |
||
| 256 | public function changeFamilyMembersAction(ServerRequestInterface $request): ResponseInterface |
||
| 353 | } |
||
| 354 | } |
||
| 355 |