Conditions | 7 |
Paths | 2 |
Total Lines | 56 |
Code Lines | 39 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
51 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
52 | { |
||
53 | $tree = $request->getAttribute('tree'); |
||
54 | assert($tree instanceof Tree); |
||
55 | |||
56 | $replace = $request->getParsedBody()['replace'] ?? ''; |
||
57 | $search = $request->getParsedBody()['search'] ?? ''; |
||
58 | $submit = $request->getParsedBody()['submit'] ?? ''; |
||
59 | |||
60 | if ($search !== '' && $replace !== '' && $submit === 'update') { |
||
61 | $individual_changes = DB::table('individuals') |
||
62 | ->where('i_file', '=', $tree->id()) |
||
63 | ->whereContains('i_gedcom', $search) |
||
64 | ->select(['individuals.*']) |
||
65 | ->get() |
||
66 | ->map(Individual::rowMapper()); |
||
67 | |||
68 | $family_changes = DB::table('families') |
||
69 | ->where('f_file', '=', $tree->id()) |
||
70 | ->whereContains('f_gedcom', $search) |
||
71 | ->select(['families.*']) |
||
72 | ->get() |
||
73 | ->map(Family::rowMapper()); |
||
74 | |||
75 | $changes = $individual_changes->merge($family_changes) |
||
76 | ->mapWithKeys(static function (GedcomRecord $record) use ($search, $replace): array { |
||
77 | $changes = []; |
||
78 | |||
79 | foreach ($record->facts() as $fact) { |
||
80 | $old_place = $fact->attribute('PLAC'); |
||
81 | if (preg_match('/(^|, )' . preg_quote($search, '/') . '$/i', $old_place)) { |
||
82 | $new_place = preg_replace('/(^|, )' . preg_quote($search, '/') . '$/i', '$1' . $replace, $old_place); |
||
83 | $changes[$old_place] = $new_place; |
||
84 | $gedcom = preg_replace('/(\n2 PLAC (?:.*, )*)' . preg_quote($search, '/') . '(\n|$)/i', '$1' . $replace . '$2', $fact->gedcom()); |
||
85 | $record->updateFact($fact->id(), $gedcom, false); |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return $changes; |
||
90 | }) |
||
91 | ->sort(); |
||
92 | |||
93 | $feedback = I18N::translate('The following places have been changed:') . '<ul>'; |
||
94 | |||
95 | foreach ($changes as $old_place => $new_place) { |
||
96 | $feedback .= '<li>' . e($old_place) . ' → ' . e($new_place) . '</li>'; |
||
97 | } |
||
98 | $feedback .= '</ul>'; |
||
99 | |||
100 | FlashMessages::addMessage($feedback, 'success'); |
||
101 | } |
||
102 | |||
103 | return redirect(route(UpdatePlacesPage::class, [ |
||
104 | 'tree' => $tree->name(), |
||
105 | 'replace' => $replace, |
||
106 | 'search' => $search, |
||
107 | ])); |
||
110 |