Total Complexity | 55 |
Total Lines | 379 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Family 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 Family, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class Family extends GedcomRecord |
||
30 | { |
||
31 | public const RECORD_TYPE = 'FAM'; |
||
32 | |||
33 | protected const ROUTE_NAME = FamilyPage::class; |
||
34 | |||
35 | // The husband (or first spouse for same-sex couples) |
||
36 | private Individual|null $husb = null; |
||
37 | |||
38 | // The wife (or second spouse for same-sex couples) |
||
39 | private Individual|null $wife = null; |
||
40 | |||
41 | /** |
||
42 | * Create a GedcomRecord object from raw GEDCOM data. |
||
43 | * |
||
44 | * @param string $xref |
||
45 | * @param string $gedcom an empty string for new/pending records |
||
46 | * @param string|null $pending null for a record with no pending edits, |
||
47 | * empty string for records with pending deletions |
||
48 | * @param Tree $tree |
||
49 | */ |
||
50 | public function __construct(string $xref, string $gedcom, string|null $pending, Tree $tree) |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * A closure which will compare families by marriage date. |
||
67 | * |
||
68 | * @return Closure(Family,Family):int |
||
69 | */ |
||
70 | public static function marriageDateComparator(): Closure |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Get the male (or first female) partner of the family |
||
77 | * |
||
78 | * @param int|null $access_level |
||
79 | * |
||
80 | * @return Individual|null |
||
81 | */ |
||
82 | public function husband(int|null $access_level = null): Individual|null |
||
83 | { |
||
84 | if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { |
||
85 | $access_level = Auth::PRIV_HIDE; |
||
86 | } |
||
87 | |||
88 | if ($this->husb instanceof Individual && $this->husb->canShowName($access_level)) { |
||
89 | return $this->husb; |
||
90 | } |
||
91 | |||
92 | return null; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Get the female (or second male) partner of the family |
||
97 | * |
||
98 | * @param int|null $access_level |
||
99 | * |
||
100 | * @return Individual|null |
||
101 | */ |
||
102 | public function wife(int|null $access_level = null): Individual|null |
||
103 | { |
||
104 | if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { |
||
105 | $access_level = Auth::PRIV_HIDE; |
||
106 | } |
||
107 | |||
108 | if ($this->wife instanceof Individual && $this->wife->canShowName($access_level)) { |
||
109 | return $this->wife; |
||
110 | } |
||
111 | |||
112 | return null; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Each object type may have its own special rules, and re-implement this function. |
||
117 | * |
||
118 | * @param int $access_level |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | protected function canShowByType(int $access_level): bool |
||
123 | { |
||
124 | // Hide a family if any member is private |
||
125 | preg_match_all('/\n1 (?:CHIL|HUSB|WIFE) @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom, $matches); |
||
126 | foreach ($matches[1] as $match) { |
||
127 | $person = Registry::individualFactory()->make($match, $this->tree); |
||
128 | if ($person && !$person->canShow($access_level)) { |
||
129 | return false; |
||
130 | } |
||
131 | } |
||
132 | |||
133 | return true; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Can the name of this record be shown? |
||
138 | * |
||
139 | * @param int|null $access_level |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function canShowName(int|null $access_level = null): bool |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Find the spouse of a person. |
||
152 | * |
||
153 | * @param Individual $person |
||
154 | * @param int|null $access_level |
||
155 | * |
||
156 | * @return Individual|null |
||
157 | */ |
||
158 | public function spouse(Individual $person, int|null $access_level = null): Individual|null |
||
159 | { |
||
160 | if ($person === $this->wife) { |
||
161 | return $this->husband($access_level); |
||
162 | } |
||
163 | |||
164 | return $this->wife($access_level); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * Get the (zero, one or two) spouses from this family. |
||
169 | * |
||
170 | * @param int|null $access_level |
||
171 | * |
||
172 | * @return Collection<int,Individual> |
||
173 | */ |
||
174 | public function spouses(int|null $access_level = null): Collection |
||
175 | { |
||
176 | $spouses = new Collection([ |
||
|
|||
177 | $this->husband($access_level), |
||
178 | $this->wife($access_level), |
||
179 | ]); |
||
180 | |||
181 | return $spouses->filter(); |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Get a list of this family’s children. |
||
186 | * |
||
187 | * @param int|null $access_level |
||
188 | * |
||
189 | * @return Collection<int,Individual> |
||
190 | */ |
||
191 | public function children(int|null $access_level = null): Collection |
||
192 | { |
||
193 | $access_level ??= Auth::accessLevel($this->tree); |
||
194 | |||
195 | if ($this->tree->getPreference('SHOW_PRIVATE_RELATIONSHIPS') === '1') { |
||
196 | $access_level = Auth::PRIV_HIDE; |
||
197 | } |
||
198 | |||
199 | $children = new Collection(); |
||
200 | |||
201 | foreach ($this->facts(['CHIL'], false, $access_level) as $fact) { |
||
202 | $child = $fact->target(); |
||
203 | |||
204 | if ($child instanceof Individual && $child->canShowName($access_level)) { |
||
205 | $children->push($child); |
||
206 | } |
||
207 | } |
||
208 | |||
209 | return $children; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Number of children - for the individual list |
||
214 | * |
||
215 | * @return int |
||
216 | */ |
||
217 | public function numberOfChildren(): int |
||
218 | { |
||
219 | $nchi = $this->children()->count(); |
||
220 | |||
221 | foreach ($this->facts(['NCHI']) as $fact) { |
||
222 | $nchi = max($nchi, (int) $fact->value()); |
||
223 | } |
||
224 | |||
225 | return $nchi; |
||
226 | } |
||
227 | |||
228 | /** |
||
229 | * get the marriage event |
||
230 | */ |
||
231 | public function getMarriage(): Fact|null |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Get marriage date |
||
238 | * |
||
239 | * @return Date |
||
240 | */ |
||
241 | public function getMarriageDate(): Date |
||
242 | { |
||
243 | $marriage = $this->getMarriage(); |
||
244 | if ($marriage instanceof Fact) { |
||
245 | return $marriage->date(); |
||
246 | } |
||
247 | |||
248 | return new Date(''); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Get the marriage year - displayed on lists of families |
||
253 | * |
||
254 | * @return int |
||
255 | */ |
||
256 | public function getMarriageYear(): int |
||
257 | { |
||
258 | return $this->getMarriageDate()->minimumDate()->year; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Get the marriage place |
||
263 | * |
||
264 | * @return Place |
||
265 | */ |
||
266 | public function getMarriagePlace(): Place |
||
267 | { |
||
268 | $marriage = $this->getMarriage(); |
||
269 | |||
270 | if ($marriage instanceof Fact) { |
||
271 | return $marriage->place(); |
||
272 | } |
||
273 | |||
274 | return new Place('', $this->tree); |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Get a list of all marriage dates - for the family lists. |
||
279 | * |
||
280 | * @return array<Date> |
||
281 | */ |
||
282 | public function getAllMarriageDates(): array |
||
283 | { |
||
284 | foreach (Gedcom::MARRIAGE_EVENTS as $event) { |
||
285 | $array = $this->getAllEventDates([$event]); |
||
286 | |||
287 | if ($array !== []) { |
||
288 | return $array; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | return []; |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Get a list of all marriage places - for the family lists. |
||
297 | * |
||
298 | * @return array<Place> |
||
299 | */ |
||
300 | public function getAllMarriagePlaces(): array |
||
301 | { |
||
302 | foreach (Gedcom::MARRIAGE_EVENTS as $event) { |
||
303 | $places = $this->getAllEventPlaces([$event]); |
||
304 | if ($places !== []) { |
||
305 | return $places; |
||
306 | } |
||
307 | } |
||
308 | |||
309 | return []; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Derived classes should redefine this function, otherwise the object will have no name |
||
314 | * |
||
315 | * @return array<int,array<string,string>> |
||
316 | */ |
||
317 | public function getAllNames(): array |
||
383 | } |
||
384 | |||
385 | /** |
||
386 | * This function should be redefined in derived classes to show any major |
||
387 | * identifying characteristics of this record. |
||
388 | * |
||
389 | * @return string |
||
390 | */ |
||
391 | public function formatListDetails(): string |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * Lock the database row, to prevent concurrent edits. |
||
400 | */ |
||
401 | public function lock(): void |
||
408 | } |
||
409 | } |
||
410 |