Total Complexity | 43 |
Total Lines | 282 |
Duplicated Lines | 0 % |
Changes | 6 | ||
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 |
||
62 | class GedcomEditService |
||
63 | { |
||
64 | /** |
||
65 | * @param Tree $tree |
||
66 | * |
||
67 | * @return Collection<int,Fact> |
||
68 | */ |
||
69 | public function newFamilyFacts(Tree $tree): Collection |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @param Tree $tree |
||
80 | * @param string $sex |
||
81 | * @param array<string> $names |
||
82 | * |
||
83 | * @return Collection<int,Fact> |
||
84 | */ |
||
85 | public function newIndividualFacts(Tree $tree, string $sex, array $names): Collection |
||
86 | { |
||
87 | $dummy = Registry::individualFactory()->new('', '0 @@ INDI', null, $tree); |
||
88 | $tags = new Collection(explode(',', $tree->getPreference('QUICK_REQUIRED_FACTS'))); |
||
89 | $facts = $tags->map(fn (string $tag): Fact => $this->createNewFact($dummy, $tag)); |
||
90 | $sex_fact = new Collection([new Fact('1 SEX ' . $sex, $dummy, '')]); |
||
91 | $name_facts = Collection::make($names)->map(static fn (string $gedcom): Fact => new Fact($gedcom, $dummy, '')); |
||
92 | |||
93 | return $sex_fact->concat($name_facts)->concat(Fact::sortFacts($facts)); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param GedcomRecord $record |
||
98 | * @param string $tag |
||
99 | * |
||
100 | * @return Fact |
||
101 | */ |
||
102 | private function createNewFact(GedcomRecord $record, string $tag): Fact |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * Reassemble edited GEDCOM fields into a GEDCOM fact/event string. |
||
113 | * |
||
114 | * @param string $record_type |
||
115 | * @param array<string> $levels |
||
116 | * @param array<string> $tags |
||
117 | * @param array<string> $values |
||
118 | * @param bool $append - Are we appending to a level 0 record, or replacing a level 1 record? |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | public function editLinesToGedcom(string $record_type, array $levels, array $tags, array $values, bool $append = true): string |
||
123 | { |
||
124 | // Assert all arrays are the same size. |
||
125 | $count = count($levels); |
||
126 | assert($count > 0); |
||
127 | assert(count($tags) === $count); |
||
128 | assert(count($values) === $count); |
||
129 | |||
130 | $gedcom_lines = []; |
||
131 | $hierarchy = [$record_type]; |
||
132 | |||
133 | for ($i = 0; $i < $count; $i++) { |
||
134 | $hierarchy[$levels[$i]] = $tags[$i]; |
||
135 | |||
136 | $full_tag = implode(':', array_slice($hierarchy, 0, 1 + (int) $levels[$i])); |
||
137 | $element = Registry::elementFactory()->make($full_tag); |
||
138 | $values[$i] = $element->canonical($values[$i]); |
||
139 | |||
140 | // If "1 FACT Y" has a DATE or PLAC, then delete the value of Y |
||
141 | if ($levels[$i] === '1' && $values[$i] === 'Y') { |
||
142 | for ($j = $i + 1; $j < $count && $levels[$j] > $levels[$i]; ++$j) { |
||
143 | if ($levels[$j] === '2' && ($tags[$j] === 'DATE' || $tags[$j] === 'PLAC') && $values[$j] !== '') { |
||
144 | $values[$i] = ''; |
||
145 | break; |
||
146 | } |
||
147 | } |
||
148 | } |
||
149 | |||
150 | // Find the next tag at the same level. Check if any child tags have values. |
||
151 | $children_with_values = false; |
||
152 | for ($j = $i + 1; $j < $count && $levels[$j] > $levels[$i]; $j++) { |
||
153 | if ($values[$j] !== '') { |
||
154 | $children_with_values = true; |
||
155 | } |
||
156 | } |
||
157 | |||
158 | if ($values[$i] !== '' || $children_with_values && !$element instanceof AbstractXrefElement) { |
||
159 | if ($values[$i] === '') { |
||
160 | $gedcom_lines[] = $levels[$i] . ' ' . $tags[$i]; |
||
161 | } else { |
||
162 | // We use CONC for editing NOTE records. |
||
163 | if ($tags[$i] === 'CONC') { |
||
164 | $next_level = (int) $levels[$i]; |
||
165 | } else { |
||
166 | $next_level = 1 + (int) $levels[$i]; |
||
167 | } |
||
168 | |||
169 | $gedcom_lines[] = $levels[$i] . ' ' . $tags[$i] . ' ' . str_replace("\n", "\n" . $next_level . ' CONT ', $values[$i]); |
||
170 | } |
||
171 | } else { |
||
172 | $i = $j - 1; |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $gedcom = implode("\n", $gedcom_lines); |
||
177 | |||
178 | if ($append && $gedcom !== '') { |
||
179 | $gedcom = "\n" . $gedcom; |
||
180 | } |
||
181 | |||
182 | return $gedcom; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Add blank lines, to allow a user to add/edit new values. |
||
187 | * |
||
188 | * @param Fact $fact |
||
189 | * @param bool $include_hidden |
||
190 | * |
||
191 | * @return string |
||
192 | */ |
||
193 | public function insertMissingFactSubtags(Fact $fact, bool $include_hidden): string |
||
194 | { |
||
195 | // Merge CONT records onto their parent line. |
||
196 | $gedcom = preg_replace('/\n\d CONT ?/', "\r", $fact->gedcom()); |
||
197 | |||
198 | return $this->insertMissingLevels($fact->record()->tree(), $fact->tag(), $gedcom, $include_hidden); |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Add blank lines, to allow a user to add/edit new values. |
||
203 | * |
||
204 | * @param GedcomRecord $record |
||
205 | * @param bool $include_hidden |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | public function insertMissingRecordSubtags(GedcomRecord $record, bool $include_hidden): string |
||
210 | { |
||
211 | // Merge CONT records onto their parent line. |
||
212 | $gedcom = preg_replace('/\n\d CONT ?/', "\r", $record->gedcom()); |
||
213 | |||
214 | $gedcom = $this->insertMissingLevels($record->tree(), $record->tag(), $gedcom, $include_hidden); |
||
215 | |||
216 | // NOTE records have data at level 0. Move it to 1 CONC. |
||
217 | if ($record instanceof Note) { |
||
218 | return preg_replace('/^0 @[^@]+@ NOTE/', '1 CONC', $gedcom); |
||
219 | } |
||
220 | |||
221 | return preg_replace('/^0.*\n/', '', $gedcom); |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * List of facts/events to add to families and individuals. |
||
226 | * |
||
227 | * @param Family|Individual $record |
||
228 | * @param bool $include_hidden |
||
229 | * |
||
230 | * @return array<string> |
||
231 | */ |
||
232 | public function factsToAdd(GedcomRecord $record, bool $include_hidden): array |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @param Tree $tree |
||
253 | * @param string $tag |
||
254 | * @param string $gedcom |
||
255 | * @param bool $include_hidden |
||
256 | * |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function insertMissingLevels(Tree $tree, string $tag, string $gedcom, bool $include_hidden): string |
||
319 | } |
||
320 | |||
321 | /** |
||
322 | * List of tags to exclude when creating new data. |
||
323 | * |
||
324 | * @param string $tag |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | private function isHiddenTag(string $tag): bool |
||
346 |