| Conditions | 25 |
| Paths | 1650 |
| Total Lines | 103 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 44 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 45 | { |
||
| 46 | $tree = $request->getAttribute('tree'); |
||
| 47 | assert($tree instanceof Tree); |
||
| 48 | |||
| 49 | $params = (array) $request->getParsedBody(); |
||
| 50 | |||
| 51 | $xref = $params['xref']; |
||
| 52 | $family = Factory::family()->make($xref, $tree); |
||
| 53 | $family = Auth::checkFamilyAccess($family, true); |
||
| 54 | |||
| 55 | $params = (array) $request->getParsedBody(); |
||
| 56 | |||
| 57 | $HUSB = $params['HUSB'] ?? ''; |
||
| 58 | $WIFE = $params['WIFE'] ?? ''; |
||
| 59 | $CHIL = $params['CHIL'] ?? []; |
||
| 60 | |||
| 61 | // Current family members |
||
| 62 | $old_father = $family->husband(); |
||
| 63 | $old_mother = $family->wife(); |
||
| 64 | $old_children = $family->children(); |
||
| 65 | |||
| 66 | // New family members |
||
| 67 | $new_father = Factory::individual()->make($HUSB, $tree); |
||
| 68 | $new_mother = Factory::individual()->make($WIFE, $tree); |
||
| 69 | $new_children = []; |
||
| 70 | foreach ($CHIL as $child) { |
||
| 71 | $new_children[] = Factory::individual()->make($child, $tree); |
||
| 72 | } |
||
| 73 | |||
| 74 | if ($old_father !== $new_father) { |
||
| 75 | if ($old_father instanceof Individual) { |
||
| 76 | // Remove old FAMS link |
||
| 77 | foreach ($old_father->facts(['FAMS']) as $fact) { |
||
| 78 | if ($fact->target() === $family) { |
||
| 79 | $old_father->deleteFact($fact->id(), true); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | // Remove old HUSB link |
||
| 83 | foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { |
||
| 84 | if ($fact->target() === $old_father) { |
||
| 85 | $family->deleteFact($fact->id(), true); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } |
||
| 89 | if ($new_father instanceof Individual) { |
||
| 90 | // Add new FAMS link |
||
| 91 | $new_father->createFact('1 FAMS @' . $family->xref() . '@', true); |
||
| 92 | // Add new HUSB link |
||
| 93 | $family->createFact('1 HUSB @' . $new_father->xref() . '@', true); |
||
| 94 | } |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($old_mother !== $new_mother) { |
||
| 98 | if ($old_mother instanceof Individual) { |
||
| 99 | // Remove old FAMS link |
||
| 100 | foreach ($old_mother->facts(['FAMS']) as $fact) { |
||
| 101 | if ($fact->target() === $family) { |
||
| 102 | $old_mother->deleteFact($fact->id(), true); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | // Remove old WIFE link |
||
| 106 | foreach ($family->facts(['HUSB', 'WIFE']) as $fact) { |
||
| 107 | if ($fact->target() === $old_mother) { |
||
| 108 | $family->deleteFact($fact->id(), true); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | if ($new_mother instanceof Individual) { |
||
| 113 | // Add new FAMS link |
||
| 114 | $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true); |
||
| 115 | // Add new WIFE link |
||
| 116 | $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true); |
||
| 117 | } |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach ($old_children as $old_child) { |
||
| 121 | if (!in_array($old_child, $new_children, true)) { |
||
| 122 | // Remove old FAMC link |
||
| 123 | foreach ($old_child->facts(['FAMC']) as $fact) { |
||
| 124 | if ($fact->target() === $family) { |
||
| 125 | $old_child->deleteFact($fact->id(), true); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | // Remove old CHIL link |
||
| 129 | foreach ($family->facts(['CHIL']) as $fact) { |
||
| 130 | if ($fact->target() === $old_child) { |
||
| 131 | $family->deleteFact($fact->id(), true); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | foreach ($new_children as $new_child) { |
||
| 138 | if ($new_child instanceof Individual && !$old_children->contains($new_child)) { |
||
| 139 | // Add new FAMC link |
||
| 140 | $new_child->createFact('1 FAMC @' . $family->xref() . '@', true); |
||
| 141 | // Add new CHIL link |
||
| 142 | $family->createFact('1 CHIL @' . $new_child->xref() . '@', true); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | return redirect($family->url()); |
||
| 147 | } |
||
| 149 |