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