CheckTree::handle()   F
last analyzed

Complexity

Conditions 64
Paths 18

Size

Total Lines 241
Code Lines 185

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 64
eloc 185
c 0
b 0
f 0
nc 18
nop 1
dl 0
loc 241
rs 3.3333

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\DB;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use Fisharebest\Webtrees\Elements\AbstractXrefElement;
24
use Fisharebest\Webtrees\Elements\MultimediaFileReference;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Ele...MultimediaFileReference was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use Fisharebest\Webtrees\Elements\MultimediaFormat;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Elements\MultimediaFormat was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use Fisharebest\Webtrees\Elements\SubmitterText;
27
use Fisharebest\Webtrees\Elements\UnknownElement;
28
use Fisharebest\Webtrees\Elements\XrefFamily;
29
use Fisharebest\Webtrees\Elements\XrefIndividual;
30
use Fisharebest\Webtrees\Elements\XrefLocation;
31
use Fisharebest\Webtrees\Elements\XrefMedia;
32
use Fisharebest\Webtrees\Elements\XrefNote;
33
use Fisharebest\Webtrees\Elements\XrefRepository;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Elements\XrefRepository was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
use Fisharebest\Webtrees\Elements\XrefSource;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Elements\XrefSource was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
35
use Fisharebest\Webtrees\Elements\XrefSubmission;
36
use Fisharebest\Webtrees\Elements\XrefSubmitter;
37
use Fisharebest\Webtrees\Factories\ElementFactory;
38
use Fisharebest\Webtrees\Factories\ImageFactory;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Factories\ImageFactory was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
use Fisharebest\Webtrees\Family;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Family was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
40
use Fisharebest\Webtrees\Gedcom;
41
use Fisharebest\Webtrees\Header;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Header was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
use Fisharebest\Webtrees\Http\ViewResponseTrait;
43
use Fisharebest\Webtrees\I18N;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\I18N was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
44
use Fisharebest\Webtrees\Individual;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Individual was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
45
use Fisharebest\Webtrees\Location;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Location was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
46
use Fisharebest\Webtrees\Media;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Media was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
use Fisharebest\Webtrees\Mime;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Mime was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
48
use Fisharebest\Webtrees\Note;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Note was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
use Fisharebest\Webtrees\Repository;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Repository was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
use Fisharebest\Webtrees\Services\TimeoutService;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Services\TimeoutService was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
use Fisharebest\Webtrees\Source;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Source was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
52
use Fisharebest\Webtrees\Submission;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Submission was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
53
use Fisharebest\Webtrees\Submitter;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Submitter was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
54
use Fisharebest\Webtrees\Tree;
55
use Fisharebest\Webtrees\Validator;
56
use Illuminate\Database\Query\Expression;
57
use Psr\Http\Message\ResponseInterface;
58
use Psr\Http\Message\ServerRequestInterface;
59
use Psr\Http\Server\RequestHandlerInterface;
60
61
use function array_key_exists;
62
use function array_slice;
63
use function e;
64
use function implode;
65
use function preg_match;
66
use function route;
67
use function str_contains;
68
use function str_starts_with;
69
use function strtoupper;
70
use function substr_count;
71
72
/**
73
 * Check a tree for errors.
74
 */
75
class CheckTree implements RequestHandlerInterface
76
{
77
    use ViewResponseTrait;
78
79
    public function __construct(private readonly Gedcom $gedcom, private readonly TimeoutService $timeout_service)
80
    {
81
    }
82
83
    public function handle(ServerRequestInterface $request): ResponseInterface
84
    {
85
        $this->layout = 'layouts/administration';
86
87
        $tree    = Validator::attributes($request)->tree();
88
        $skip_to = Validator::queryParams($request)->string('skip_to', '');
89
90
        // We need to work with raw GEDCOM data, as we are looking for errors
91
        // which may prevent the GedcomRecord objects from working.
92
93
        $q1 = DB::table('individuals')
94
            ->where('i_file', '=', $tree->id())
95
            ->select(['i_id AS xref', 'i_gedcom AS gedcom', new Expression("'INDI' AS type")]);
0 ignored issues
show
Bug introduced by
''INDI' AS type' of type string is incompatible with the type Illuminate\Database\Query\TValue expected by parameter $value of Illuminate\Database\Quer...pression::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            ->select(['i_id AS xref', 'i_gedcom AS gedcom', new Expression(/** @scrutinizer ignore-type */ "'INDI' AS type")]);
Loading history...
96
        $q2 = DB::table('families')
97
            ->where('f_file', '=', $tree->id())
98
            ->select(['f_id AS xref', 'f_gedcom AS gedcom', new Expression("'FAM' AS type")]);
99
        $q3 = DB::table('media')
100
            ->where('m_file', '=', $tree->id())
101
            ->select(['m_id AS xref', 'm_gedcom AS gedcom', new Expression("'OBJE' AS type")]);
102
        $q4 = DB::table('sources')
103
            ->where('s_file', '=', $tree->id())
104
            ->select(['s_id AS xref', 's_gedcom AS gedcom', new Expression("'SOUR' AS type")]);
105
        $q5 = DB::table('other')
106
            ->where('o_file', '=', $tree->id())
107
            ->select(['o_id AS xref', 'o_gedcom AS gedcom', 'o_type']);
108
        $q6 = DB::table('change')
109
            ->where('gedcom_id', '=', $tree->id())
110
            ->where('status', '=', 'pending')
111
            ->orderBy('change_id')
112
            ->select(['xref', 'new_gedcom AS gedcom', new Expression("'' AS type")]);
113
114
        $rows = $q1
115
            ->unionAll($q2)
116
            ->unionAll($q3)
117
            ->unionAll($q4)
118
            ->unionAll($q5)
119
            ->unionAll($q6)
120
            ->get()
121
            ->map(static function (object $row): object {
122
                // Extract type for pending record
123
                if ($row->type === '' && str_starts_with($row->gedcom, '0 HEAD')) {
124
                    $row->type = 'HEAD';
125
                }
126
127
                if ($row->type === '' && preg_match('/^0 @[^@]*@ ([_A-Z0-9]+)/', $row->gedcom, $match) === 1) {
128
                    $row->type = $match[1];
129
                }
130
131
                return $row;
132
            });
133
134
        $records = [];
135
        $xrefs   = [];
136
137
        foreach ($rows as $row) {
138
            if ($row->gedcom !== '') {
139
                // existing or updated record
140
                $records[$row->xref] = $row;
141
            } else {
142
                // deleted record
143
                unset($records[$row->xref]);
144
            }
145
146
            $xrefs[strtoupper($row->xref)] = $row->xref;
147
        }
148
149
        unset($rows);
150
151
        $errors   = [];
152
        $warnings = [];
153
        $infos    = [];
154
155
        $element_factory = new ElementFactory();
156
        $this->gedcom->registerTags($element_factory, false);
157
158
        foreach ($records as $record) {
159
            // If we are nearly out of time, then stop processing here
160
            if ($skip_to === $record->xref) {
161
                $skip_to = '';
162
            } elseif ($skip_to !== '') {
163
                continue;
164
            } elseif ($this->timeout_service->isTimeNearlyUp()) {
165
                $skip_to = $record->xref;
166
                break;
167
            }
168
169
            $lines = explode("\n", $record->gedcom);
170
            array_shift($lines);
171
172
            $last_level = 0;
173
            $hierarchy  = [$record->type];
174
175
            foreach ($lines as $line_number => $line) {
176
                if (preg_match('/^(\d+) (\w+) ?(.*)/', $line, $match) !== 1) {
177
                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM record.'), '');
178
                    break;
179
                }
180
181
                $level = (int) $match[1];
182
                if ($level > $last_level + 1) {
183
                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, I18N::translate('Invalid GEDCOM level number.'), '');
184
                    break;
185
                }
186
187
                $tag               = $match[2];
188
                $value             = $match[3];
189
                $hierarchy[$level] = $tag;
190
                $full_tag          = implode(':', array_slice($hierarchy, 0, 1 + $level));
191
                $element           = $element_factory->make($full_tag);
192
                $last_level        = $level;
193
194
                if ($tag === 'CONT') {
195
                    $element = new SubmitterText('CONT');
196
                }
197
198
                if ($element instanceof UnknownElement) {
199
                    if (str_starts_with($tag, '_') || str_starts_with($full_tag, '_') || str_contains($full_tag, ':_')) {
200
                        $message    = I18N::translate('Custom GEDCOM tags are discouraged. Try to use only standard GEDCOM tags.');
201
                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag);
202
                    } else {
203
                        $message  = I18N::translate('Invalid GEDCOM tag.') . ' ' . $full_tag;
204
                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag);
205
                    }
206
                } elseif ($element instanceof AbstractXrefElement) {
207
                    if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $value, $match) === 1) {
208
                        $xref1  = $match[1];
209
                        $xref2  = $xrefs[strtoupper($xref1)] ?? null;
210
                        $linked = $records[$xref2] ?? null;
211
212
                        if ($linked === null) {
213
                            $message  = I18N::translate('%s does not exist.', e($xref1));
214
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $tag . '-' . $xref1);
215
                        } elseif ($element instanceof XrefFamily && $linked->type !== Family::RECORD_TYPE) {
216
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Family::RECORD_TYPE);
217
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
218
                        } elseif ($element instanceof XrefIndividual && $linked->type !== Individual::RECORD_TYPE) {
219
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Individual::RECORD_TYPE);
220
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
221
                        } elseif ($element instanceof XrefMedia && $linked->type !== Media::RECORD_TYPE) {
222
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Media::RECORD_TYPE);
223
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
224
                        } elseif ($element instanceof XrefNote && $linked->type !== Note::RECORD_TYPE) {
225
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Note::RECORD_TYPE);
226
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
227
                        } elseif ($element instanceof XrefSource && $linked->type !== Source::RECORD_TYPE) {
228
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Source::RECORD_TYPE);
229
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
230
                        } elseif ($element instanceof XrefRepository && $linked->type !== Repository::RECORD_TYPE) {
231
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Repository::RECORD_TYPE);
232
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
233
                        } elseif ($element instanceof XrefSubmitter && $linked->type !== Submitter::RECORD_TYPE) {
234
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submitter::RECORD_TYPE);
235
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
236
                        } elseif ($element instanceof XrefSubmission && $linked->type !== Submission::RECORD_TYPE) {
237
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Submission::RECORD_TYPE);
238
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
239
                        } elseif ($element instanceof XrefLocation && $linked->type !== Location::RECORD_TYPE) {
240
                            $message  = $this->linkErrorMessage($tree, $xref1, $linked->type, Location::RECORD_TYPE);
241
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-type');
242
                        } elseif (($full_tag === 'FAM:HUSB' || $full_tag === 'FAM:WIFE') && !str_contains($linked->gedcom, "\n1 FAMS @" . $record->xref . '@')) {
243
                            $link1    = $this->recordLink($tree, $linked->xref);
244
                            $link2    = $this->recordLink($tree, $record->xref);
245
                            $message  = I18N::translate('%1$s does not have a link back to %2$s.', $link1, $link2);
246
                            $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-FAMS');
247
                        } elseif ($full_tag === 'FAM:CHIL' && !str_contains($linked->gedcom, "\n1 FAMC @" . $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, $full_tag . '-FAMC');
252
                        } elseif ($full_tag === 'INDI:FAMC' && !str_contains($linked->gedcom, "\n1 CHIL @" . $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, $full_tag . '-CHIL');
257
                        } elseif ($full_tag === 'INDI:FAMS' && !str_contains($linked->gedcom, "\n1 HUSB @" . $record->xref . '@') && !str_contains($linked->gedcom, "\n1 WIFE @" . $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, $full_tag . '-HUSB-WIFE');
262
                        } elseif ($xref1 !== $xref2) {
263
                            $message    = I18N::translate('%1$s does not exist. Did you mean %2$s?', e($xref1), e($xref2));
264
                            $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $tag . '-' . $xref1);
265
                        }
266
                    } elseif ($tag === 'SOUR') {
267
                        $message    = I18N::translate('Inline-source records are discouraged.');
268
                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-inline');
269
                    } else {
270
                        $message  = I18N::translate('Invalid GEDCOM value.');
271
                        $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-value-' . e($value));
272
                    }
273
                } elseif ($element->canonical($value) !== $value) {
274
                    $expected = e($element->canonical($value));
275
                    $actual   = strtr(e($value), ["\t" => '&rarr;']);
276
                    $message  = I18N::translate('“%1$s” should be “%2$s”.', $actual, $expected);
277
                    if (strtoupper($element->canonical($value)) !== strtoupper($value)) {
278
                        // This will be relevant for GEDCOM 7.0.  It's not relevant now, and causes confusion.
279
                        $infos[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-value');
280
                    }
281
                } elseif ($element instanceof MultimediaFormat) {
282
                    $mime = Mime::TYPES[$value] ?? Mime::DEFAULT_TYPE;
283
284
                    if ($mime === Mime::DEFAULT_TYPE) {
285
                        $message    = I18N::translate('webtrees does not recognise this file format.');
286
                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-' . e($value));
287
                    } elseif (str_starts_with($mime, 'image/') && !array_key_exists($mime, ImageFactory::SUPPORTED_FORMATS)) {
288
                        $message    = I18N::translate('webtrees cannot create thumbnails for this file format.');
289
                        $warnings[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '-' . e($value));
290
                    }
291
                } elseif ($element instanceof MultimediaFileReference && $value === 'gedcom.ged') {
292
                    $message  = I18N::translate('This filename is not compatible with the GEDZIP file format.');
293
                    $errors[] = $this->lineError($tree, $record->type, $record->xref, $line_number, $line, $message, $full_tag . '_' . e($value));
294
                }
295
            }
296
297
            if ($record->type === Family::RECORD_TYPE) {
298
                if (substr_count($record->gedcom, "\n1 HUSB @") > 1) {
299
                    $message  = I18N::translate('%s occurs too many times.', 'FAM:HUSB');
300
                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message, 'FAM:HUSB-count');
301
                }
302
                if (substr_count($record->gedcom, "\n1 WIFE @") > 1) {
303
                    $message  = I18N::translate('%s occurs too many times.', 'FAM:WIFE');
304
                    $errors[] = $this->recordError($tree, $record->type, $record->xref, $message, 'FAM:WIFE-count');
305
                }
306
            }
307
        }
308
309
        $title = I18N::translate('Check for errors') . ' — ' . e($tree->title());
310
311
        if ($skip_to === '') {
312
            $more_url = '';
313
        } else {
314
            $more_url = route(self::class, ['tree' => $tree->name(), 'skip_to' => $skip_to]);
315
        }
316
317
        return $this->viewResponse('admin/trees-check', [
318
            'errors'   => $errors,
319
            'infos'    => $infos,
320
            'more_url' => $more_url,
321
            'title'    => $title,
322
            'tree'     => $tree,
323
            'warnings' => $warnings,
324
        ]);
325
    }
326
327
    private function recordType(string $type): string
328
    {
329
        $types = [
330
            Family::RECORD_TYPE     => I18N::translate('Family'),
331
            Header::RECORD_TYPE     => I18N::translate('Header'),
332
            Individual::RECORD_TYPE => I18N::translate('Individual'),
333
            Location::RECORD_TYPE   => I18N::translate('Location'),
334
            Media::RECORD_TYPE      => I18N::translate('Media object'),
335
            Note::RECORD_TYPE       => I18N::translate('Note'),
336
            Repository::RECORD_TYPE => I18N::translate('Repository'),
337
            Source::RECORD_TYPE     => I18N::translate('Source'),
338
            Submission::RECORD_TYPE => I18N::translate('Submission'),
339
            Submitter::RECORD_TYPE  => I18N::translate('Submitter'),
340
        ];
341
342
        return $types[$type] ?? e($type);
343
    }
344
345
    private function recordLink(Tree $tree, string $xref): string
346
    {
347
        $url = route(GedcomRecordPage::class, ['xref' => $xref, 'tree' => $tree->name()]);
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\Htt...ndlers\GedcomRecordPage was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
348
349
        return '<a href="' . e($url) . '">' . e($xref) . '</a>';
350
    }
351
352
    private function linkErrorMessage(Tree $tree, string $xref, string $type1, string $type2): string
353
    {
354
        $link  = $this->recordLink($tree, $xref);
355
        $type1 = $this->recordType($type1);
356
        $type2 = $this->recordType($type2);
357
358
        return I18N::translate('%1$s is a %2$s but a %3$s is expected.', $link, $type1, $type2);
359
    }
360
361
    /**
362
     * Format a link to a record.
363
     *
364
     * @return object{message:string,tag:string}
365
     */
366
    private function lineError(
367
        Tree $tree,
368
        string $type,
369
        string $xref,
370
        int $line_number,
371
        string $line,
372
        string $message,
373
        string $tag
374
    ): object {
375
        $message =
376
            I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) .
377
            ' — ' .
378
            I18N::translate('%1$s: %2$s', I18N::translate('Line number'), I18N::number($line_number)) .
379
            ' — ' .
380
            '<code>' . e($line) . '</code>' .
381
            '<br>' . $message;
382
383
        return (object) [
384
            'message' => $message,
385
            'tag'     => $tag,
386
        ];
387
    }
388
389
    /**
390
     * Format a link to a record.
391
     *
392
     * @return object{message:string,tag:string}
393
     */
394
    private function recordError(Tree $tree, string $type, string $xref, string $message, string $tag): object
395
    {
396
        $message = I18N::translate('%1$s: %2$s', $this->recordType($type), $this->recordLink($tree, $xref)) . ' — ' . $message;
397
398
        return (object) [
399
            'message' => $message,
400
            'tag'     => $tag,
401
        ];
402
    }
403
}
404