| Conditions | 4 |
| Paths | 5 |
| Total Lines | 57 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 40 | protected function search(ServerRequestInterface $request): Collection |
||
| 41 | { |
||
| 42 | $tree = $request->getAttribute('tree'); |
||
| 43 | assert($tree instanceof Tree); |
||
| 44 | |||
| 45 | $query = $request->getAttribute('query'); |
||
| 46 | $xref = $request->getQueryParams()['extra'] ?? ''; |
||
| 47 | $source = Registry::sourceFactory()->make($xref, $tree); |
||
| 48 | $source = Auth::checkSourceAccess($source); |
||
| 49 | |||
| 50 | $regex_query = preg_quote(strtr($query, [' ' => '.+']), '/'); |
||
| 51 | |||
| 52 | // Fetch all records with a link to this source |
||
| 53 | $individuals = DB::table('individuals') |
||
| 54 | ->join('link', static function (JoinClause $join): void { |
||
| 55 | $join |
||
| 56 | ->on('l_file', '=', 'i_file') |
||
| 57 | ->on('l_from', '=', 'i_id'); |
||
| 58 | }) |
||
| 59 | ->where('i_file', '=', $tree->id()) |
||
| 60 | ->where('l_to', '=', $source->xref()) |
||
| 61 | ->where('l_type', '=', 'SOUR') |
||
| 62 | ->distinct() |
||
| 63 | ->select(['individuals.*']) |
||
| 64 | ->get() |
||
| 65 | ->map(Registry::individualFactory()->mapper($tree)) |
||
| 66 | ->filter(GedcomRecord::accessFilter()); |
||
| 67 | |||
| 68 | $families = DB::table('families') |
||
| 69 | ->join('link', static function (JoinClause $join): void { |
||
| 70 | $join |
||
| 71 | ->on('l_file', '=', 'f_file') |
||
| 72 | ->on('l_from', '=', 'f_id') |
||
| 73 | ->where('l_type', '=', 'SOUR'); |
||
| 74 | }) |
||
| 75 | ->where('f_file', '=', $tree->id()) |
||
| 76 | ->where('l_to', '=', $source->xref()) |
||
| 77 | ->where('l_type', '=', 'SOUR') |
||
| 78 | ->distinct() |
||
| 79 | ->select(['families.*']) |
||
| 80 | ->get() |
||
| 81 | ->map(Registry::familyFactory()->mapper($tree)) |
||
| 82 | ->filter(GedcomRecord::accessFilter()); |
||
| 83 | |||
| 84 | $pages = new Collection(); |
||
| 85 | |||
| 86 | foreach ($individuals->merge($families) as $record) { |
||
| 87 | if (preg_match_all('/\n1 SOUR @' . $source->xref() . '@(?:\n[2-9].*)*\n2 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) { |
||
| 88 | $pages = $pages->concat($matches[1]); |
||
| 89 | } |
||
| 90 | |||
| 91 | if (preg_match_all('/\n2 SOUR @' . $source->xref() . '@(?:\n[3-9].*)*\n3 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) { |
||
| 92 | $pages = $pages->concat($matches[1]); |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | return $pages->uniqueStrict(); |
||
| 97 | } |
||
| 99 |