| Total Complexity | 69 |
| Total Lines | 460 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like GedcomEditService 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 GedcomEditService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class GedcomEditService |
||
| 39 | { |
||
| 40 | /** @var string[] */ |
||
| 41 | public $glevels = []; |
||
| 42 | |||
| 43 | /** @var string[] */ |
||
| 44 | public $tag = []; |
||
| 45 | |||
| 46 | /** @var string[] */ |
||
| 47 | public $islink = []; |
||
| 48 | |||
| 49 | /** @var string[] */ |
||
| 50 | public $text = []; |
||
| 51 | |||
| 52 | /** @var string[] */ |
||
| 53 | protected $glevelsSOUR = []; |
||
| 54 | |||
| 55 | /** @var string[] */ |
||
| 56 | protected $tagSOUR = []; |
||
| 57 | |||
| 58 | /** @var string[] */ |
||
| 59 | protected $islinkSOUR = []; |
||
| 60 | |||
| 61 | /** @var string[] */ |
||
| 62 | protected $textSOUR = []; |
||
| 63 | |||
| 64 | /** @var string[] */ |
||
| 65 | protected $glevelsRest = []; |
||
| 66 | |||
| 67 | /** @var string[] */ |
||
| 68 | protected $tagRest = []; |
||
| 69 | |||
| 70 | /** @var string[] */ |
||
| 71 | protected $islinkRest = []; |
||
| 72 | |||
| 73 | /** @var string[] */ |
||
| 74 | protected $textRest = []; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * This function splits the $glevels, $tag, $islink, and $text arrays so that the |
||
| 78 | * entries associated with a SOUR record are separate from everything else. |
||
| 79 | * |
||
| 80 | * Input arrays: |
||
| 81 | * - $glevels[] - an array of the gedcom level for each line that was edited |
||
| 82 | * - $tag[] - an array of the tags for each gedcom line that was edited |
||
| 83 | * - $islink[] - an array of 1 or 0 values to indicate when the text is a link element |
||
| 84 | * - $text[] - an array of the text data for each line |
||
| 85 | * |
||
| 86 | * Output arrays: |
||
| 87 | * ** For the SOUR record: |
||
| 88 | * - $glevelsSOUR[] - an array of the gedcom level for each line that was edited |
||
| 89 | * - $tagSOUR[] - an array of the tags for each gedcom line that was edited |
||
| 90 | * - $islinkSOUR[] - an array of 1 or 0 values to indicate when the text is a link element |
||
| 91 | * - $textSOUR[] - an array of the text data for each line |
||
| 92 | * ** For the remaining records: |
||
| 93 | * - $glevelsRest[] - an array of the gedcom level for each line that was edited |
||
| 94 | * - $tagRest[] - an array of the tags for each gedcom line that was edited |
||
| 95 | * - $islinkRest[] - an array of 1 or 0 values to indicate when the text is a link element |
||
| 96 | * - $textRest[] - an array of the text data for each line |
||
| 97 | * |
||
| 98 | * @return void |
||
| 99 | */ |
||
| 100 | public function splitSource(): void |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Add new GEDCOM lines from the $xxxRest interface update arrays, which |
||
| 150 | * were produced by the splitSOUR() function. |
||
| 151 | * See the FunctionsEdit::handle_updatesges() function for details. |
||
| 152 | * |
||
| 153 | * @param string $inputRec |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function updateRest(string $inputRec): string |
||
| 158 | { |
||
| 159 | if (count($this->tagRest) === 0) { |
||
| 160 | return $inputRec; // No update required |
||
| 161 | } |
||
| 162 | |||
| 163 | // Save original interface update arrays before replacing them with the xxxRest ones |
||
| 164 | $glevelsSave = $this->glevels; |
||
| 165 | $tagSave = $this->tag; |
||
| 166 | $islinkSave = $this->islink; |
||
| 167 | $textSave = $this->text; |
||
| 168 | |||
| 169 | $this->glevels = $this->glevelsRest; |
||
| 170 | $this->tag = $this->tagRest; |
||
| 171 | $this->islink = $this->islinkRest; |
||
| 172 | $this->text = $this->textRest; |
||
| 173 | |||
| 174 | $myRecord = $this->handleUpdates($inputRec, 'no'); // Now do the update |
||
| 175 | |||
| 176 | // Restore the original interface update arrays (just in case ...) |
||
| 177 | $this->glevels = $glevelsSave; |
||
| 178 | $this->tag = $tagSave; |
||
| 179 | $this->islink = $islinkSave; |
||
| 180 | $this->text = $textSave; |
||
| 181 | |||
| 182 | return $myRecord; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Add new gedcom lines from interface update arrays |
||
| 187 | * The edit_interface and FunctionsEdit::add_simple_tag function produce the following |
||
| 188 | * arrays incoming from the $_POST form |
||
| 189 | * - $glevels[] - an array of the gedcom level for each line that was edited |
||
| 190 | * - $tag[] - an array of the tags for each gedcom line that was edited |
||
| 191 | * - $islink[] - an array of 1 or 0 values to tell whether the text is a link element and should be surrounded by @@ |
||
| 192 | * - $text[] - an array of the text data for each line |
||
| 193 | * With these arrays you can recreate the gedcom lines like this |
||
| 194 | * <code>$glevel[0].' '.$tag[0].' '.$text[0]</code> |
||
| 195 | * There will be an index in each of these arrays for each line of the gedcom |
||
| 196 | * fact that is being edited. |
||
| 197 | * If the $text[] array is empty for the given line, then it means that the |
||
| 198 | * user removed that line during editing or that the line is supposed to be |
||
| 199 | * empty (1 DEAT, 1 BIRT) for example. To know if the line should be removed |
||
| 200 | * there is a section of code that looks ahead to the next lines to see if there |
||
| 201 | * are sub lines. For example we don't want to remove the 1 DEAT line if it has |
||
| 202 | * a 2 PLAC or 2 DATE line following it. If there are no sub lines, then the line |
||
| 203 | * can be safely removed. |
||
| 204 | * |
||
| 205 | * @param string $newged the new gedcom record to add the lines to |
||
| 206 | * @param string $levelOverride Override GEDCOM level specified in $glevels[0] |
||
| 207 | * |
||
| 208 | * @return string The updated gedcom record |
||
| 209 | */ |
||
| 210 | public function handleUpdates(string $newged, $levelOverride = 'no'): string |
||
| 211 | { |
||
| 212 | if ($levelOverride === 'no') { |
||
| 213 | $levelAdjust = 0; |
||
| 214 | } else { |
||
| 215 | $levelAdjust = 1; |
||
| 216 | } |
||
| 217 | |||
| 218 | // Assert all arrays are the same size. |
||
| 219 | assert(count($this->glevels) === count($this->tag)); |
||
| 220 | assert(count($this->glevels) === count($this->text)); |
||
| 221 | assert(count($this->glevels) === count($this->islink)); |
||
| 222 | |||
| 223 | $count = count($this->glevels); |
||
| 224 | |||
| 225 | for ($j = 0; $j < $count; $j++) { |
||
| 226 | // Look for empty SOUR reference with non-empty sub-records. |
||
| 227 | // This can happen when the SOUR entry is deleted but its sub-records |
||
| 228 | // were incorrectly left intact. |
||
| 229 | // The sub-records should be deleted. |
||
| 230 | if ($this->tag[$j] === 'SOUR' && ($this->text[$j] === '@@' || $this->text[$j] === '')) { |
||
| 231 | $this->text[$j] = ''; |
||
| 232 | $k = $j + 1; |
||
| 233 | while ($k < $count && $this->glevels[$k] > $this->glevels[$j]) { |
||
| 234 | $this->text[$k] = ''; |
||
| 235 | $k++; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | if (trim($this->text[$j]) !== '') { |
||
| 240 | $pass = true; |
||
| 241 | } else { |
||
| 242 | //-- for facts with empty values they must have sub records |
||
| 243 | //-- this section checks if they have subrecords |
||
| 244 | $k = $j + 1; |
||
| 245 | $pass = false; |
||
| 246 | while ($k < $count && $this->glevels[$k] > $this->glevels[$j]) { |
||
| 247 | if ($this->text[$k] !== '') { |
||
| 248 | if ($this->tag[$j] !== 'OBJE' || $this->tag[$k] === 'FILE') { |
||
| 249 | $pass = true; |
||
| 250 | break; |
||
| 251 | } |
||
| 252 | } |
||
| 253 | $k++; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | //-- if the value is not empty or it has sub lines |
||
| 258 | //--- then write the line to the gedcom record |
||
| 259 | //-- we have to let some emtpy text lines pass through... (DEAT, BIRT, etc) |
||
| 260 | if ($pass) { |
||
| 261 | $newline = (int) $this->glevels[$j] + $levelAdjust . ' ' . $this->tag[$j]; |
||
| 262 | if ($this->text[$j] !== '') { |
||
| 263 | if ($this->islink[$j]) { |
||
| 264 | $newline .= ' @' . $this->text[$j] . '@'; |
||
| 265 | } else { |
||
| 266 | $newline .= ' ' . $this->text[$j]; |
||
| 267 | } |
||
| 268 | } |
||
| 269 | $next_level = 1 + (int) $this->glevels[$j] + $levelAdjust; |
||
| 270 | |||
| 271 | $newged .= "\n" . str_replace("\n", "\n" . $next_level . ' CONT ', $newline); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | return $newged; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Create a form to add a new fact. |
||
| 280 | * |
||
| 281 | * @param ServerRequestInterface $request |
||
| 282 | * @param Tree $tree |
||
| 283 | * @param string $fact |
||
| 284 | * |
||
| 285 | * @return string |
||
| 286 | */ |
||
| 287 | public function addNewFact(ServerRequestInterface $request, Tree $tree, $fact): string |
||
| 288 | { |
||
| 289 | $params = (array) $request->getParsedBody(); |
||
| 290 | |||
| 291 | $FACT = $params[$fact]; |
||
| 292 | $DATE = $params[$fact . '_DATE'] ?? ''; |
||
| 293 | $PLAC = $params[$fact . '_PLAC'] ?? ''; |
||
| 294 | |||
| 295 | if ($DATE !== '' || $PLAC !== '' || $FACT !== '' && $FACT !== 'Y') { |
||
| 296 | if ($FACT !== '' && $FACT !== 'Y') { |
||
| 297 | $gedrec = "\n1 " . $fact . ' ' . $FACT; |
||
| 298 | } else { |
||
| 299 | $gedrec = "\n1 " . $fact; |
||
| 300 | } |
||
| 301 | if ($DATE !== '') { |
||
| 302 | $gedrec .= "\n2 DATE " . $DATE; |
||
| 303 | } |
||
| 304 | if ($PLAC !== '') { |
||
| 305 | $gedrec .= "\n2 PLAC " . $PLAC; |
||
| 306 | |||
| 307 | if (preg_match_all('/(' . Gedcom::REGEX_TAG . ')/', $tree->getPreference('ADVANCED_PLAC_FACTS'), $match)) { |
||
| 308 | foreach ($match[1] as $tag) { |
||
| 309 | $TAG = $params[$fact . '_' . $tag]; |
||
| 310 | if ($TAG !== '') { |
||
| 311 | $gedrec .= "\n3 " . $tag . ' ' . $TAG; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | $LATI = $params[$fact . '_LATI'] ?? ''; |
||
| 316 | $LONG = $params[$fact . '_LONG'] ?? ''; |
||
| 317 | if ($LATI !== '' || $LONG !== '') { |
||
| 318 | $gedrec .= "\n3 MAP\n4 LATI " . $LATI . "\n4 LONG " . $LONG; |
||
| 319 | } |
||
| 320 | } |
||
| 321 | if ((bool) ($params['SOUR_' . $fact] ?? false)) { |
||
| 322 | return $this->updateSource($gedrec, 'yes'); |
||
| 323 | } |
||
| 324 | |||
| 325 | return $gedrec; |
||
| 326 | } |
||
| 327 | |||
| 328 | if ($FACT === 'Y') { |
||
| 329 | if ((bool) ($params['SOUR_' . $fact] ?? false)) { |
||
| 330 | return $this->updateSource("\n1 " . $fact . ' Y', 'yes'); |
||
| 331 | } |
||
| 332 | |||
| 333 | return "\n1 " . $fact . ' Y'; |
||
| 334 | } |
||
| 335 | |||
| 336 | return ''; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Add new GEDCOM lines from the $xxxSOUR interface update arrays, which |
||
| 341 | * were produced by the splitSOUR() function. |
||
| 342 | * See the FunctionsEdit::handle_updatesges() function for details. |
||
| 343 | * |
||
| 344 | * @param string $inputRec |
||
| 345 | * @param string $levelOverride |
||
| 346 | * |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | public function updateSource(string $inputRec, string $levelOverride = 'no'): string |
||
| 350 | { |
||
| 351 | if (count($this->tagSOUR) === 0) { |
||
| 352 | return $inputRec; // No update required |
||
| 353 | } |
||
| 354 | |||
| 355 | // Save original interface update arrays before replacing them with the xxxSOUR ones |
||
| 356 | $glevelsSave = $this->glevels; |
||
| 357 | $tagSave = $this->tag; |
||
| 358 | $islinkSave = $this->islink; |
||
| 359 | $textSave = $this->text; |
||
| 360 | |||
| 361 | $this->glevels = $this->glevelsSOUR; |
||
| 362 | $this->tag = $this->tagSOUR; |
||
| 363 | $this->islink = $this->islinkSOUR; |
||
| 364 | $this->text = $this->textSOUR; |
||
| 365 | |||
| 366 | $myRecord = $this->handleUpdates($inputRec, $levelOverride); // Now do the update |
||
| 367 | |||
| 368 | // Restore the original interface update arrays (just in case ...) |
||
| 369 | $this->glevels = $glevelsSave; |
||
| 370 | $this->tag = $tagSave; |
||
| 371 | $this->islink = $islinkSave; |
||
| 372 | $this->text = $textSave; |
||
| 373 | |||
| 374 | return $myRecord; |
||
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Create a form to add a sex record. |
||
| 379 | * |
||
| 380 | * @param ServerRequestInterface $request |
||
| 381 | * |
||
| 382 | * @return string |
||
| 383 | */ |
||
| 384 | public function addNewSex(ServerRequestInterface $request): string |
||
| 385 | { |
||
| 386 | $params = (array) $request->getParsedBody(); |
||
| 387 | |||
| 388 | switch ($params['SEX']) { |
||
| 389 | case 'M': |
||
| 390 | return "\n1 SEX M"; |
||
| 391 | case 'F': |
||
| 392 | return "\n1 SEX F"; |
||
| 393 | default: |
||
| 394 | return "\n1 SEX U"; |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Assemble the pieces of a newly created record into gedcom |
||
| 400 | * |
||
| 401 | * @param ServerRequestInterface $request |
||
| 402 | * @param Tree $tree |
||
| 403 | * |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function addNewName(ServerRequestInterface $request, Tree $tree): string |
||
| 439 | } |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Reassemble edited GEDCOM fields into a GEDCOM fact/event string. |
||
| 443 | * |
||
| 444 | * @param string $record_type |
||
| 445 | * @param array<string> $levels |
||
| 446 | * @param array<string> $tags |
||
| 447 | * @param array<string> $values |
||
| 448 | * |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function editLinesToGedcom(string $record_type, array $levels, array $tags, array $values): string |
||
| 500 |