| Total Complexity | 70 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CheckTree 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 CheckTree, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 72 | final class CheckTree implements RequestHandlerInterface |
||
| 73 | { |
||
| 74 | use ViewResponseTrait; |
||
| 75 | |||
| 76 | public function __construct( |
||
| 77 | private readonly Gedcom $gedcom, |
||
| 78 | private readonly TimeoutService $timeout_service, |
||
| 79 | ) { |
||
| 80 | } |
||
| 81 | |||
| 82 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 83 | { |
||
| 84 | $this->layout = 'layouts/administration'; |
||
| 85 | |||
| 86 | $tree = Validator::attributes($request)->tree(); |
||
| 87 | $skip_to = Validator::queryParams($request)->string('skip_to', ''); |
||
| 88 | |||
| 89 | // We need to work with raw GEDCOM data, as we are looking for errors |
||
| 90 | // which may prevent the GedcomRecord objects from working. |
||
| 91 | |||
| 92 | $q1 = DB::table('individuals') |
||
| 93 | ->where('i_file', '=', $tree->id()) |
||
| 94 | ->select(['i_id AS xref', 'i_gedcom AS gedcom', new Expression("'INDI' AS type")]); |
||
| 95 | $q2 = DB::table('families') |
||
| 96 | ->where('f_file', '=', $tree->id()) |
||
| 97 | ->select(['f_id AS xref', 'f_gedcom AS gedcom', new Expression("'FAM' AS type")]); |
||
| 98 | $q3 = DB::table('media') |
||
| 99 | ->where('m_file', '=', $tree->id()) |
||
| 100 | ->select(['m_id AS xref', 'm_gedcom AS gedcom', new Expression("'OBJE' AS type")]); |
||
| 101 | $q4 = DB::table('sources') |
||
| 102 | ->where('s_file', '=', $tree->id()) |
||
| 103 | ->select(['s_id AS xref', 's_gedcom AS gedcom', new Expression("'SOUR' AS type")]); |
||
| 104 | $q5 = DB::table('other') |
||
| 105 | ->where('o_file', '=', $tree->id()) |
||
| 106 | ->select(['o_id AS xref', 'o_gedcom AS gedcom', 'o_type']); |
||
| 107 | $q6 = DB::table('change') |
||
| 108 | ->where('gedcom_id', '=', $tree->id()) |
||
| 109 | ->where('status', '=', 'pending') |
||
| 110 | ->orderBy('change_id') |
||
| 111 | ->select(['xref', 'new_gedcom AS gedcom', new Expression("'' AS type")]); |
||
| 112 | |||
| 113 | $rows = $q1 |
||
| 114 | ->unionAll($q2) |
||
| 115 | ->unionAll($q3) |
||
| 116 | ->unionAll($q4) |
||
| 117 | ->unionAll($q5) |
||
| 118 | ->unionAll($q6) |
||
| 119 | ->get() |
||
| 120 | ->map(static function (object $row): object { |
||
| 121 | // Extract type for pending record |
||
| 122 | if ($row->type === '' && str_starts_with($row->gedcom, '0 HEAD')) { |
||
| 123 | $row->type = 'HEAD'; |
||
| 124 | } |
||
| 125 | |||
| 126 | if ($row->type === '' && preg_match('/^0 @[^@]*@ ([_A-Z0-9]+)/', $row->gedcom, $match) === 1) { |
||
| 127 | $row->type = $match[1]; |
||
| 128 | } |
||
| 129 | |||
| 130 | return $row; |
||
| 131 | }); |
||
| 132 | |||
| 133 | $records = []; |
||
| 134 | $xrefs = []; |
||
| 135 | |||
| 136 | foreach ($rows as $row) { |
||
| 137 | if ($row->gedcom !== '') { |
||
| 138 | // existing or updated record |
||
| 139 | $records[$row->xref] = $row; |
||
| 140 | } else { |
||
| 141 | // deleted record |
||
| 142 | unset($records[$row->xref]); |
||
| 143 | } |
||
| 144 | |||
| 145 | $xrefs[strtoupper($row->xref)] = $row->xref; |
||
| 146 | } |
||
| 147 | |||
| 148 | unset($rows); |
||
| 149 | |||
| 150 | $errors = []; |
||
| 151 | $warnings = []; |
||
| 152 | $infos = []; |
||
| 153 | |||
| 154 | $element_factory = new ElementFactory(); |
||
| 155 | $this->gedcom->registerTags($element_factory, false); |
||
| 156 | |||
| 157 | foreach ($records as $record) { |
||
| 158 | // If we are nearly out of time, then stop processing here |
||
| 159 | if ($skip_to === $record->xref) { |
||
| 160 | $skip_to = ''; |
||
| 161 | } elseif ($skip_to !== '') { |
||
| 162 | continue; |
||
| 163 | } elseif ($this->timeout_service->isTimeNearlyUp()) { |
||
| 164 | $skip_to = $record->xref; |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | |||
| 168 | $lines = explode("\n", $record->gedcom); |
||
| 169 | array_shift($lines); |
||
| 170 | |||
| 171 | $last_level = 0; |
||
| 172 | $hierarchy = [$record->type]; |
||
| 173 | |||
| 174 | foreach ($lines as $line_number => $line) { |
||
| 175 | if (preg_match('/^(\d+) (\w+) ?(.*)/', $line, $match) !== 1) { |
||
| 176 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM record.'), ''); |
||
| 177 | break; |
||
| 178 | } |
||
| 179 | |||
| 180 | $level = (int) $match[1]; |
||
| 181 | if ($level > $last_level + 1) { |
||
| 182 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM level number.'), ''); |
||
| 183 | break; |
||
| 184 | } |
||
| 185 | |||
| 186 | $tag = $match[2]; |
||
| 187 | $value = $match[3]; |
||
| 188 | $hierarchy[$level] = $tag; |
||
| 189 | $full_tag = implode(':', array_slice($hierarchy, 0, 1 + $level)); |
||
| 190 | $element = $element_factory->make($full_tag); |
||
| 191 | $last_level = $level; |
||
| 192 | |||
| 193 | if ($tag === 'CONT') { |
||
| 194 | $element = new SubmitterText('CONT'); |
||
| 195 | } |
||
| 196 | |||
| 197 | if ($element instanceof UnknownElement) { |
||
| 198 | if (str_starts_with($tag, '_') || str_starts_with($full_tag, '_') || str_contains($full_tag, ':_')) { |
||
| 199 | $message = I18N::translate('Custom GEDCOM tags are discouraged. Try to use only standard GEDCOM tags.'); |
||
| 200 | $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag); |
||
| 201 | } else { |
||
| 202 | $message = I18N::translate('Invalid GEDCOM tag.') . ' ' . $full_tag; |
||
| 203 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag); |
||
| 204 | } |
||
| 205 | } elseif ($element instanceof AbstractXrefElement) { |
||
| 206 | if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $value, $match) === 1) { |
||
| 207 | $xref1 = $match[1]; |
||
| 208 | $xref2 = $xrefs[strtoupper($xref1)] ?? null; |
||
| 209 | $linked = $records[$xref2] ?? null; |
||
| 210 | |||
| 211 | if ($linked === null) { |
||
| 212 | $message = I18N::translate('%s does not exist.', e($xref1)); |
||
| 213 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $tag . '-' . $xref1); |
||
| 214 | } elseif ($element instanceof XrefFamily && $linked->type !== Family::RECORD_TYPE) { |
||
| 215 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Family::RECORD_TYPE); |
||
| 216 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 217 | } elseif ($element instanceof XrefIndividual && $linked->type !== Individual::RECORD_TYPE) { |
||
| 218 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Individual::RECORD_TYPE); |
||
| 219 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 220 | } elseif ($element instanceof XrefMedia && $linked->type !== Media::RECORD_TYPE) { |
||
| 221 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Media::RECORD_TYPE); |
||
| 222 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 223 | } elseif ($element instanceof XrefNote && $linked->type !== Note::RECORD_TYPE) { |
||
| 224 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Note::RECORD_TYPE); |
||
| 225 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 226 | } elseif ($element instanceof XrefSource && $linked->type !== Source::RECORD_TYPE) { |
||
| 227 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Source::RECORD_TYPE); |
||
| 228 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 229 | } elseif ($element instanceof XrefRepository && $linked->type !== Repository::RECORD_TYPE) { |
||
| 230 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Repository::RECORD_TYPE); |
||
| 231 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 232 | } elseif ($element instanceof XrefSubmitter && $linked->type !== Submitter::RECORD_TYPE) { |
||
| 233 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Submitter::RECORD_TYPE); |
||
| 234 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 235 | } elseif ($element instanceof XrefSubmission && $linked->type !== Submission::RECORD_TYPE) { |
||
| 236 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Submission::RECORD_TYPE); |
||
| 237 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 238 | } elseif ($element instanceof XrefLocation && $linked->type !== Location::RECORD_TYPE) { |
||
| 239 | $message = $this->linkErrorMessage($tree, $xref1, $linked->type, Location::RECORD_TYPE); |
||
| 240 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type'); |
||
| 241 | } elseif (($full_tag === 'FAM:HUSB' || $full_tag === 'FAM:WIFE') && !str_contains($linked->gedcom, "\n1 FAMS @" . $record->xref . '@')) { |
||
| 242 | $link1 = $this->recordLink($tree, $linked->xref); |
||
| 243 | $link2 = $this->recordLink($tree, $record->xref); |
||
| 244 | $message = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2); |
||
| 245 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-FAMS'); |
||
| 246 | } elseif ($full_tag === 'FAM:CHIL' && !str_contains($linked->gedcom, "\n1 FAMC @" . $record->xref . '@')) { |
||
| 247 | $link1 = $this->recordLink($tree, $linked->xref); |
||
| 248 | $link2 = $this->recordLink($tree, $record->xref); |
||
| 249 | $message = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2); |
||
| 250 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-FAMC'); |
||
| 251 | } elseif ($full_tag === 'INDI:FAMC' && !str_contains($linked->gedcom, "\n1 CHIL @" . $record->xref . '@')) { |
||
| 252 | $link1 = $this->recordLink($tree, $linked->xref); |
||
| 253 | $link2 = $this->recordLink($tree, $record->xref); |
||
| 254 | $message = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2); |
||
| 255 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-CHIL'); |
||
| 256 | } elseif ($full_tag === 'INDI:FAMS' && !str_contains($linked->gedcom, "\n1 HUSB @" . $record->xref . '@') && !str_contains($linked->gedcom, "\n1 WIFE @" . $record->xref . '@')) { |
||
| 257 | $link1 = $this->recordLink($tree, $linked->xref); |
||
| 258 | $link2 = $this->recordLink($tree, $record->xref); |
||
| 259 | $message = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2); |
||
| 260 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-HUSB-WIFE'); |
||
| 261 | } elseif ($xref1 !== $xref2) { |
||
| 262 | $message = I18N::translate('%1$s does not exist. Did you mean %2$s?', e($xref1), e($xref2)); |
||
| 263 | $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $tag . '-' . $xref1); |
||
| 264 | } |
||
| 265 | } elseif ($tag === 'SOUR') { |
||
| 266 | $message = I18N::translate('Inline-source records are discouraged.'); |
||
| 267 | $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-inline'); |
||
| 268 | } else { |
||
| 269 | $message = I18N::translate('Invalid GEDCOM value.'); |
||
| 270 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-value-' . e($value)); |
||
| 271 | } |
||
| 272 | } elseif ($element->canonical($value) !== $value) { |
||
| 273 | $expected = e($element->canonical($value)); |
||
| 274 | $actual = strtr(e($value), ["\t" => '→']); |
||
| 275 | $message = I18N::translate('“%1$s” should be “%2$s”.', $actual, $expected); |
||
| 276 | if (strtoupper($element->canonical($value)) !== strtoupper($value)) { |
||
| 277 | // This will be relevant for GEDCOM 7.0. It's not relevant now, and causes confusion. |
||
| 278 | $infos[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-value'); |
||
| 279 | } |
||
| 280 | } elseif ($element instanceof MultimediaFormat) { |
||
| 281 | $mime = Mime::TYPES[$value] ?? Mime::DEFAULT_TYPE; |
||
| 282 | |||
| 283 | if ($mime === Mime::DEFAULT_TYPE) { |
||
| 284 | $message = I18N::translate('webtrees does not recognise this file format.'); |
||
| 285 | $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-' . e($value)); |
||
| 286 | } elseif (str_starts_with($mime, 'image/') && !array_key_exists($mime, ImageFactory::SUPPORTED_FORMATS)) { |
||
| 287 | $message = I18N::translate('webtrees cannot create thumbnails for this file format.'); |
||
| 288 | $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-' . e($value)); |
||
| 289 | } |
||
| 290 | } elseif ($element instanceof MultimediaFileReference && $value === 'gedcom.ged') { |
||
| 291 | $message = I18N::translate('This filename is not compatible with the GEDZIP file format.'); |
||
| 292 | $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '_' . e($value)); |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | if ($record->type === Family::RECORD_TYPE) { |
||
| 297 | if (substr_count($record->gedcom, "\n1 HUSB @") > 1) { |
||
| 298 | $message = I18N::translate('%s occurs too many times.', 'FAM:HUSB'); |
||
| 299 | $errors[] = $this->recordError($tree, $record->type, $record->xref, $message, 'FAM:HUSB-count'); |
||
| 300 | } |
||
| 301 | if (substr_count($record->gedcom, "\n1 WIFE @") > 1) { |
||
| 302 | $message = I18N::translate('%s occurs too many times.', 'FAM:WIFE'); |
||
| 303 | $errors[] = $this->recordError($tree, $record->type, $record->xref, $message, 'FAM:WIFE-count'); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | |||
| 308 | $title = I18N::translate('Check for errors') . ' — ' . e($tree->title()); |
||
| 309 | |||
| 310 | if ($skip_to === '') { |
||
| 311 | $more_url = ''; |
||
| 312 | } else { |
||
| 313 | $more_url = route(self::class, ['tree' => $tree->name(), 'skip_to' => $skip_to]); |
||
| 314 | } |
||
| 315 | |||
| 316 | return $this->viewResponse('admin/trees-check', [ |
||
| 317 | 'errors' => $errors, |
||
| 318 | 'infos' => $infos, |
||
| 319 | 'more_url' => $more_url, |
||
| 320 | 'title' => $title, |
||
| 321 | 'tree' => $tree, |
||
| 322 | 'warnings' => $warnings, |
||
| 323 | ]); |
||
| 324 | } |
||
| 325 | |||
| 326 | private function recordType(string $type): string |
||
| 327 | { |
||
| 328 | $types = [ |
||
| 329 | Family::RECORD_TYPE => I18N::translate('Family'), |
||
| 330 | Header::RECORD_TYPE => I18N::translate('Header'), |
||
| 331 | Individual::RECORD_TYPE => I18N::translate('Individual'), |
||
| 332 | Location::RECORD_TYPE => I18N::translate('Location'), |
||
| 333 | Media::RECORD_TYPE => I18N::translate('Media object'), |
||
| 334 | Note::RECORD_TYPE => I18N::translate('Note'), |
||
| 335 | Repository::RECORD_TYPE => I18N::translate('Repository'), |
||
| 336 | Source::RECORD_TYPE => I18N::translate('Source'), |
||
| 337 | Submission::RECORD_TYPE => I18N::translate('Submission'), |
||
| 338 | Submitter::RECORD_TYPE => I18N::translate('Submitter'), |
||
| 339 | ]; |
||
| 340 | |||
| 341 | return $types[$type] ?? e($type); |
||
| 342 | } |
||
| 343 | |||
| 344 | private function recordLink(Tree $tree, string $xref): string |
||
| 349 | } |
||
| 350 | |||
| 351 | private function linkErrorMessage(Tree $tree, string $xref, string $type1, string $type2): string |
||
| 352 | { |
||
| 353 | $link = $this->recordLink($tree, $xref); |
||
| 354 | $type1 = $this->recordType($type1); |
||
| 355 | $type2 = $this->recordType($type2); |
||
| 356 | |||
| 357 | return I18N::translate('%1$s is a %2$s but a %3$s is expected.', $link, $type1, $type2); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Format a link to a record. |
||
| 362 | * |
||
| 363 | * @return object{message:string,tag:string} |
||
| 364 | */ |
||
| 365 | private function lineError( |
||
| 366 | Tree $tree, |
||
| 367 | string $type, |
||
| 368 | string $xref, |
||
| 369 | int $line_number, |
||
| 370 | string $line, |
||
| 371 | string $message, |
||
| 372 | string $tag |
||
| 373 | ): object { |
||
| 374 | $message = |
||
| 375 | I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) . |
||
| 376 | ' — ' . |
||
| 377 | I18N::translate('%1$s: %2$s', I18N::translate('Line number'), I18N::number($line_number)) . |
||
| 378 | ' — ' . |
||
| 379 | '<code>' . e($line) . '</code>' . |
||
| 380 | '<br>' . $message; |
||
| 381 | |||
| 382 | return (object) [ |
||
| 383 | 'message' => $message, |
||
| 384 | 'tag' => $tag, |
||
| 385 | ]; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Format a link to a record. |
||
| 390 | * |
||
| 391 | * @return object{message:string,tag:string} |
||
| 392 | */ |
||
| 393 | private function recordError(Tree $tree, string $type, string $xref, string $message, string $tag): object |
||
| 400 | ]; |
||
| 401 | } |
||
| 402 | } |
||
| 403 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths