Conditions | 4 |
Paths | 4 |
Total Lines | 96 |
Code Lines | 67 |
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 |
||
114 | * |
||
115 | * @param Individual $individual |
||
116 | * @param string[] $parameters |
||
117 | * |
||
118 | * @return string |
||
119 | */ |
||
120 | public function chartUrl(Individual $individual, array $parameters = []): string |
||
121 | { |
||
122 | return route(self::ROUTE_NAME, [ |
||
123 | 'tree' => $individual->tree()->name(), |
||
124 | ] + $parameters + self::DEFAULT_PARAMETERS); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param ServerRequestInterface $request |
||
129 | * |
||
130 | * @return ResponseInterface |
||
131 | */ |
||
132 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
133 | { |
||
134 | $tree = $request->getAttribute('tree'); |
||
135 | $user = $request->getAttribute('user'); |
||
136 | $scale = (int) $request->getAttribute('scale'); |
||
137 | $xrefs = $request->getQueryParams()['xrefs'] ?? []; |
||
138 | $ajax = $request->getQueryParams()['ajax'] ?? ''; |
||
139 | |||
140 | Auth::checkComponentAccess($this, 'chart', $tree, $user); |
||
141 | |||
142 | $scale = min($scale, self::MAXIMUM_SCALE); |
||
143 | $scale = max($scale, self::MINIMUM_SCALE); |
||
144 | |||
145 | $xrefs = array_unique($xrefs); |
||
146 | |||
147 | // Find the requested individuals. |
||
148 | $individuals = (new Collection($xrefs)) |
||
149 | ->unique() |
||
150 | ->map(static function (string $xref) use ($tree): ?Individual { |
||
151 | return Individual::getInstance($xref, $tree); |
||
152 | }) |
||
153 | ->filter() |
||
154 | ->filter(GedcomRecord::accessFilter()); |
||
155 | |||
156 | // Generate URLs omitting each xref. |
||
157 | $remove_urls = []; |
||
158 | |||
159 | foreach ($individuals as $exclude) { |
||
160 | $xrefs_1 = $individuals |
||
161 | ->filter(static function (Individual $individual) use ($exclude): bool { |
||
162 | return $individual->xref() !== $exclude->xref(); |
||
163 | }) |
||
164 | ->map(static function (Individual $individual): string { |
||
165 | return $individual->xref(); |
||
166 | }); |
||
167 | |||
168 | $remove_urls[$exclude->xref()] = route(self::ROUTE_NAME, [ |
||
169 | 'tree' => $tree->name(), |
||
170 | 'scale' => $scale, |
||
171 | 'xrefs' => $xrefs_1->all(), |
||
172 | ]); |
||
173 | } |
||
174 | |||
175 | $individuals = array_map(static function (string $xref) use ($tree): ?Individual { |
||
176 | return Individual::getInstance($xref, $tree); |
||
177 | }, $xrefs); |
||
178 | |||
179 | $individuals = array_filter($individuals, static function (?Individual $individual): bool { |
||
180 | return $individual instanceof Individual && $individual->canShow(); |
||
181 | }); |
||
182 | |||
183 | // Convert POST requests into GET requests for pretty URLs. |
||
184 | if ($request->getMethod() === RequestMethodInterface::METHOD_POST) { |
||
185 | return redirect(route(self::ROUTE_NAME, [ |
||
186 | 'scale' => $scale, |
||
187 | 'tree' => $tree->name(), |
||
188 | 'xrefs' => $xrefs, |
||
189 | ])); |
||
190 | } |
||
191 | |||
192 | Auth::checkComponentAccess($this, 'chart', $tree, $user); |
||
193 | |||
194 | if ($ajax === '1') { |
||
195 | $this->layout = 'layouts/ajax'; |
||
196 | |||
197 | return $this->chart($tree, $xrefs, $scale); |
||
198 | } |
||
199 | |||
200 | $reset_url = route(self::ROUTE_NAME, [ |
||
201 | 'scale' => self::DEFAULT_SCALE, |
||
202 | 'tree' => $tree->name(), |
||
203 | ]); |
||
204 | |||
205 | $zoom_in_url = route(self::ROUTE_NAME, [ |
||
206 | 'scale' => min(self::MAXIMUM_SCALE, $scale + (int) ($scale * 0.2 + 1)), |
||
207 | 'tree' => $tree->name(), |
||
208 | 'xrefs' => $xrefs, |
||
209 | ]); |
||
210 | |||
332 |