| Total Complexity | 40 |
| Total Lines | 273 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 60 | class GedcomEditService |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @param Tree $tree |
||
| 64 | * |
||
| 65 | * @return Collection<Fact> |
||
| 66 | */ |
||
| 67 | public function newFamilyFacts(Tree $tree): Collection |
||
| 68 | { |
||
| 69 | $dummy = Registry::familyFactory()->new('', '0 @@ FAM', null, $tree); |
||
| 70 | $tags = new Collection(explode(',', $tree->getPreference('QUICK_REQUIRED_FAMFACTS'))); |
||
| 71 | $facts = $tags->map(fn (string $tag): Fact => $this->createNewFact($dummy, $tag)); |
||
| 72 | |||
| 73 | return Fact::sortFacts($facts); |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param Tree $tree |
||
| 78 | * @param string $sex |
||
| 79 | * @param array<string> $names |
||
| 80 | * |
||
| 81 | * @return Collection<Fact> |
||
| 82 | */ |
||
| 83 | public function newIndividualFacts(Tree $tree, string $sex, array $names): Collection |
||
| 84 | { |
||
| 85 | $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); |
||
| 86 | $tags = new Collection(explode(',', $tree->getPreference('QUICK_REQUIRED_FACTS'))); |
||
| 87 | $facts = $tags->map(fn (string $tag): Fact => $this->createNewFact($dummy, $tag)); |
||
| 88 | $sex_fact = new Collection([new Fact('1 SEX ' . $sex, $dummy, '')]); |
||
| 89 | $name_facts = Collection::make($names)->map(static fn (string $gedcom): Fact => new Fact($gedcom, $dummy, '')); |
||
| 90 | |||
| 91 | return $sex_fact->concat($name_facts)->concat(Fact::sortFacts($facts)); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param GedcomRecord $record |
||
| 96 | * @param string $tag |
||
| 97 | * |
||
| 98 | * @return Fact |
||
| 99 | */ |
||
| 100 | private function createNewFact(GedcomRecord $record, string $tag): Fact |
||
| 101 | { |
||
| 102 | $element = Registry::elementFactory()->make($record->tag() . ':' . $tag); |
||
| 103 | $default = $element->default($record->tree()); |
||
| 104 | $gedcom = trim('1 ' . $tag . ' ' . $default); |
||
| 105 | |||
| 106 | return new Fact($gedcom, $record, ''); |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Reassemble edited GEDCOM fields into a GEDCOM fact/event string. |
||
| 111 | * |
||
| 112 | * @param string $record_type |
||
| 113 | * @param array<string> $levels |
||
| 114 | * @param array<string> $tags |
||
| 115 | * @param array<string> $values |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | public function editLinesToGedcom(string $record_type, array $levels, array $tags, array $values): string |
||
| 120 | { |
||
| 121 | // Assert all arrays are the same size. |
||
| 122 | $count = count($levels); |
||
| 123 | assert($count > 0); |
||
| 124 | assert(count($tags) === $count); |
||
| 125 | assert(count($values) === $count); |
||
| 126 | |||
| 127 | $gedcom_lines = []; |
||
| 128 | $hierarchy = [$record_type]; |
||
| 129 | |||
| 130 | for ($i = 0; $i < $count; $i++) { |
||
| 131 | $hierarchy[$levels[$i]] = $tags[$i]; |
||
| 132 | |||
| 133 | $full_tag = implode(':', array_slice($hierarchy, 0, 1 + (int) $levels[$i])); |
||
| 134 | $element = Registry::elementFactory()->make($full_tag); |
||
| 135 | $values[$i] = $element->canonical($values[$i]); |
||
| 136 | |||
| 137 | // If "1 FACT Y" has a DATE or PLAC, then delete the value of Y |
||
| 138 | if ($levels[$i] === '1' && $values[$i] === 'Y') { |
||
| 139 | for ($j = $i + 1; $j < $count && $levels[$j] > $levels[$i]; ++$j) { |
||
| 140 | if ($levels[$j] === '2' && ($tags[$j] === 'DATE' || $tags[$j] === 'PLAC') && $values[$j] !== '') { |
||
| 141 | $values[$i] = ''; |
||
| 142 | break; |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | // Find the next tag at the same level. Check if any child tags have values. |
||
| 148 | $children_with_values = false; |
||
| 149 | for ($j = $i + 1; $j < $count && $levels[$j] > $levels[$i]; $j++) { |
||
| 150 | if ($values[$j] !== '') { |
||
| 151 | $children_with_values = true; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | |||
| 155 | if ($values[$i] !== '' || $children_with_values && !$element instanceof AbstractXrefElement) { |
||
| 156 | if ($values[$i] === '') { |
||
| 157 | $gedcom_lines[] = $levels[$i] . ' ' . $tags[$i]; |
||
| 158 | } else { |
||
| 159 | // We use CONC for editing NOTE records. |
||
| 160 | if ($tags[$i] === 'CONC') { |
||
| 161 | $next_level = (int) $levels[$i]; |
||
| 162 | } else { |
||
| 163 | $next_level = 1 + (int) $levels[$i]; |
||
| 164 | } |
||
| 165 | |||
| 166 | $gedcom_lines[] = $levels[$i] . ' ' . $tags[$i] . ' ' . str_replace("\n", "\n" . $next_level . ' CONT ', $values[$i]); |
||
| 167 | } |
||
| 168 | } else { |
||
| 169 | $i = $j - 1; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | |||
| 173 | return implode("\n", $gedcom_lines); |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Add blank lines, to allow a user to add/edit new values. |
||
| 178 | * |
||
| 179 | * @param Fact $fact |
||
| 180 | * @param bool $include_hidden |
||
| 181 | * |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function insertMissingFactSubtags(Fact $fact, bool $include_hidden): string |
||
| 185 | { |
||
| 186 | return $this->insertMissingLevels($fact->record()->tree(), $fact->tag(), $fact->gedcom(), $include_hidden); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Add blank lines, to allow a user to add/edit new values. |
||
| 191 | * |
||
| 192 | * @param GedcomRecord $record |
||
| 193 | * @param bool $include_hidden |
||
| 194 | * |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function insertMissingRecordSubtags(GedcomRecord $record, bool $include_hidden): string |
||
| 198 | { |
||
| 199 | $gedcom = $this->insertMissingLevels($record->tree(), $record->tag(), $record->gedcom(), $include_hidden); |
||
| 200 | |||
| 201 | // NOTE records have data at level 0. Move it to 1 CONC. |
||
| 202 | if ($record instanceof Note) { |
||
| 203 | return preg_replace('/^0 @[^@]+@ NOTE/', '1 CONC', $gedcom); |
||
| 204 | } |
||
| 205 | |||
| 206 | return preg_replace('/^0.*\n/', '', $gedcom); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * List of facts/events to add to families and individuals. |
||
| 211 | * |
||
| 212 | * @param Family|Individual $record |
||
| 213 | * @param bool $include_hidden |
||
| 214 | * |
||
| 215 | * @return array<string> |
||
| 216 | */ |
||
| 217 | public function factsToAdd(GedcomRecord $record, bool $include_hidden): array |
||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param Tree $tree |
||
| 238 | * @param string $tag |
||
| 239 | * @param string $gedcom |
||
| 240 | * @param bool $include_hidden |
||
| 241 | * |
||
| 242 | * @return string |
||
| 243 | */ |
||
| 244 | protected function insertMissingLevels(Tree $tree, string $tag, string $gedcom, bool $include_hidden): string |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * List of tags to exclude when creating new data. |
||
| 312 | * |
||
| 313 | * @param string $tag |
||
| 314 | * |
||
| 315 | * @return bool |
||
| 316 | */ |
||
| 317 | private function isHiddenTag(string $tag): bool |
||
| 333 | } |
||
| 334 | } |
||
| 335 |