Total Complexity | 54 |
Total Lines | 486 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Registry 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 Registry, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class Registry |
||
54 | { |
||
55 | private static CacheFactoryInterface $cache_factory; |
||
56 | |||
57 | private static CalendarDateFactoryInterface $calendar_date_factory; |
||
58 | |||
59 | private static ElementFactoryInterface $element_factory; |
||
60 | |||
61 | private static EncodingFactoryInterface $encoding_factory; |
||
62 | |||
63 | private static FamilyFactoryInterface $family_factory; |
||
64 | |||
65 | private static FilesystemFactoryInterface $filesystem_factory; |
||
66 | |||
67 | private static GedcomRecordFactoryInterface $gedcom_record_factory; |
||
68 | |||
69 | private static HeaderFactoryInterface $header_factory; |
||
70 | |||
71 | private static IdFactoryInterface $id_factory; |
||
72 | |||
73 | private static ImageFactoryInterface $image_factory; |
||
74 | |||
75 | private static IndividualFactoryInterface $individual_factory; |
||
76 | |||
77 | private static LocationFactoryInterface $location_factory; |
||
78 | |||
79 | private static MarkdownFactoryInterface $markdown_factory; |
||
80 | |||
81 | private static MediaFactoryInterface $media_factory; |
||
82 | |||
83 | private static NoteFactoryInterface $note_factory; |
||
84 | |||
85 | private static RepositoryFactoryInterface $repository_factory; |
||
86 | |||
87 | private static ResponseFactoryInterface $response_factory; |
||
88 | |||
89 | private static RouteFactoryInterface $route_factory; |
||
90 | |||
91 | private static SharedNoteFactoryInterface $shared_note_factory; |
||
92 | |||
93 | private static SlugFactoryInterface $slug_factory; |
||
94 | |||
95 | private static SourceFactoryInterface $source_factory; |
||
96 | |||
97 | private static SubmissionFactoryInterface $submission_factory; |
||
98 | |||
99 | private static SubmitterFactoryInterface $submitter_factory; |
||
100 | |||
101 | private static SurnameTraditionFactoryInterface $surname_tradition_factory; |
||
102 | |||
103 | private static TimeFactoryInterface $time_factory; |
||
104 | |||
105 | private static TimestampFactoryInterface $timestamp_factory; |
||
106 | |||
107 | private static XrefFactoryInterface $xref_factory; |
||
108 | |||
109 | /** |
||
110 | * Store or retrieve a factory object. |
||
111 | * |
||
112 | * @param CacheFactoryInterface|null $factory |
||
113 | * |
||
114 | * @return CacheFactoryInterface |
||
115 | */ |
||
116 | public static function cache(CacheFactoryInterface $factory = null): CacheFactoryInterface |
||
117 | { |
||
118 | if ($factory instanceof CacheFactoryInterface) { |
||
119 | self::$cache_factory = $factory; |
||
120 | } |
||
121 | |||
122 | return self::$cache_factory; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Store or retrieve a factory object. |
||
127 | * |
||
128 | * @param CalendarDateFactoryInterface|null $factory |
||
129 | * |
||
130 | * @return CalendarDateFactoryInterface |
||
131 | */ |
||
132 | public static function calendarDateFactory(CalendarDateFactoryInterface $factory = null): CalendarDateFactoryInterface |
||
133 | { |
||
134 | if ($factory instanceof CalendarDateFactoryInterface) { |
||
135 | self::$calendar_date_factory = $factory; |
||
136 | } |
||
137 | |||
138 | return self::$calendar_date_factory; |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Store or retrieve a factory object. |
||
143 | * |
||
144 | * @param ElementFactoryInterface|null $factory |
||
145 | * |
||
146 | * @return ElementFactoryInterface |
||
147 | */ |
||
148 | public static function elementFactory(ElementFactoryInterface $factory = null): ElementFactoryInterface |
||
149 | { |
||
150 | if ($factory instanceof ElementFactoryInterface) { |
||
151 | self::$element_factory = $factory; |
||
152 | } |
||
153 | |||
154 | return self::$element_factory; |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Store or retrieve a factory object. |
||
159 | * |
||
160 | * @param EncodingFactoryInterface|null $factory |
||
161 | * |
||
162 | * @return EncodingFactoryInterface |
||
163 | */ |
||
164 | public static function encodingFactory(EncodingFactoryInterface $factory = null): EncodingFactoryInterface |
||
165 | { |
||
166 | if ($factory instanceof EncodingFactoryInterface) { |
||
167 | self::$encoding_factory = $factory; |
||
168 | } |
||
169 | |||
170 | return self::$encoding_factory; |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * Store or retrieve a factory object. |
||
175 | * |
||
176 | * @param FamilyFactoryInterface|null $factory |
||
177 | * |
||
178 | * @return FamilyFactoryInterface |
||
179 | */ |
||
180 | public static function familyFactory(FamilyFactoryInterface $factory = null): FamilyFactoryInterface |
||
181 | { |
||
182 | if ($factory instanceof FamilyFactoryInterface) { |
||
183 | self::$family_factory = $factory; |
||
184 | } |
||
185 | |||
186 | return self::$family_factory; |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * Store or retrieve a factory object. |
||
191 | * |
||
192 | * @param FilesystemFactoryInterface|null $factory |
||
193 | * |
||
194 | * @return FilesystemFactoryInterface |
||
195 | */ |
||
196 | public static function filesystem(FilesystemFactoryInterface $factory = null): FilesystemFactoryInterface |
||
197 | { |
||
198 | if ($factory instanceof FilesystemFactoryInterface) { |
||
199 | self::$filesystem_factory = $factory; |
||
200 | } |
||
201 | |||
202 | return self::$filesystem_factory; |
||
203 | } |
||
204 | |||
205 | /** |
||
206 | * Store or retrieve a factory object. |
||
207 | * |
||
208 | * @param GedcomRecordFactoryInterface|null $factory |
||
209 | * |
||
210 | * @return GedcomRecordFactoryInterface |
||
211 | */ |
||
212 | public static function gedcomRecordFactory(GedcomRecordFactoryInterface $factory = null): GedcomRecordFactoryInterface |
||
213 | { |
||
214 | if ($factory instanceof GedcomRecordFactoryInterface) { |
||
215 | self::$gedcom_record_factory = $factory; |
||
216 | } |
||
217 | |||
218 | return self::$gedcom_record_factory; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Store or retrieve a factory object. |
||
223 | * |
||
224 | * @param HeaderFactoryInterface|null $factory |
||
225 | * |
||
226 | * @return HeaderFactoryInterface |
||
227 | */ |
||
228 | public static function headerFactory(HeaderFactoryInterface $factory = null): HeaderFactoryInterface |
||
229 | { |
||
230 | if ($factory instanceof HeaderFactoryInterface) { |
||
231 | self::$header_factory = $factory; |
||
232 | } |
||
233 | |||
234 | return self::$header_factory; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Store or retrieve a factory object. |
||
239 | * |
||
240 | * @param IdFactoryInterface|null $factory |
||
241 | * |
||
242 | * @return IdFactoryInterface |
||
243 | */ |
||
244 | public static function idFactory(IdFactoryInterface $factory = null): IdFactoryInterface |
||
245 | { |
||
246 | if ($factory instanceof IdFactoryInterface) { |
||
247 | self::$id_factory = $factory; |
||
248 | } |
||
249 | |||
250 | return self::$id_factory; |
||
251 | } |
||
252 | |||
253 | /** |
||
254 | * Store or retrieve a factory object. |
||
255 | * |
||
256 | * @param ImageFactoryInterface|null $factory |
||
257 | * |
||
258 | * @return ImageFactoryInterface |
||
259 | */ |
||
260 | public static function imageFactory(ImageFactoryInterface $factory = null): ImageFactoryInterface |
||
261 | { |
||
262 | if ($factory instanceof ImageFactoryInterface) { |
||
263 | self::$image_factory = $factory; |
||
264 | } |
||
265 | |||
266 | return self::$image_factory; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * Store or retrieve a factory object. |
||
271 | * |
||
272 | * @param IndividualFactoryInterface|null $factory |
||
273 | * |
||
274 | * @return IndividualFactoryInterface |
||
275 | */ |
||
276 | public static function individualFactory(IndividualFactoryInterface $factory = null): IndividualFactoryInterface |
||
277 | { |
||
278 | if ($factory instanceof IndividualFactoryInterface) { |
||
279 | self::$individual_factory = $factory; |
||
280 | } |
||
281 | |||
282 | return self::$individual_factory; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * Store or retrieve a factory object. |
||
287 | * |
||
288 | * @param LocationFactoryInterface|null $factory |
||
289 | * |
||
290 | * @return LocationFactoryInterface |
||
291 | */ |
||
292 | public static function locationFactory(LocationFactoryInterface $factory = null): LocationFactoryInterface |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * Store or retrieve a factory object. |
||
303 | * |
||
304 | * @param MarkdownFactoryInterface|null $factory |
||
305 | * |
||
306 | * @return MarkdownFactoryInterface |
||
307 | */ |
||
308 | public static function markdownFactory(MarkdownFactoryInterface $factory = null): MarkdownFactoryInterface |
||
309 | { |
||
310 | if ($factory instanceof MarkdownFactoryInterface) { |
||
311 | self::$markdown_factory = $factory; |
||
312 | } |
||
313 | |||
314 | return self::$markdown_factory; |
||
315 | } |
||
316 | |||
317 | /** |
||
318 | * Store or retrieve a factory object. |
||
319 | * |
||
320 | * @param MediaFactoryInterface|null $factory |
||
321 | * |
||
322 | * @return MediaFactoryInterface |
||
323 | */ |
||
324 | public static function mediaFactory(MediaFactoryInterface $factory = null): MediaFactoryInterface |
||
325 | { |
||
326 | if ($factory instanceof MediaFactoryInterface) { |
||
327 | self::$media_factory = $factory; |
||
328 | } |
||
329 | |||
330 | return self::$media_factory; |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Store or retrieve a factory object. |
||
335 | * |
||
336 | * @param NoteFactoryInterface|null $factory |
||
337 | * |
||
338 | * @return NoteFactoryInterface |
||
339 | */ |
||
340 | public static function noteFactory(NoteFactoryInterface $factory = null): NoteFactoryInterface |
||
341 | { |
||
342 | if ($factory instanceof NoteFactoryInterface) { |
||
343 | self::$note_factory = $factory; |
||
344 | } |
||
345 | |||
346 | return self::$note_factory; |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Store or retrieve a factory object. |
||
351 | * |
||
352 | * @param RepositoryFactoryInterface|null $factory |
||
353 | * |
||
354 | * @return RepositoryFactoryInterface |
||
355 | */ |
||
356 | public static function repositoryFactory(RepositoryFactoryInterface $factory = null): RepositoryFactoryInterface |
||
357 | { |
||
358 | if ($factory instanceof RepositoryFactoryInterface) { |
||
359 | self::$repository_factory = $factory; |
||
360 | } |
||
361 | |||
362 | return self::$repository_factory; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Store or retrieve a factory object. |
||
367 | * |
||
368 | * @param ResponseFactoryInterface|null $factory |
||
369 | * |
||
370 | * @return ResponseFactoryInterface |
||
371 | */ |
||
372 | public static function responseFactory(ResponseFactoryInterface $factory = null): ResponseFactoryInterface |
||
373 | { |
||
374 | if ($factory instanceof ResponseFactoryInterface) { |
||
375 | self::$response_factory = $factory; |
||
376 | } |
||
377 | |||
378 | return self::$response_factory; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Store or retrieve a factory object. |
||
383 | * |
||
384 | * @param RouteFactoryInterface|null $factory |
||
385 | * |
||
386 | * @return RouteFactoryInterface |
||
387 | */ |
||
388 | public static function routeFactory(RouteFactoryInterface $factory = null): RouteFactoryInterface |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * Store or retrieve a factory object. |
||
399 | * |
||
400 | * @param SharedNoteFactoryInterface|null $factory |
||
401 | * |
||
402 | * @return SharedNoteFactoryInterface |
||
403 | */ |
||
404 | public static function sharedNoteFactory(SharedNoteFactoryInterface $factory = null): SharedNoteFactoryInterface |
||
405 | { |
||
406 | if ($factory instanceof SharedNoteFactoryInterface) { |
||
407 | self::$shared_note_factory = $factory; |
||
408 | } |
||
409 | |||
410 | return self::$shared_note_factory; |
||
411 | } |
||
412 | |||
413 | /** |
||
414 | * Store or retrieve a factory object. |
||
415 | * |
||
416 | * @param SlugFactoryInterface|null $factory |
||
417 | * |
||
418 | * @return SlugFactoryInterface |
||
419 | */ |
||
420 | public static function slugFactory(SlugFactoryInterface $factory = null): SlugFactoryInterface |
||
421 | { |
||
422 | if ($factory instanceof SlugFactoryInterface) { |
||
423 | self::$slug_factory = $factory; |
||
424 | } |
||
425 | |||
426 | return self::$slug_factory; |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Store or retrieve a factory object. |
||
431 | * |
||
432 | * @param SourceFactoryInterface|null $factory |
||
433 | * |
||
434 | * @return SourceFactoryInterface |
||
435 | */ |
||
436 | public static function sourceFactory(SourceFactoryInterface $factory = null): SourceFactoryInterface |
||
437 | { |
||
438 | if ($factory instanceof SourceFactoryInterface) { |
||
439 | self::$source_factory = $factory; |
||
440 | } |
||
441 | |||
442 | return self::$source_factory; |
||
443 | } |
||
444 | |||
445 | /** |
||
446 | * Store or retrieve a factory object. |
||
447 | * |
||
448 | * @param SubmissionFactoryInterface|null $factory |
||
449 | * |
||
450 | * @return SubmissionFactoryInterface |
||
451 | */ |
||
452 | public static function submissionFactory(SubmissionFactoryInterface $factory = null): SubmissionFactoryInterface |
||
453 | { |
||
454 | if ($factory instanceof SubmissionFactoryInterface) { |
||
455 | self::$submission_factory = $factory; |
||
456 | } |
||
457 | |||
458 | return self::$submission_factory; |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Store or retrieve a factory object. |
||
463 | * |
||
464 | * @param SubmitterFactoryInterface|null $factory |
||
465 | * |
||
466 | * @return SubmitterFactoryInterface |
||
467 | */ |
||
468 | public static function submitterFactory(SubmitterFactoryInterface $factory = null): SubmitterFactoryInterface |
||
469 | { |
||
470 | if ($factory instanceof SubmitterFactoryInterface) { |
||
471 | self::$submitter_factory = $factory; |
||
472 | } |
||
473 | |||
474 | return self::$submitter_factory; |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * Store or retrieve a factory object. |
||
479 | * |
||
480 | * @param SurnameTraditionFactoryInterface|null $factory |
||
481 | * |
||
482 | * @return SurnameTraditionFactoryInterface |
||
483 | */ |
||
484 | public static function surnameTraditionFactory(SurnameTraditionFactoryInterface $factory = null): SurnameTraditionFactoryInterface |
||
491 | } |
||
492 | |||
493 | /** |
||
494 | * Store or retrieve a factory object. |
||
495 | * |
||
496 | * @param TimeFactoryInterface|null $factory |
||
497 | * |
||
498 | * @return TimeFactoryInterface |
||
499 | */ |
||
500 | public static function timeFactory(TimeFactoryInterface $factory = null): TimeFactoryInterface |
||
501 | { |
||
502 | if ($factory instanceof TimeFactoryInterface) { |
||
503 | self::$time_factory = $factory; |
||
504 | } |
||
505 | |||
506 | return self::$time_factory; |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Store or retrieve a factory object. |
||
511 | * |
||
512 | * @param TimestampFactoryInterface|null $factory |
||
513 | * |
||
514 | * @return TimestampFactoryInterface |
||
515 | */ |
||
516 | public static function timestampFactory(TimestampFactoryInterface $factory = null): TimestampFactoryInterface |
||
523 | } |
||
524 | |||
525 | /** |
||
526 | * Store or retrieve a factory object. |
||
527 | * |
||
528 | * @param XrefFactoryInterface|null $factory |
||
529 | * |
||
530 | * @return XrefFactoryInterface |
||
531 | */ |
||
532 | public static function xrefFactory(XrefFactoryInterface $factory = null): XrefFactoryInterface |
||
539 | } |
||
540 | } |
||
541 |