| Total Complexity | 49 |
| Total Lines | 319 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 63 | class GedcomEditService |
||
| 64 | { |
||
| 65 | /** |
||
| 66 | * @param Tree $tree |
||
| 67 | * |
||
| 68 | * @return Collection<int,Fact> |
||
| 69 | */ |
||
| 70 | public function newFamilyFacts(Tree $tree): Collection |
||
| 71 | { |
||
| 72 | $dummy = Registry::familyFactory()->new('', '0 @@ FAM', null, $tree); |
||
| 73 | $tags = (new Collection(explode(',', $tree->getPreference('QUICK_REQUIRED_FAMFACTS')))) |
||
| 74 | ->filter(static fn (string $tag): bool => $tag !== ''); |
||
| 75 | $facts = $tags->map(fn (string $tag): Fact => $this->createNewFact($dummy, $tag)); |
||
| 76 | |||
| 77 | return Fact::sortFacts($facts); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param Tree $tree |
||
| 82 | * @param string $sex |
||
| 83 | * @param array<string> $names |
||
| 84 | * |
||
| 85 | * @return Collection<int,Fact> |
||
| 86 | */ |
||
| 87 | public function newIndividualFacts(Tree $tree, string $sex, array $names): Collection |
||
| 88 | { |
||
| 89 | $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); |
||
| 90 | $tags = (new Collection(explode(',', $tree->getPreference('QUICK_REQUIRED_FACTS')))) |
||
| 91 | ->filter(static fn (string $tag): bool => $tag !== ''); |
||
| 92 | $facts = $tags->map(fn (string $tag): Fact => $this->createNewFact($dummy, $tag)); |
||
| 93 | $sex_fact = new Collection([new Fact('1 SEX ' . $sex, $dummy, '')]); |
||
| 94 | $name_facts = Collection::make($names)->map(static fn (string $gedcom): Fact => new Fact($gedcom, $dummy, '')); |
||
| 95 | |||
| 96 | return $sex_fact->concat($name_facts)->concat(Fact::sortFacts($facts)); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param GedcomRecord $record |
||
| 101 | * @param string $tag |
||
| 102 | * |
||
| 103 | * @return Fact |
||
| 104 | */ |
||
| 105 | private function createNewFact(GedcomRecord $record, string $tag): Fact |
||
| 106 | { |
||
| 107 | $element = Registry::elementFactory()->make($record->tag() . ':' . $tag); |
||
| 108 | $default = $element->default($record->tree()); |
||
| 109 | $gedcom = trim('1 ' . $tag . ' ' . $default); |
||
| 110 | |||
| 111 | return new Fact($gedcom, $record, ''); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Reassemble edited GEDCOM fields into a GEDCOM fact/event string. |
||
| 116 | * |
||
| 117 | * @param string $record_type |
||
| 118 | * @param array<string> $levels |
||
| 119 | * @param array<string> $tags |
||
| 120 | * @param array<string> $values |
||
| 121 | * @param bool $append Are we appending to a level 0 record, or replacing a level 1 record? |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function editLinesToGedcom(string $record_type, array $levels, array $tags, array $values, bool $append = true): string |
||
| 126 | { |
||
| 127 | // Assert all arrays are the same size. |
||
| 128 | $count = count($levels); |
||
| 129 | assert($count > 0); |
||
| 130 | assert(count($tags) === $count); |
||
| 131 | assert(count($values) === $count); |
||
| 132 | |||
| 133 | $gedcom_lines = []; |
||
| 134 | $hierarchy = [$record_type]; |
||
| 135 | |||
| 136 | for ($i = 0; $i < $count; $i++) { |
||
| 137 | $hierarchy[$levels[$i]] = $tags[$i]; |
||
| 138 | |||
| 139 | $full_tag = implode(':', array_slice($hierarchy, 0, 1 + (int) $levels[$i])); |
||
| 140 | $element = Registry::elementFactory()->make($full_tag); |
||
| 141 | $values[$i] = $element->canonical($values[$i]); |
||
| 142 | |||
| 143 | // If "1 FACT Y" has a DATE or PLAC, then delete the value of Y |
||
| 144 | if ($levels[$i] === '1' && $values[$i] === 'Y') { |
||
| 145 | for ($j = $i + 1; $j < $count && $levels[$j] > $levels[$i]; ++$j) { |
||
| 146 | if ($levels[$j] === '2' && ($tags[$j] === 'DATE' || $tags[$j] === 'PLAC') && $values[$j] !== '') { |
||
| 147 | $values[$i] = ''; |
||
| 148 | break; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | // Find the next tag at the same level. Check if any child tags have values. |
||
| 154 | $children_with_values = false; |
||
| 155 | for ($j = $i + 1; $j < $count && $levels[$j] > $levels[$i]; $j++) { |
||
| 156 | if ($values[$j] !== '') { |
||
| 157 | $children_with_values = true; |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | if ($values[$i] !== '' || $children_with_values && !$element instanceof AbstractXrefElement) { |
||
| 162 | if ($values[$i] === '') { |
||
| 163 | $gedcom_lines[] = $levels[$i] . ' ' . $tags[$i]; |
||
| 164 | } else { |
||
| 165 | // We use CONC for editing NOTE records. |
||
| 166 | if ($tags[$i] === 'CONC') { |
||
| 167 | $next_level = (int) $levels[$i]; |
||
| 168 | } else { |
||
| 169 | $next_level = 1 + (int) $levels[$i]; |
||
| 170 | } |
||
| 171 | |||
| 172 | $gedcom_lines[] = $levels[$i] . ' ' . $tags[$i] . ' ' . str_replace("\n", "\n" . $next_level . ' CONT ', $values[$i]); |
||
| 173 | } |
||
| 174 | } else { |
||
| 175 | $i = $j - 1; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | $gedcom = implode("\n", $gedcom_lines); |
||
| 180 | |||
| 181 | if ($append && $gedcom !== '') { |
||
| 182 | $gedcom = "\n" . $gedcom; |
||
| 183 | } |
||
| 184 | |||
| 185 | return $gedcom; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Add blank lines, to allow a user to add/edit new values. |
||
| 190 | * |
||
| 191 | * @param Fact $fact |
||
| 192 | * @param bool $include_hidden |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function insertMissingFactSubtags(Fact $fact, bool $include_hidden): string |
||
| 197 | { |
||
| 198 | // Merge CONT records onto their parent line. |
||
| 199 | $gedcom = preg_replace('/\n\d CONT ?/', "\r", $fact->gedcom()); |
||
| 200 | |||
| 201 | return $this->insertMissingLevels($fact->record()->tree(), $fact->tag(), $gedcom, $include_hidden); |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Add blank lines, to allow a user to add/edit new values. |
||
| 206 | * |
||
| 207 | * @param GedcomRecord $record |
||
| 208 | * @param bool $include_hidden |
||
| 209 | * |
||
| 210 | * @return string |
||
| 211 | */ |
||
| 212 | public function insertMissingRecordSubtags(GedcomRecord $record, bool $include_hidden): string |
||
| 213 | { |
||
| 214 | // Merge CONT records onto their parent line. |
||
| 215 | $gedcom = preg_replace('/\n\d CONT ?/', "\r", $record->gedcom()); |
||
| 216 | |||
| 217 | $gedcom = $this->insertMissingLevels($record->tree(), $record->tag(), $gedcom, $include_hidden); |
||
| 218 | |||
| 219 | // NOTE records have data at level 0. Move it to 1 CONC. |
||
| 220 | if ($record instanceof Note) { |
||
| 221 | return preg_replace('/^0 @[^@]+@ NOTE/', '1 CONC', $gedcom); |
||
| 222 | } |
||
| 223 | |||
| 224 | return preg_replace('/^0.*\n/', '', $gedcom); |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * List of facts/events to add to families and individuals. |
||
| 229 | * |
||
| 230 | * @param Family|Individual $record |
||
| 231 | * @param bool $include_hidden |
||
| 232 | * |
||
| 233 | * @return array<string> |
||
| 234 | */ |
||
| 235 | public function factsToAdd(Family|Individual $record, bool $include_hidden): array |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @param Tree $tree |
||
| 256 | * @param string $tag |
||
| 257 | * @param string $gedcom |
||
| 258 | * @param bool $include_hidden |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | protected function insertMissingLevels(Tree $tree, string $tag, string $gedcom, bool $include_hidden): string |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * List of tags to exclude when creating new data. |
||
| 326 | * |
||
| 327 | * @param string $tag |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | private function isHiddenTag(string $tag): bool |
||
| 347 | } |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Add a child to a family. |
||
| 351 | * |
||
| 352 | * @param Individual $child |
||
| 353 | * @param Family $family |
||
| 354 | * |
||
| 355 | * @return void |
||
| 356 | */ |
||
| 357 | public function addChildToFamily(Individual $child, Family $family): void |
||
| 382 | } |
||
| 383 | |||
| 385 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths