| Conditions | 5 |
| Paths | 6 |
| Total Lines | 102 |
| Code Lines | 66 |
| 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 |
||
| 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 | |||
| 211 | $zoom_out_url = route(self::ROUTE_NAME, [ |
||
| 212 | 'scale' => max(self::MINIMUM_SCALE, $scale - (int) ($scale * 0.2 + 1)), |
||
| 213 | 'tree' => $tree->name(), |
||
| 214 | 'xrefs' => $xrefs, |
||
| 215 | ]); |
||
| 216 | |||
| 217 | $ajax_url = route(self::ROUTE_NAME, [ |
||
| 218 | 'ajax' => true, |
||
| 219 | 'scale' => $scale, |
||
| 220 | 'tree' => $tree->name(), |
||
| 221 | 'xrefs' => $xrefs, |
||
| 222 | ]); |
||
| 223 | |||
| 224 | return $this->viewResponse('modules/timeline-chart/page', [ |
||
| 225 | 'ajax_url' => $ajax_url, |
||
| 226 | 'individuals' => $individuals, |
||
| 227 | 'module' => $this->name(), |
||
| 228 | 'remove_urls' => $remove_urls, |
||
| 229 | 'reset_url' => $reset_url, |
||
| 230 | 'scale' => $scale, |
||
| 231 | 'title' => $this->title(), |
||
| 232 | 'zoom_in_url' => $zoom_in_url, |
||
| 233 | 'zoom_out_url' => $zoom_out_url, |
||
| 234 | ]); |
||
| 332 |