Conditions | 15 |
Paths | 109 |
Total Lines | 122 |
Code Lines | 80 |
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 |
||
60 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
61 | { |
||
62 | $tree = Validator::attributes($request)->tree(); |
||
63 | |||
64 | $params = (array) $request->getParsedBody(); |
||
65 | |||
66 | $xref1 = $params['xref1'] ?? ''; |
||
67 | $xref2 = $params['xref2'] ?? ''; |
||
68 | |||
69 | $keep1 = $params['keep1'] ?? []; |
||
70 | $keep2 = $params['keep2'] ?? []; |
||
71 | |||
72 | // Merge record2 into record1 |
||
73 | $record1 = Registry::gedcomRecordFactory()->make($xref1, $tree); |
||
74 | $record2 = Registry::gedcomRecordFactory()->make($xref2, $tree); |
||
75 | |||
76 | if ( |
||
77 | $record1 === null || |
||
78 | $record2 === null || |
||
79 | $record1 === $record2 || |
||
80 | $record1->tag() !== $record2->tag() || |
||
81 | $record1->isPendingDeletion() || |
||
82 | $record2->isPendingDeletion() |
||
83 | ) { |
||
84 | return Registry::responseFactory()->redirect(MergeRecordsPage::class, [ |
||
85 | 'tree' => $tree->name(), |
||
86 | 'xref1' => $xref1, |
||
87 | 'xref2' => $xref2, |
||
88 | ]); |
||
89 | } |
||
90 | |||
91 | // If we are not auto-accepting, then we can show a link to the pending deletion |
||
92 | if (Auth::user()->getPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS) === '1') { |
||
93 | $record2_name = $record2->fullName(); |
||
94 | } else { |
||
95 | $record2_name = '<a class="alert-link" href="' . e($record2->url()) . '">' . $record2->fullName() . '</a>'; |
||
96 | } |
||
97 | |||
98 | // Update records that link to the one we will be removing. |
||
99 | $linking_records = $this->linked_record_service->allLinkedRecords($record2); |
||
100 | |||
101 | foreach ($linking_records as $record) { |
||
102 | if (!$record->isPendingDeletion()) { |
||
103 | /* I18N: The placeholders are the names of individuals, sources, etc. */ |
||
104 | FlashMessages::addMessage(I18N::translate( |
||
105 | 'The link from “%1$s” to “%2$s” has been updated.', |
||
106 | '<a class="alert-link" href="' . e($record->url()) . '">' . $record->fullName() . '</a>', |
||
107 | $record2_name |
||
108 | ), 'info'); |
||
109 | $gedcom = str_replace('@' . $xref2 . '@', '@' . $xref1 . '@', $record->gedcom()); |
||
110 | $gedcom = preg_replace( |
||
111 | '/(\n1.*@.+@.*(?:\n[2-9].*)*)((?:\n1.*(?:\n[2-9].*)*)*\1)/', |
||
112 | '$2', |
||
113 | $gedcom |
||
114 | ); |
||
115 | $record->updateRecord($gedcom, true); |
||
116 | } |
||
117 | } |
||
118 | |||
119 | // Update any linked user-accounts |
||
120 | DB::table('user_gedcom_setting') |
||
121 | ->where('gedcom_id', '=', $tree->id()) |
||
122 | ->whereIn('setting_name', [UserInterface::PREF_TREE_ACCOUNT_XREF, UserInterface::PREF_TREE_DEFAULT_XREF]) |
||
123 | ->where('setting_value', '=', $xref2) |
||
124 | ->update(['setting_value' => $xref1]); |
||
125 | |||
126 | // Merge stories, etc. |
||
127 | DB::table('block') |
||
128 | ->where('gedcom_id', '=', $tree->id()) |
||
129 | ->where('xref', '=', $xref2) |
||
130 | ->update(['xref' => $xref1]); |
||
131 | |||
132 | // Merge hit counters |
||
133 | $hits = DB::table('hit_counter') |
||
134 | ->where('gedcom_id', '=', $tree->id()) |
||
135 | ->whereIn('page_parameter', [$xref1, $xref2]) |
||
136 | ->groupBy(['page_name']) |
||
137 | ->pluck(new Expression('SUM(page_count)'), 'page_name'); |
||
138 | |||
139 | foreach ($hits as $page_name => $page_count) { |
||
140 | DB::table('hit_counter') |
||
141 | ->where('gedcom_id', '=', $tree->id()) |
||
142 | ->where('page_name', '=', $page_name) |
||
143 | ->where('page_parameter', '=', $xref1) |
||
144 | ->update(['page_count' => $page_count]); |
||
145 | } |
||
146 | |||
147 | DB::table('hit_counter') |
||
148 | ->where('gedcom_id', '=', $tree->id()) |
||
149 | ->where('page_parameter', '=', $xref2) |
||
150 | ->delete(); |
||
151 | |||
152 | $gedcom = '0 @' . $record1->xref() . '@ ' . $record1->tag(); |
||
153 | |||
154 | foreach ($record1->facts() as $fact) { |
||
155 | if (in_array($fact->id(), $keep1, true)) { |
||
156 | $gedcom .= "\n" . $fact->gedcom(); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | foreach ($record2->facts() as $fact) { |
||
161 | if (in_array($fact->id(), $keep2, true)) { |
||
162 | $gedcom .= "\n" . $fact->gedcom(); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | DB::table('favorite') |
||
167 | ->where('gedcom_id', '=', $tree->id()) |
||
168 | ->where('xref', '=', $xref2) |
||
169 | ->update(['xref' => $xref1]); |
||
170 | |||
171 | $record1->updateRecord($gedcom, true); |
||
172 | $record2->deleteRecord(); |
||
173 | |||
174 | /* I18N: Records are individuals, sources, etc. */ |
||
175 | FlashMessages::addMessage(I18N::translate( |
||
176 | 'The records “%1$s” and “%2$s” have been merged.', |
||
177 | '<a class="alert-link" href="' . e($record1->url()) . '">' . $record1->fullName() . '</a>', |
||
178 | $record2_name |
||
179 | ), 'success'); |
||
180 | |||
181 | return Registry::responseFactory()->redirect(ManageTrees::class, ['tree' => $tree->name()]); |
||
182 | } |
||
184 |