| Total Complexity | 43 | 
| Total Lines | 392 | 
| Duplicated Lines | 0 % | 
| Changes | 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  | 
            ||
| 50 | class FixSearchAndReplace extends AbstractModule implements ModuleDataFixInterface  | 
            ||
| 51 | { | 
            ||
| 52 | use ModuleDataFixTrait;  | 
            ||
| 53 | |||
| 54 | // A regular expression that never matches.  | 
            ||
| 55 | private const INVALID_REGEX = '/(?!)/';  | 
            ||
| 56 | |||
| 57 | private DataFixService $data_fix_service;  | 
            ||
| 58 | |||
| 59 | /**  | 
            ||
| 60 | * @param DataFixService $data_fix_service  | 
            ||
| 61 | */  | 
            ||
| 62 | public function __construct(DataFixService $data_fix_service)  | 
            ||
| 63 |     { | 
            ||
| 64 | $this->data_fix_service = $data_fix_service;  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * How should this module be identified in the control panel, etc.?  | 
            ||
| 69 | *  | 
            ||
| 70 | * @return string  | 
            ||
| 71 | */  | 
            ||
| 72 | public function title(): string  | 
            ||
| 73 |     { | 
            ||
| 74 | /* I18N: Name of a module */  | 
            ||
| 75 |         return I18N::translate('Search and replace'); | 
            ||
| 76 | }  | 
            ||
| 77 | |||
| 78 | /**  | 
            ||
| 79 | * A sentence describing what this module does.  | 
            ||
| 80 | *  | 
            ||
| 81 | * @return string  | 
            ||
| 82 | */  | 
            ||
| 83 | public function description(): string  | 
            ||
| 84 |     { | 
            ||
| 85 | /* I18N: Description of a “Data fix” module */  | 
            ||
| 86 |         return I18N::translate('Search and replace text, using simple searches or advanced pattern matching.'); | 
            ||
| 87 | }  | 
            ||
| 88 | |||
| 89 | /**  | 
            ||
| 90 | * Options form.  | 
            ||
| 91 | *  | 
            ||
| 92 | * @param Tree $tree  | 
            ||
| 93 | *  | 
            ||
| 94 | * @return string  | 
            ||
| 95 | */  | 
            ||
| 96 | public function fixOptions(Tree $tree): string  | 
            ||
| 124 | ]);  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * A list of all records that need examining. This may include records  | 
            ||
| 129 | * that do not need updating, if we can't detect this quickly using SQL.  | 
            ||
| 130 | *  | 
            ||
| 131 | * @param Tree $tree  | 
            ||
| 132 | * @param array<string,string> $params  | 
            ||
| 133 | *  | 
            ||
| 134 | * @return Collection<int,string>|null  | 
            ||
| 135 | */  | 
            ||
| 136 | protected function familiesToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 137 |     { | 
            ||
| 138 |         if ($params['type'] !== Family::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 139 | return null;  | 
            ||
| 140 | }  | 
            ||
| 141 | |||
| 142 |         $query = DB::table('families')->where('f_file', '=', $tree->id()); | 
            ||
| 143 | $this->recordQuery($query, 'f_gedcom', $params);  | 
            ||
| 144 | |||
| 145 |         return $query->pluck('f_id'); | 
            ||
| 146 | }  | 
            ||
| 147 | |||
| 148 | /**  | 
            ||
| 149 | * A list of all records that need examining. This may include records  | 
            ||
| 150 | * that do not need updating, if we can't detect this quickly using SQL.  | 
            ||
| 151 | *  | 
            ||
| 152 | * @param Tree $tree  | 
            ||
| 153 | * @param array<string,string> $params  | 
            ||
| 154 | *  | 
            ||
| 155 | * @return Collection<int,string>|null  | 
            ||
| 156 | */  | 
            ||
| 157 | protected function individualsToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 158 |     { | 
            ||
| 159 |         if ($params['type'] !== Individual::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 160 | return null;  | 
            ||
| 161 | }  | 
            ||
| 162 | |||
| 163 |         $query = DB::table('individuals') | 
            ||
| 164 |             ->where('i_file', '=', $tree->id()); | 
            ||
| 165 | |||
| 166 | $this->recordQuery($query, 'i_gedcom', $params);  | 
            ||
| 167 | |||
| 168 |         return $query->pluck('i_id'); | 
            ||
| 169 | }  | 
            ||
| 170 | |||
| 171 | /**  | 
            ||
| 172 | * A list of all records that need examining. This may include records  | 
            ||
| 173 | * that do not need updating, if we can't detect this quickly using SQL.  | 
            ||
| 174 | *  | 
            ||
| 175 | * @param Tree $tree  | 
            ||
| 176 | * @param array<string,string> $params  | 
            ||
| 177 | *  | 
            ||
| 178 | * @return Collection<int,string>|null  | 
            ||
| 179 | */  | 
            ||
| 180 | protected function locationsToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 181 |     { | 
            ||
| 182 |         if ($params['type'] !== Location::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 183 | return null;  | 
            ||
| 184 | }  | 
            ||
| 185 | |||
| 186 |         $query = DB::table('other') | 
            ||
| 187 |             ->where('o_file', '=', $tree->id()) | 
            ||
| 188 |             ->where('o_type', '=', Location::RECORD_TYPE); | 
            ||
| 189 | |||
| 190 | $this->recordQuery($query, 'o_gedcom', $params);  | 
            ||
| 191 | |||
| 192 |         return $query->pluck('o_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<int,string>|null  | 
            ||
| 203 | */  | 
            ||
| 204 | protected function mediaToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 205 |     { | 
            ||
| 206 |         if ($params['type'] !== Media::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 207 | return null;  | 
            ||
| 208 | }  | 
            ||
| 209 | |||
| 210 |         $query = DB::table('media') | 
            ||
| 211 |             ->where('m_file', '=', $tree->id()); | 
            ||
| 212 | |||
| 213 | $this->recordQuery($query, 'm_gedcom', $params);  | 
            ||
| 214 | |||
| 215 |         return $query->pluck('m_id'); | 
            ||
| 216 | }  | 
            ||
| 217 | |||
| 218 | /**  | 
            ||
| 219 | * A list of all records that need examining. This may include records  | 
            ||
| 220 | * that do not need updating, if we can't detect this quickly using SQL.  | 
            ||
| 221 | *  | 
            ||
| 222 | * @param Tree $tree  | 
            ||
| 223 | * @param array<string,string> $params  | 
            ||
| 224 | *  | 
            ||
| 225 | * @return Collection<int,string>|null  | 
            ||
| 226 | */  | 
            ||
| 227 | protected function notesToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 228 |     { | 
            ||
| 229 |         if ($params['type'] !== Note::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 230 | return null;  | 
            ||
| 231 | }  | 
            ||
| 232 | |||
| 233 |         $query = DB::table('other') | 
            ||
| 234 |             ->where('o_file', '=', $tree->id()) | 
            ||
| 235 |             ->where('o_type', '=', Note::RECORD_TYPE); | 
            ||
| 236 | |||
| 237 | $this->recordQuery($query, 'o_gedcom', $params);  | 
            ||
| 238 | |||
| 239 |         return $query->pluck('o_id'); | 
            ||
| 240 | }  | 
            ||
| 241 | |||
| 242 | /**  | 
            ||
| 243 | * A list of all records that need examining. This may include records  | 
            ||
| 244 | * that do not need updating, if we can't detect this quickly using SQL.  | 
            ||
| 245 | *  | 
            ||
| 246 | * @param Tree $tree  | 
            ||
| 247 | * @param array<string,string> $params  | 
            ||
| 248 | *  | 
            ||
| 249 | * @return Collection<int,string>|null  | 
            ||
| 250 | */  | 
            ||
| 251 | protected function repositoriesToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 252 |     { | 
            ||
| 253 |         if ($params['type'] !== Repository::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 254 | return null;  | 
            ||
| 255 | }  | 
            ||
| 256 | |||
| 257 |         $query = DB::table('other') | 
            ||
| 258 |             ->where('o_file', '=', $tree->id()) | 
            ||
| 259 |             ->where('o_type', '=', Repository::RECORD_TYPE); | 
            ||
| 260 | |||
| 261 | $this->recordQuery($query, 'o_gedcom', $params);  | 
            ||
| 262 | |||
| 263 |         return $query->pluck('o_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<int,string>|null  | 
            ||
| 274 | */  | 
            ||
| 275 | protected function sourcesToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 276 |     { | 
            ||
| 277 |         if ($params['type'] !== Source::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 278 | return null;  | 
            ||
| 279 | }  | 
            ||
| 280 | |||
| 281 | $query = $this->sourcesToFixQuery($tree, $params);  | 
            ||
| 282 | |||
| 283 | $this->recordQuery($query, 's_gedcom', $params);  | 
            ||
| 284 | |||
| 285 |         return $query->pluck('s_id'); | 
            ||
| 286 | }  | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * A list of all records that need examining. This may include records  | 
            ||
| 290 | * that do not need updating, if we can't detect this quickly using SQL.  | 
            ||
| 291 | *  | 
            ||
| 292 | * @param Tree $tree  | 
            ||
| 293 | * @param array<string,string> $params  | 
            ||
| 294 | *  | 
            ||
| 295 | * @return Collection<int,string>|null  | 
            ||
| 296 | */  | 
            ||
| 297 | protected function submittersToFix(Tree $tree, array $params): Collection|null  | 
            ||
| 298 |     { | 
            ||
| 299 |         if ($params['type'] !== Submitter::RECORD_TYPE || $params['search-for'] === '') { | 
            ||
| 300 | return null;  | 
            ||
| 301 | }  | 
            ||
| 302 | |||
| 303 | $query = $this->submittersToFixQuery($tree, $params);  | 
            ||
| 304 | |||
| 305 | $this->recordQuery($query, 'o_gedcom', $params);  | 
            ||
| 306 | |||
| 307 |         return $query->pluck('o_id'); | 
            ||
| 308 | }  | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * Does a record need updating?  | 
            ||
| 312 | *  | 
            ||
| 313 | * @param GedcomRecord $record  | 
            ||
| 314 | * @param array<string,string> $params  | 
            ||
| 315 | *  | 
            ||
| 316 | * @return bool  | 
            ||
| 317 | */  | 
            ||
| 318 | public function doesRecordNeedUpdate(GedcomRecord $record, array $params): bool  | 
            ||
| 319 |     { | 
            ||
| 320 | return preg_match($this->createRegex($params), $record->gedcom()) === 1;  | 
            ||
| 321 | }  | 
            ||
| 322 | |||
| 323 | /**  | 
            ||
| 324 | * Show the changes we would make  | 
            ||
| 325 | *  | 
            ||
| 326 | * @param GedcomRecord $record  | 
            ||
| 327 | * @param array<string,string> $params  | 
            ||
| 328 | *  | 
            ||
| 329 | * @return string  | 
            ||
| 330 | */  | 
            ||
| 331 | public function previewUpdate(GedcomRecord $record, array $params): string  | 
            ||
| 332 |     { | 
            ||
| 333 | $old = $record->gedcom();  | 
            ||
| 334 | $new = $this->updateGedcom($record, $params);  | 
            ||
| 335 | |||
| 336 | return $this->data_fix_service->gedcomDiff($record->tree(), $old, $new);  | 
            ||
| 337 | }  | 
            ||
| 338 | |||
| 339 | /**  | 
            ||
| 340 | * Fix a record  | 
            ||
| 341 | *  | 
            ||
| 342 | * @param GedcomRecord $record  | 
            ||
| 343 | * @param array<string,string> $params  | 
            ||
| 344 | *  | 
            ||
| 345 | * @return void  | 
            ||
| 346 | */  | 
            ||
| 347 | public function updateRecord(GedcomRecord $record, array $params): void  | 
            ||
| 348 |     { | 
            ||
| 349 | $record->updateRecord($this->updateGedcom($record, $params), false);  | 
            ||
| 350 | }  | 
            ||
| 351 | |||
| 352 | /**  | 
            ||
| 353 | * @param GedcomRecord $record  | 
            ||
| 354 | * @param array<string,string> $params  | 
            ||
| 355 | *  | 
            ||
| 356 | * @return string  | 
            ||
| 357 | */  | 
            ||
| 358 | private function updateGedcom(GedcomRecord $record, array $params): string  | 
            ||
| 367 | }  | 
            ||
| 368 | |||
| 369 | /**  | 
            ||
| 370 | * Create a regular expression from the search pattern.  | 
            ||
| 371 | *  | 
            ||
| 372 | * @param array<string,string> $params  | 
            ||
| 373 | *  | 
            ||
| 374 | * @return string  | 
            ||
| 375 | */  | 
            ||
| 376 | private function createRegex(array $params): string  | 
            ||
| 377 |     { | 
            ||
| 378 | $search = $params['search-for'];  | 
            ||
| 379 | $method = $params['method'];  | 
            ||
| 407 | }  | 
            ||
| 408 | |||
| 409 | /**  | 
            ||
| 410 | * Create a regular expression from the search pattern.  | 
            ||
| 411 | *  | 
            ||
| 412 | * @param Builder $query  | 
            ||
| 413 | * @param string $column  | 
            ||
| 414 | * @param array<string,string> $params  | 
            ||
| 415 | *  | 
            ||
| 416 | * @return void  | 
            ||
| 417 | */  | 
            ||
| 418 | private function recordQuery(Builder $query, string $column, array $params): void  | 
            ||
| 445 |