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