Total Complexity | 44 |
Total Lines | 382 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 0 |
Complex classes like FixSearchAndReplace often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FixSearchAndReplace, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
49 | class FixSearchAndReplace extends AbstractModule implements ModuleDataFixInterface |
||
50 | { |
||
51 | use ModuleDataFixTrait; |
||
1 ignored issue
–
show
|
|||
52 | |||
53 | // A regular expression that never matches. |
||
54 | private const INVALID_REGEX = '/(?!)/'; |
||
55 | |||
56 | /** @var DataFixService */ |
||
57 | private $data_fix_service; |
||
58 | |||
59 | /** |
||
60 | * FixMissingDeaths constructor. |
||
61 | * |
||
62 | * @param DataFixService $data_fix_service |
||
63 | */ |
||
64 | public function __construct(DataFixService $data_fix_service) |
||
65 | { |
||
66 | $this->data_fix_service = $data_fix_service; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * How should this module be identified in the control panel, etc.? |
||
71 | * |
||
72 | * @return string |
||
73 | */ |
||
74 | public function title(): string |
||
75 | { |
||
76 | /* I18N: Name of a module */ |
||
77 | return I18N::translate('Search and replace'); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * A sentence describing what this module does. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function description(): string |
||
86 | { |
||
87 | /* I18N: Description of a “Data fix” module */ |
||
88 | return I18N::translate('Search and replace text, using simple searches or advanced pattern matching.'); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Options form. |
||
93 | * |
||
94 | * @param Tree $tree |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public function fixOptions(Tree $tree): string |
||
125 | ]); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * A list of all records that need examining. This may include records |
||
130 | * that do not need updating, if we can't detect this quickly using SQL. |
||
131 | * |
||
132 | * @param Tree $tree |
||
133 | * @param array<string,string> $params |
||
134 | * |
||
135 | * @return Collection<string>|null |
||
136 | */ |
||
137 | protected function familiesToFix(Tree $tree, array $params): ?Collection |
||
138 | { |
||
139 | if ($params['type'] !== Family::RECORD_TYPE || $params['search'] === '') { |
||
140 | return null; |
||
141 | } |
||
142 | |||
143 | $query = DB::table('families')->where('f_file', '=', $tree->id()); |
||
144 | $this->recordQuery($query, 'f_gedcom', $params); |
||
145 | |||
146 | return $query->pluck('f_id'); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * A list of all records that need examining. This may include records |
||
151 | * that do not need updating, if we can't detect this quickly using SQL. |
||
152 | * |
||
153 | * @param Tree $tree |
||
154 | * @param array<string,string> $params |
||
155 | * |
||
156 | * @return Collection<string>|null |
||
157 | */ |
||
158 | protected function individualsToFix(Tree $tree, array $params): ?Collection |
||
159 | { |
||
160 | if ($params['type'] !== Individual::RECORD_TYPE || $params['search'] === '') { |
||
161 | return null; |
||
162 | } |
||
163 | |||
164 | $query = DB::table('individuals') |
||
165 | ->where('i_file', '=', $tree->id()); |
||
166 | |||
167 | $this->recordQuery($query, 'i_gedcom', $params); |
||
168 | |||
169 | return $query->pluck('i_id'); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * A list of all records that need examining. This may include records |
||
174 | * that do not need updating, if we can't detect this quickly using SQL. |
||
175 | * |
||
176 | * @param Tree $tree |
||
177 | * @param array<string,string> $params |
||
178 | * |
||
179 | * @return Collection<string>|null |
||
180 | */ |
||
181 | protected function mediaToFix(Tree $tree, array $params): ?Collection |
||
182 | { |
||
183 | if ($params['type'] !== Media::RECORD_TYPE || $params['search'] === '') { |
||
184 | return null; |
||
185 | } |
||
186 | |||
187 | $query = DB::table('media') |
||
188 | ->where('m_file', '=', $tree->id()); |
||
189 | |||
190 | $this->recordQuery($query, 'm_gedcom', $params); |
||
191 | |||
192 | return $query->pluck('m_id'); |
||
193 | } |
||
194 | |||
195 | /** |
||
196 | * A list of all records that need examining. This may include records |
||
197 | * that do not need updating, if we can't detect this quickly using SQL. |
||
198 | * |
||
199 | * @param Tree $tree |
||
200 | * @param array<string,string> $params |
||
201 | * |
||
202 | * @return Collection<string>|null |
||
203 | */ |
||
204 | protected function notesToFix(Tree $tree, array $params): ?Collection |
||
205 | { |
||
206 | if ($params['type'] !== Note::RECORD_TYPE || $params['search'] === '') { |
||
207 | return null; |
||
208 | } |
||
209 | |||
210 | $query = DB::table('other') |
||
211 | ->where('o_file', '=', $tree->id()) |
||
212 | ->where('o_type', '=', Note::RECORD_TYPE); |
||
213 | |||
214 | $this->recordQuery($query, 'o_gedcom', $params); |
||
215 | |||
216 | return $query->pluck('o_id'); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * A list of all records that need examining. This may include records |
||
221 | * that do not need updating, if we can't detect this quickly using SQL. |
||
222 | * |
||
223 | * @param Tree $tree |
||
224 | * @param array<string,string> $params |
||
225 | * |
||
226 | * @return Collection<string>|null |
||
227 | */ |
||
228 | protected function repositoriesToFix(Tree $tree, array $params): ?Collection |
||
229 | { |
||
230 | if ($params['type'] !== Repository::RECORD_TYPE || $params['search'] === '') { |
||
231 | return null; |
||
232 | } |
||
233 | |||
234 | $query = DB::table('other') |
||
235 | ->where('o_file', '=', $tree->id()) |
||
236 | ->where('o_type', '=', Repository::RECORD_TYPE); |
||
237 | |||
238 | $this->recordQuery($query, 'o_gedcom', $params); |
||
239 | |||
240 | return $query->pluck('o_id'); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * A list of all records that need examining. This may include records |
||
245 | * that do not need updating, if we can't detect this quickly using SQL. |
||
246 | * |
||
247 | * @param Tree $tree |
||
248 | * @param array<string,string> $params |
||
249 | * |
||
250 | * @return Collection<string>|null |
||
251 | */ |
||
252 | protected function sourcesToFix(Tree $tree, array $params): ?Collection |
||
253 | { |
||
254 | if ($params['type'] !== Source::RECORD_TYPE || $params['search'] === '') { |
||
255 | return null; |
||
256 | } |
||
257 | |||
258 | $query = DB::table('sources') |
||
259 | ->where('s_file', '=', $tree->id()); |
||
260 | |||
261 | $this->recordQuery($query, 's_gedcom', $params); |
||
262 | |||
263 | return $query->pluck('s_id'); |
||
264 | } |
||
265 | |||
266 | /** |
||
267 | * A list of all records that need examining. This may include records |
||
268 | * that do not need updating, if we can't detect this quickly using SQL. |
||
269 | * |
||
270 | * @param Tree $tree |
||
271 | * @param array<string,string> $params |
||
272 | * |
||
273 | * @return Collection<string>|null |
||
274 | */ |
||
275 | protected function submittersToFix(Tree $tree, array $params): ?Collection |
||
276 | { |
||
277 | if ($params['type'] !== Submitter::RECORD_TYPE || $params['search'] === '') { |
||
278 | return null; |
||
279 | } |
||
280 | |||
281 | $query = DB::table('other') |
||
282 | ->where('o_file', '=', $tree->id()) |
||
283 | ->where('o_type', '=', Submitter::RECORD_TYPE); |
||
284 | |||
285 | $this->recordQuery($query, 'o_gedcom', $params); |
||
286 | |||
287 | return $query->pluck('o_id'); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Does a record need updating? |
||
292 | * |
||
293 | * @param GedcomRecord $record |
||
294 | * @param array<string,string> $params |
||
295 | * |
||
296 | * @return bool |
||
297 | */ |
||
298 | public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool |
||
299 | { |
||
300 | return preg_match($this->createRegex($params), $record->gedcom()) === 1; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Show the changes we would make |
||
305 | * |
||
306 | * @param GedcomRecord $record |
||
307 | * @param array<string,string> $params |
||
308 | * |
||
309 | * @return string |
||
310 | */ |
||
311 | public function previewUpdate(GedcomRecord $record, array $params): string |
||
312 | { |
||
313 | $old = $record->gedcom(); |
||
314 | $new = $this->updateGedcom($record, $params); |
||
315 | |||
316 | return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new); |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Fix a record |
||
321 | * |
||
322 | * @param GedcomRecord $record |
||
323 | * @param array<string,string> $params |
||
324 | * |
||
325 | * @return void |
||
326 | */ |
||
327 | public function updateRecord(GedcomRecord $record, array $params): void |
||
328 | { |
||
329 | $record->updateRecord($this->updateGedcom($record, $params), false); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @param GedcomRecord $record |
||
334 | * @param array<string,string> $params |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | private function updateGedcom(GedcomRecord $record, array $params): string |
||
339 | { |
||
340 | // Allow "\n" to indicate a line-feed in replacement text. |
||
341 | // Back-references such as $1, $2 are handled automatically. |
||
342 | $replace = strtr($params['replace'], ['\\n', "\n"]); |
||
343 | |||
344 | $regex = $this->createRegex($params); |
||
345 | |||
346 | return preg_replace($regex, $replace, $record->gedcom()); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Create a regular expression from the search pattern. |
||
351 | * |
||
352 | * @param array<string,string> $params |
||
353 | * |
||
354 | * @return string |
||
355 | */ |
||
356 | private function createRegex(array $params): string |
||
357 | { |
||
358 | $search = $params['search']; |
||
359 | $method = $params['method']; |
||
360 | $case = $params['case']; |
||
361 | |||
362 | switch ($method) { |
||
363 | case 'exact': |
||
364 | return '/' . preg_quote($search, '/') . '/' . $case; |
||
365 | |||
366 | case 'words': |
||
367 | return '/\b' . preg_quote($search, '/') . '\b/' . $case; |
||
368 | |||
369 | case 'wildcards': |
||
370 | return '/\b' . strtr(preg_quote($search, '/'), ['\*' => '.*', '\?' => '.']) . '\b/' . $case; |
||
371 | |||
372 | case 'regex': |
||
373 | $regex = '/' . addcslashes($search, '/') . '/' . $case; |
||
374 | |||
375 | try { |
||
376 | // A valid regex on an empty string returns zero. |
||
377 | // An invalid regex on an empty string returns false and throws a warning. |
||
378 | preg_match($regex, ''); |
||
379 | } catch (Throwable $ex) { |
||
380 | $regex = self::INVALID_REGEX; |
||
381 | } |
||
382 | |||
383 | return $regex; |
||
384 | } |
||
385 | |||
386 | throw new HttpNotFoundException(); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * Create a regular expression from the search pattern. |
||
391 | * |
||
392 | * @param Builder $query |
||
393 | * @param string $column |
||
394 | * @param array<string,string> $params |
||
395 | * |
||
396 | * @return void |
||
397 | */ |
||
398 | private function recordQuery(Builder $query, string $column, array $params): void |
||
431 | } |
||
432 | } |
||
433 | } |
||
434 |