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