Total Complexity | 54 |
Total Lines | 398 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like AbstractEditController 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 AbstractEditController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | abstract class AbstractEditController extends AbstractBaseController |
||
30 | { |
||
31 | /** @var string[] */ |
||
32 | protected $glevels = []; |
||
33 | |||
34 | /** @var string[] */ |
||
35 | protected $tag = []; |
||
36 | |||
37 | /** @var string[] */ |
||
38 | protected $islink = []; |
||
39 | |||
40 | /** @var string[] */ |
||
41 | protected $text = []; |
||
42 | |||
43 | /** @var string[] */ |
||
44 | protected $glevelsSOUR = []; |
||
45 | |||
46 | /** @var string[] */ |
||
47 | protected $tagSOUR = []; |
||
48 | |||
49 | /** @var string[] */ |
||
50 | protected $islinkSOUR = []; |
||
51 | |||
52 | /** @var string[] */ |
||
53 | protected $textSOUR = []; |
||
54 | |||
55 | /** @var string[] */ |
||
56 | protected $glevelsRest = []; |
||
57 | |||
58 | /** @var string[] */ |
||
59 | protected $tagRest = []; |
||
60 | |||
61 | /** @var string[] */ |
||
62 | protected $islinkRest = []; |
||
63 | |||
64 | /** @var string[] */ |
||
65 | protected $textRest = []; |
||
66 | |||
67 | /** |
||
68 | * This function splits the $glevels, $tag, $islink, and $text arrays so that the |
||
69 | * entries associated with a SOUR record are separate from everything else. |
||
70 | * |
||
71 | * Input arrays: |
||
72 | * - $glevels[] - an array of the gedcom level for each line that was edited |
||
73 | * - $tag[] - an array of the tags for each gedcom line that was edited |
||
74 | * - $islink[] - an array of 1 or 0 values to indicate when the text is a link element |
||
75 | * - $text[] - an array of the text data for each line |
||
76 | * |
||
77 | * Output arrays: |
||
78 | * ** For the SOUR record: |
||
79 | * - $glevelsSOUR[] - an array of the gedcom level for each line that was edited |
||
80 | * - $tagSOUR[] - an array of the tags for each gedcom line that was edited |
||
81 | * - $islinkSOUR[] - an array of 1 or 0 values to indicate when the text is a link element |
||
82 | * - $textSOUR[] - an array of the text data for each line |
||
83 | * ** For the remaining records: |
||
84 | * - $glevelsRest[] - an array of the gedcom level for each line that was edited |
||
85 | * - $tagRest[] - an array of the tags for each gedcom line that was edited |
||
86 | * - $islinkRest[] - an array of 1 or 0 values to indicate when the text is a link element |
||
87 | * - $textRest[] - an array of the text data for each line |
||
88 | * |
||
89 | * @return void |
||
90 | */ |
||
91 | protected function splitSource(): void |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Add new GEDCOM lines from the $xxxRest interface update arrays, which |
||
141 | * were produced by the splitSOUR() function. |
||
142 | * See the FunctionsEdit::handle_updatesges() function for details. |
||
143 | * |
||
144 | * @param string $inputRec |
||
145 | * @param string $levelOverride |
||
146 | * |
||
147 | * @return string |
||
148 | */ |
||
149 | protected function updateRest(string $inputRec): string |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Add new gedcom lines from interface update arrays |
||
179 | * The edit_interface and FunctionsEdit::add_simple_tag function produce the following |
||
180 | * arrays incoming from the $_POST form |
||
181 | * - $glevels[] - an array of the gedcom level for each line that was edited |
||
182 | * - $tag[] - an array of the tags for each gedcom line that was edited |
||
183 | * - $islink[] - an array of 1 or 0 values to tell whether the text is a link element and should be surrounded by @@ |
||
184 | * - $text[] - an array of the text data for each line |
||
185 | * With these arrays you can recreate the gedcom lines like this |
||
186 | * <code>$glevel[0].' '.$tag[0].' '.$text[0]</code> |
||
187 | * There will be an index in each of these arrays for each line of the gedcom |
||
188 | * fact that is being edited. |
||
189 | * If the $text[] array is empty for the given line, then it means that the |
||
190 | * user removed that line during editing or that the line is supposed to be |
||
191 | * empty (1 DEAT, 1 BIRT) for example. To know if the line should be removed |
||
192 | * there is a section of code that looks ahead to the next lines to see if there |
||
193 | * are sub lines. For example we don't want to remove the 1 DEAT line if it has |
||
194 | * a 2 PLAC or 2 DATE line following it. If there are no sub lines, then the line |
||
195 | * can be safely removed. |
||
196 | * |
||
197 | * @param string $newged the new gedcom record to add the lines to |
||
198 | * @param string $levelOverride Override GEDCOM level specified in $glevels[0] |
||
199 | * |
||
200 | * @return string The updated gedcom record |
||
201 | */ |
||
202 | protected function handleUpdates(string $newged, $levelOverride = 'no'): string |
||
203 | { |
||
204 | if ($levelOverride === 'no') { |
||
205 | $levelAdjust = 0; |
||
206 | } else { |
||
207 | $levelAdjust = 1; |
||
208 | } |
||
209 | |||
210 | // Assert all arrays are the same size. |
||
211 | assert(count($this->glevels) === count($this->tag)); |
||
212 | assert(count($this->glevels) === count($this->text)); |
||
213 | assert(count($this->glevels) === count($this->islink)); |
||
214 | |||
215 | $count = count($this->glevels); |
||
216 | |||
217 | for ($j = 0; $j < $count; $j++) { |
||
218 | // Look for empty SOUR reference with non-empty sub-records. |
||
219 | // This can happen when the SOUR entry is deleted but its sub-records |
||
220 | // were incorrectly left intact. |
||
221 | // The sub-records should be deleted. |
||
222 | if ($this->tag[$j] === 'SOUR' && ($this->text[$j] === '@@' || $this->text[$j] === '')) { |
||
223 | $this->text[$j] = ''; |
||
224 | $k = $j + 1; |
||
225 | while ($k < $count && $this->glevels[$k] > $this->glevels[$j]) { |
||
226 | $this->text[$k] = ''; |
||
227 | $k++; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | if (trim($this->text[$j]) !== '') { |
||
232 | $pass = true; |
||
233 | } else { |
||
234 | //-- for facts with empty values they must have sub records |
||
235 | //-- this section checks if they have subrecords |
||
236 | $k = $j + 1; |
||
237 | $pass = false; |
||
238 | while ($k < $count && $this->glevels[$k] > $this->glevels[$j]) { |
||
239 | if ($this->text[$k] !== '') { |
||
240 | if (($this->tag[$j] !== 'OBJE') || ($this->tag[$k] === 'FILE')) { |
||
241 | $pass = true; |
||
242 | break; |
||
243 | } |
||
244 | } |
||
245 | $k++; |
||
246 | } |
||
247 | } |
||
248 | |||
249 | //-- if the value is not empty or it has sub lines |
||
250 | //--- then write the line to the gedcom record |
||
251 | //-- we have to let some emtpy text lines pass through... (DEAT, BIRT, etc) |
||
252 | if ($pass) { |
||
253 | $newline = (int) $this->glevels[$j] + $levelAdjust . ' ' . $this->tag[$j]; |
||
254 | if ($this->text[$j] !== '') { |
||
255 | if ($this->islink[$j]) { |
||
256 | $newline .= ' @' . $this->text[$j] . '@'; |
||
257 | } else { |
||
258 | $newline .= ' ' . $this->text[$j]; |
||
259 | } |
||
260 | } |
||
261 | $newged .= "\n" . str_replace("\n", "\n" . (1 + substr($newline, 0, 1)) . ' CONT ', $newline); |
||
262 | } |
||
263 | } |
||
264 | |||
265 | return $newged; |
||
266 | } |
||
267 | |||
268 | /** |
||
269 | * Create a form to add a new fact. |
||
270 | * |
||
271 | * @param ServerRequestInterface $request |
||
272 | * @param Tree $tree |
||
273 | * @param string $fact |
||
274 | * |
||
275 | * @return string |
||
276 | */ |
||
277 | protected function addNewFact(ServerRequestInterface $request, Tree $tree, $fact): string |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Add new GEDCOM lines from the $xxxSOUR interface update arrays, which |
||
331 | * were produced by the splitSOUR() function. |
||
332 | * See the FunctionsEdit::handle_updatesges() function for details. |
||
333 | * |
||
334 | * @param string $inputRec |
||
335 | * @param string $levelOverride |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | protected function updateSource(string $inputRec, string $levelOverride = 'no'): string |
||
340 | { |
||
341 | if (count($this->tagSOUR) === 0) { |
||
342 | return $inputRec; // No update required |
||
343 | } |
||
344 | |||
345 | // Save original interface update arrays before replacing them with the xxxSOUR ones |
||
346 | $glevelsSave = $this->glevels; |
||
347 | $tagSave = $this->tag; |
||
348 | $islinkSave = $this->islink; |
||
349 | $textSave = $this->text; |
||
350 | |||
351 | $this->glevels = $this->glevelsSOUR; |
||
352 | $this->tag = $this->tagSOUR; |
||
353 | $this->islink = $this->islinkSOUR; |
||
354 | $this->text = $this->textSOUR; |
||
355 | |||
356 | $myRecord = $this->handleUpdates($inputRec, $levelOverride); // Now do the update |
||
357 | |||
358 | // Restore the original interface update arrays (just in case ...) |
||
359 | $this->glevels = $glevelsSave; |
||
360 | $this->tag = $tagSave; |
||
361 | $this->islink = $islinkSave; |
||
362 | $this->text = $textSave; |
||
363 | |||
364 | return $myRecord; |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Create a form to add a sex record. |
||
369 | * |
||
370 | * @param ServerRequestInterface $request |
||
371 | * |
||
372 | * @return string |
||
373 | */ |
||
374 | protected function addNewSex(ServerRequestInterface $request): string |
||
383 | } |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * Assemble the pieces of a newly created record into gedcom |
||
388 | * |
||
389 | * @param ServerRequestInterface $request |
||
390 | * @param Tree $tree |
||
391 | * |
||
392 | * @return string |
||
393 | */ |
||
394 | protected function addNewName(ServerRequestInterface $request, Tree $tree): string |
||
429 |