Conditions | 64 |
Paths | 18 |
Total Lines | 241 |
Code Lines | 185 |
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 |
||
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")]); |
||
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" => '→']); |
||
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 | ]); |
||
404 |
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