| Total Complexity | 45 | 
| Total Lines | 414 | 
| Duplicated Lines | 0 % | 
| Changes | 1 | ||
| Bugs | 0 | Features | 0 | 
Complex classes like GedcomExportService 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 GedcomExportService, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 73 | class GedcomExportService  | 
            ||
| 74 | { | 
            ||
| 75 | private const ACCESS_LEVELS = [  | 
            ||
| 76 | 'gedadmin' => Auth::PRIV_NONE,  | 
            ||
| 77 | 'user' => Auth::PRIV_USER,  | 
            ||
| 78 | 'visitor' => Auth::PRIV_PRIVATE,  | 
            ||
| 79 | 'none' => Auth::PRIV_HIDE,  | 
            ||
| 80 | ];  | 
            ||
| 81 | |||
| 82 | private ResponseFactoryInterface $response_factory;  | 
            ||
| 83 | |||
| 84 | private StreamFactoryInterface $stream_factory;  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * @param ResponseFactoryInterface $response_factory  | 
            ||
| 88 | * @param StreamFactoryInterface $stream_factory  | 
            ||
| 89 | */  | 
            ||
| 90 | public function __construct(ResponseFactoryInterface $response_factory, StreamFactoryInterface $stream_factory)  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * @param Tree $tree - Export data from this tree  | 
            ||
| 98 | * @param bool $sort_by_xref - Write GEDCOM records in XREF order  | 
            ||
| 99 | * @param string $encoding - Convert from UTF-8 to other encoding  | 
            ||
| 100 | * @param string $privacy - Filter records by role  | 
            ||
| 101 | * @param string $line_endings  | 
            ||
| 102 | * @param string $filename - Name of download file, without an extension  | 
            ||
| 103 | * @param string $format - One of: gedcom, zip, zipmedia, gedzip  | 
            ||
| 104 | * @param Collection|null $records  | 
            ||
| 105 | *  | 
            ||
| 106 | * @return ResponseInterface  | 
            ||
| 107 | */  | 
            ||
| 108 | public function downloadResponse(  | 
            ||
| 163 | }  | 
            ||
| 164 | |||
| 165 | /**  | 
            ||
| 166 | * Write GEDCOM data to a stream.  | 
            ||
| 167 | *  | 
            ||
| 168 | * @param Tree $tree - Export data from this tree  | 
            ||
| 169 | * @param bool $sort_by_xref - Write GEDCOM records in XREF order  | 
            ||
| 170 | * @param string $encoding - Convert from UTF-8 to other encoding  | 
            ||
| 171 | * @param int $access_level - Apply privacy filtering  | 
            ||
| 172 | * @param string $line_endings - CRLF or LF  | 
            ||
| 173 | * @param Collection<int,string>|null $records - Just export these records  | 
            ||
| 174 | * @param FilesystemOperator|null $zip_filesystem - Write media files to this filesystem  | 
            ||
| 175 | * @param string|null $media_path - Location within the zip filesystem  | 
            ||
| 176 | *  | 
            ||
| 177 | * @return resource  | 
            ||
| 178 | */  | 
            ||
| 179 | public function export(  | 
            ||
| 280 | }  | 
            ||
| 281 | |||
| 282 | /**  | 
            ||
| 283 | * Create a header record for a gedcom file.  | 
            ||
| 284 | *  | 
            ||
| 285 | * @param Tree $tree  | 
            ||
| 286 | * @param string $encoding  | 
            ||
| 287 | * @param bool $include_sub  | 
            ||
| 288 | *  | 
            ||
| 289 | * @return string  | 
            ||
| 290 | */  | 
            ||
| 291 | public function createHeader(Tree $tree, string $encoding, bool $include_sub): string  | 
            ||
| 292 |     { | 
            ||
| 293 | // Force a ".ged" suffix  | 
            ||
| 294 | $filename = $tree->name();  | 
            ||
| 295 | |||
| 296 |         if (strtolower(pathinfo($filename, PATHINFO_EXTENSION)) !== 'ged') { | 
            ||
| 297 | $filename .= '.ged';  | 
            ||
| 298 | }  | 
            ||
| 299 | |||
| 300 | $gedcom_encodings = [  | 
            ||
| 301 | UTF16BE::NAME => 'UNICODE',  | 
            ||
| 302 | UTF16LE::NAME => 'UNICODE',  | 
            ||
| 303 | Windows1252::NAME => 'ANSI',  | 
            ||
| 304 | ];  | 
            ||
| 305 | |||
| 306 | $encoding = $gedcom_encodings[$encoding] ?? $encoding;  | 
            ||
| 307 | |||
| 308 | // Build a new header record  | 
            ||
| 309 | $gedcom = '0 HEAD';  | 
            ||
| 310 | $gedcom .= "\n1 SOUR " . Webtrees::NAME;  | 
            ||
| 311 | $gedcom .= "\n2 NAME " . Webtrees::NAME;  | 
            ||
| 312 | $gedcom .= "\n2 VERS " . Webtrees::VERSION;  | 
            ||
| 313 | $gedcom .= "\n1 DEST DISKETTE";  | 
            ||
| 314 |         $gedcom .= "\n1 DATE " . strtoupper(date('d M Y')); | 
            ||
| 315 |         $gedcom .= "\n2 TIME " . date('H:i:s'); | 
            ||
| 316 | $gedcom .= "\n1 GEDC\n2 VERS 5.5.1\n2 FORM LINEAGE-LINKED";  | 
            ||
| 317 | $gedcom .= "\n1 CHAR " . $encoding;  | 
            ||
| 318 | $gedcom .= "\n1 FILE " . $filename;  | 
            ||
| 319 | |||
| 320 | // Preserve some values from the original header  | 
            ||
| 321 |         $header = Registry::headerFactory()->make('HEAD', $tree) ?? Registry::headerFactory()->new('HEAD', '0 HEAD', null, $tree); | 
            ||
| 322 | |||
| 323 | // There should always be a header record.  | 
            ||
| 324 |         if ($header instanceof Header) { | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 325 |             foreach ($header->facts(['COPR', 'LANG', 'PLAC', 'NOTE']) as $fact) { | 
            ||
| 326 | $gedcom .= "\n" . $fact->gedcom();  | 
            ||
| 327 | }  | 
            ||
| 328 | |||
| 329 |             if ($include_sub) { | 
            ||
| 330 |                 foreach ($header->facts(['SUBM', 'SUBN']) as $fact) { | 
            ||
| 331 | $gedcom .= "\n" . $fact->gedcom();  | 
            ||
| 332 | }  | 
            ||
| 333 | }  | 
            ||
| 334 | }  | 
            ||
| 335 | |||
| 336 | return $gedcom;  | 
            ||
| 337 | }  | 
            ||
| 338 | |||
| 339 | /**  | 
            ||
| 340 | * Wrap long lines using concatenation records.  | 
            ||
| 341 | *  | 
            ||
| 342 | * @param string $gedcom  | 
            ||
| 343 | * @param int $max_line_length  | 
            ||
| 344 | *  | 
            ||
| 345 | * @return string  | 
            ||
| 346 | */  | 
            ||
| 347 | public function wrapLongLines(string $gedcom, int $max_line_length): string  | 
            ||
| 348 |     { | 
            ||
| 349 | $lines = [];  | 
            ||
| 350 | |||
| 351 |         foreach (explode("\n", $gedcom) as $line) { | 
            ||
| 352 | // Split long lines  | 
            ||
| 353 | // The total length of a GEDCOM line, including level number, cross-reference number,  | 
            ||
| 354 | // tag, value, delimiters, and terminator, must not exceed 255 (wide) characters.  | 
            ||
| 355 |             if (mb_strlen($line) > $max_line_length) { | 
            ||
| 356 |                 [$level, $tag] = explode(' ', $line, 3); | 
            ||
| 357 |                 if ($tag !== 'CONT') { | 
            ||
| 358 | $level++;  | 
            ||
| 359 | }  | 
            ||
| 360 |                 do { | 
            ||
| 361 | // Split after $pos chars  | 
            ||
| 362 | $pos = $max_line_length;  | 
            ||
| 363 | // Split on a non-space (standard gedcom behavior)  | 
            ||
| 364 |                     while (mb_substr($line, $pos - 1, 1) === ' ') { | 
            ||
| 365 | --$pos;  | 
            ||
| 366 | }  | 
            ||
| 367 |                     if ($pos === strpos($line, ' ', 3)) { | 
            ||
| 368 | // No non-spaces in the data! Can’t split it :-(  | 
            ||
| 369 | break;  | 
            ||
| 370 | }  | 
            ||
| 371 | $lines[] = mb_substr($line, 0, $pos);  | 
            ||
| 372 | $line = $level . ' CONC ' . mb_substr($line, $pos);  | 
            ||
| 373 | } while (mb_strlen($line) > $max_line_length);  | 
            ||
| 374 | }  | 
            ||
| 375 | $lines[] = $line;  | 
            ||
| 376 | }  | 
            ||
| 377 | |||
| 378 |         return implode("\n", $lines); | 
            ||
| 379 | }  | 
            ||
| 380 | |||
| 381 | /**  | 
            ||
| 382 | * @param Tree $tree  | 
            ||
| 383 | * @param bool $sort_by_xref  | 
            ||
| 384 | *  | 
            ||
| 385 | * @return Builder  | 
            ||
| 386 | */  | 
            ||
| 387 | private function familyQuery(Tree $tree, bool $sort_by_xref): Builder  | 
            ||
| 401 | }  | 
            ||
| 402 | |||
| 403 | /**  | 
            ||
| 404 | * @param Tree $tree  | 
            ||
| 405 | * @param bool $sort_by_xref  | 
            ||
| 406 | *  | 
            ||
| 407 | * @return Builder  | 
            ||
| 408 | */  | 
            ||
| 409 | private function individualQuery(Tree $tree, bool $sort_by_xref): Builder  | 
            ||
| 410 |     { | 
            ||
| 411 |         $query = DB::table('individuals') | 
            ||
| 412 |             ->where('i_file', '=', $tree->id()) | 
            ||
| 413 | ->select(['i_gedcom', 'i_id']);  | 
            ||
| 414 | |||
| 415 |         if ($sort_by_xref) { | 
            ||
| 416 | $query  | 
            ||
| 417 |                 ->orderBy(new Expression('LENGTH(i_id)')) | 
            ||
| 418 |                 ->orderBy('i_id'); | 
            ||
| 419 | }  | 
            ||
| 420 | |||
| 421 | return $query;  | 
            ||
| 422 | }  | 
            ||
| 423 | |||
| 424 | /**  | 
            ||
| 425 | * @param Tree $tree  | 
            ||
| 426 | * @param bool $sort_by_xref  | 
            ||
| 427 | *  | 
            ||
| 428 | * @return Builder  | 
            ||
| 429 | */  | 
            ||
| 430 | private function sourceQuery(Tree $tree, bool $sort_by_xref): Builder  | 
            ||
| 431 |     { | 
            ||
| 432 |         $query = DB::table('sources') | 
            ||
| 433 |             ->where('s_file', '=', $tree->id()) | 
            ||
| 434 | ->select(['s_gedcom', 's_id']);  | 
            ||
| 435 | |||
| 436 |         if ($sort_by_xref) { | 
            ||
| 437 | $query  | 
            ||
| 438 |                 ->orderBy(new Expression('LENGTH(s_id)')) | 
            ||
| 439 |                 ->orderBy('s_id'); | 
            ||
| 440 | }  | 
            ||
| 441 | |||
| 442 | return $query;  | 
            ||
| 443 | }  | 
            ||
| 444 | |||
| 445 | /**  | 
            ||
| 446 | * @param Tree $tree  | 
            ||
| 447 | * @param bool $sort_by_xref  | 
            ||
| 448 | *  | 
            ||
| 449 | * @return Builder  | 
            ||
| 450 | */  | 
            ||
| 451 | private function mediaQuery(Tree $tree, bool $sort_by_xref): Builder  | 
            ||
| 464 | }  | 
            ||
| 465 | |||
| 466 | /**  | 
            ||
| 467 | * @param Tree $tree  | 
            ||
| 468 | * @param bool $sort_by_xref  | 
            ||
| 469 | *  | 
            ||
| 470 | * @return Builder  | 
            ||
| 471 | */  | 
            ||
| 472 | private function otherQuery(Tree $tree, bool $sort_by_xref): Builder  | 
            ||
| 487 | }  | 
            ||
| 488 | }  | 
            ||
| 489 |