| Conditions | 14 |
| Paths | 68 |
| Total Lines | 80 |
| Code Lines | 53 |
| 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 |
||
| 45 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 46 | { |
||
| 47 | $tree = $request->getAttribute('tree'); |
||
| 48 | assert($tree instanceof Tree); |
||
| 49 | |||
| 50 | $params = (array) $request->getParsedBody(); |
||
| 51 | |||
| 52 | $delete_default_resn_id = $params['delete'] ?? []; |
||
| 53 | |||
| 54 | DB::table('default_resn') |
||
| 55 | ->whereIn('default_resn_id', $delete_default_resn_id) |
||
| 56 | ->delete(); |
||
| 57 | |||
| 58 | $xrefs = $params['xref'] ?? []; |
||
| 59 | $tag_types = $params['tag_type'] ?? []; |
||
| 60 | $resns = $params['resn'] ?? []; |
||
| 61 | |||
| 62 | foreach ($xrefs as $n => $xref) { |
||
| 63 | $tag_type = $tag_types[$n]; |
||
| 64 | $resn = $resns[$n]; |
||
| 65 | |||
| 66 | // Delete any existing data |
||
| 67 | if ($tag_type !== '' && $xref !== '') { |
||
| 68 | DB::table('default_resn') |
||
| 69 | ->where('gedcom_id', '=', $tree->id()) |
||
| 70 | ->where('tag_type', '=', $tag_type) |
||
| 71 | ->where('xref', '=', $xref) |
||
| 72 | ->delete(); |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($tag_type !== '' && $xref === '') { |
||
| 76 | DB::table('default_resn') |
||
| 77 | ->where('gedcom_id', '=', $tree->id()) |
||
| 78 | ->where('tag_type', '=', $tag_type) |
||
| 79 | ->whereNull('xref') |
||
| 80 | ->delete(); |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($tag_type === '' && $xref !== '') { |
||
| 84 | DB::table('default_resn') |
||
| 85 | ->where('gedcom_id', '=', $tree->id()) |
||
| 86 | ->whereNull('tag_type') |
||
| 87 | ->where('xref', '=', $xref) |
||
| 88 | ->delete(); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Add (or update) the new data |
||
| 92 | if ($tag_type !== '' || $xref !== '') { |
||
| 93 | DB::table('default_resn')->insert([ |
||
| 94 | 'gedcom_id' => $tree->id(), |
||
| 95 | 'xref' => $xref === '' ? null : $xref, |
||
| 96 | 'tag_type' => $tag_type === '' ? null : $tag_type, |
||
| 97 | 'resn' => $resn, |
||
| 98 | ]); |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | $tree->setPreference('HIDE_LIVE_PEOPLE', $params['HIDE_LIVE_PEOPLE']); |
||
| 103 | $tree->setPreference('KEEP_ALIVE_YEARS_BIRTH', $params['KEEP_ALIVE_YEARS_BIRTH']); |
||
| 104 | $tree->setPreference('KEEP_ALIVE_YEARS_DEATH', $params['KEEP_ALIVE_YEARS_DEATH']); |
||
| 105 | $tree->setPreference('MAX_ALIVE_AGE', $params['MAX_ALIVE_AGE']); |
||
| 106 | $tree->setPreference('REQUIRE_AUTHENTICATION', $params['REQUIRE_AUTHENTICATION']); |
||
| 107 | $tree->setPreference('SHOW_DEAD_PEOPLE', $params['SHOW_DEAD_PEOPLE']); |
||
| 108 | $tree->setPreference('SHOW_LIVING_NAMES', $params['SHOW_LIVING_NAMES']); |
||
| 109 | $tree->setPreference('SHOW_PRIVATE_RELATIONSHIPS', $params['SHOW_PRIVATE_RELATIONSHIPS']); |
||
| 110 | |||
| 111 | FlashMessages::addMessage(I18N::translate('The preferences for the family tree ā%sā have been updated.', e($tree->title())), 'success'); |
||
| 112 | |||
| 113 | // Coming soon... |
||
| 114 | $all_trees = $params['all_trees'] ?? ''; |
||
| 115 | $new_trees = $params['new_trees'] ?? ''; |
||
| 116 | |||
| 117 | if ($all_trees === 'on') { |
||
| 118 | FlashMessages::addMessage(I18N::translate('The preferences for all family trees have been updated.', e($tree->title())), 'success'); |
||
| 119 | } |
||
| 120 | if ($new_trees === 'on') { |
||
| 121 | FlashMessages::addMessage(I18N::translate('The preferences for new family trees have been updated.', e($tree->title())), 'success'); |
||
| 122 | } |
||
| 123 | |||
| 124 | return redirect(route(ManageTrees::class, ['tree' => $tree->name()])); |
||
| 125 | } |
||
| 127 |