Total Complexity | 80 |
Total Lines | 581 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like RegistrationContext 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 RegistrationContext, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
56 | class RegistrationContext { |
||
57 | |||
58 | /** @var ServiceRegistration<ICapability>[] */ |
||
59 | private $capabilities = []; |
||
60 | |||
61 | /** @var ServiceRegistration<IReporter>[] */ |
||
62 | private $crashReporters = []; |
||
63 | |||
64 | /** @var ServiceRegistration<IWidget>[] */ |
||
65 | private $dashboardPanels = []; |
||
66 | |||
67 | /** @var ServiceRegistration<ILinkAction>[] */ |
||
68 | private $profileLinkActions = []; |
||
69 | |||
70 | /** @var null|ServiceRegistration<ITalkBackend> */ |
||
71 | private $talkBackendRegistration = null; |
||
72 | |||
73 | /** @var ServiceFactoryRegistration[] */ |
||
74 | private $services = []; |
||
75 | |||
76 | /** @var ServiceAliasRegistration[] */ |
||
77 | private $aliases = []; |
||
78 | |||
79 | /** @var ParameterRegistration[] */ |
||
80 | private $parameters = []; |
||
81 | |||
82 | /** @var EventListenerRegistration[] */ |
||
83 | private $eventListeners = []; |
||
84 | |||
85 | /** @var ServiceRegistration<Middleware>[] */ |
||
86 | private $middlewares = []; |
||
87 | |||
88 | /** @var ServiceRegistration<IProvider>[] */ |
||
89 | private $searchProviders = []; |
||
90 | |||
91 | /** @var ServiceRegistration<IAlternativeLogin>[] */ |
||
92 | private $alternativeLogins = []; |
||
93 | |||
94 | /** @var ServiceRegistration<InitialStateProvider>[] */ |
||
95 | private $initialStates = []; |
||
96 | |||
97 | /** @var ServiceRegistration<IHandler>[] */ |
||
98 | private $wellKnownHandlers = []; |
||
99 | |||
100 | /** @var ServiceRegistration<ICustomTemplateProvider>[] */ |
||
101 | private $templateProviders = []; |
||
102 | |||
103 | /** @var ServiceRegistration<INotifier>[] */ |
||
104 | private $notifierServices = []; |
||
105 | |||
106 | /** @var ServiceRegistration<\OCP\Authentication\TwoFactorAuth\IProvider>[] */ |
||
107 | private $twoFactorProviders = []; |
||
108 | |||
109 | /** @var ServiceRegistration<ICalendarProvider>[] */ |
||
110 | private $calendarProviders = []; |
||
111 | |||
112 | /** @var LoggerInterface */ |
||
113 | private $logger; |
||
114 | |||
115 | /** @var PreviewProviderRegistration[] */ |
||
116 | private $previewProviders = []; |
||
117 | |||
118 | public function __construct(LoggerInterface $logger) { |
||
119 | $this->logger = $logger; |
||
120 | } |
||
121 | |||
122 | public function for(string $appId): IRegistrationContext { |
||
123 | return new class($appId, $this) implements IRegistrationContext { |
||
124 | /** @var string */ |
||
125 | private $appId; |
||
126 | |||
127 | /** @var RegistrationContext */ |
||
128 | private $context; |
||
129 | |||
130 | public function __construct(string $appId, RegistrationContext $context) { |
||
131 | $this->appId = $appId; |
||
132 | $this->context = $context; |
||
133 | } |
||
134 | |||
135 | public function registerCapability(string $capability): void { |
||
136 | $this->context->registerCapability( |
||
137 | $this->appId, |
||
138 | $capability |
||
139 | ); |
||
140 | } |
||
141 | |||
142 | public function registerCrashReporter(string $reporterClass): void { |
||
143 | $this->context->registerCrashReporter( |
||
144 | $this->appId, |
||
145 | $reporterClass |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | public function registerDashboardWidget(string $widgetClass): void { |
||
150 | $this->context->registerDashboardPanel( |
||
151 | $this->appId, |
||
152 | $widgetClass |
||
153 | ); |
||
154 | } |
||
155 | |||
156 | public function registerService(string $name, callable $factory, bool $shared = true): void { |
||
157 | $this->context->registerService( |
||
158 | $this->appId, |
||
159 | $name, |
||
160 | $factory, |
||
161 | $shared |
||
162 | ); |
||
163 | } |
||
164 | |||
165 | public function registerServiceAlias(string $alias, string $target): void { |
||
166 | $this->context->registerServiceAlias( |
||
167 | $this->appId, |
||
168 | $alias, |
||
169 | $target |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | public function registerParameter(string $name, $value): void { |
||
174 | $this->context->registerParameter( |
||
175 | $this->appId, |
||
176 | $name, |
||
177 | $value |
||
178 | ); |
||
179 | } |
||
180 | |||
181 | public function registerEventListener(string $event, string $listener, int $priority = 0): void { |
||
182 | $this->context->registerEventListener( |
||
183 | $this->appId, |
||
184 | $event, |
||
185 | $listener, |
||
186 | $priority |
||
187 | ); |
||
188 | } |
||
189 | |||
190 | public function registerMiddleware(string $class): void { |
||
191 | $this->context->registerMiddleware( |
||
192 | $this->appId, |
||
193 | $class |
||
194 | ); |
||
195 | } |
||
196 | |||
197 | public function registerSearchProvider(string $class): void { |
||
198 | $this->context->registerSearchProvider( |
||
199 | $this->appId, |
||
200 | $class |
||
201 | ); |
||
202 | } |
||
203 | |||
204 | public function registerAlternativeLogin(string $class): void { |
||
205 | $this->context->registerAlternativeLogin( |
||
206 | $this->appId, |
||
207 | $class |
||
208 | ); |
||
209 | } |
||
210 | |||
211 | public function registerInitialStateProvider(string $class): void { |
||
212 | $this->context->registerInitialState( |
||
213 | $this->appId, |
||
214 | $class |
||
215 | ); |
||
216 | } |
||
217 | |||
218 | public function registerWellKnownHandler(string $class): void { |
||
219 | $this->context->registerWellKnown( |
||
220 | $this->appId, |
||
221 | $class |
||
222 | ); |
||
223 | } |
||
224 | |||
225 | public function registerTemplateProvider(string $providerClass): void { |
||
226 | $this->context->registerTemplateProvider( |
||
227 | $this->appId, |
||
228 | $providerClass |
||
229 | ); |
||
230 | } |
||
231 | |||
232 | public function registerNotifierService(string $notifierClass): void { |
||
233 | $this->context->registerNotifierService( |
||
234 | $this->appId, |
||
235 | $notifierClass |
||
236 | ); |
||
237 | } |
||
238 | |||
239 | public function registerTwoFactorProvider(string $twoFactorProviderClass): void { |
||
240 | $this->context->registerTwoFactorProvider( |
||
241 | $this->appId, |
||
242 | $twoFactorProviderClass |
||
243 | ); |
||
244 | } |
||
245 | |||
246 | public function registerPreviewProvider(string $previewProviderClass, string $mimeTypeRegex): void { |
||
247 | $this->context->registerPreviewProvider( |
||
248 | $this->appId, |
||
249 | $previewProviderClass, |
||
250 | $mimeTypeRegex |
||
251 | ); |
||
252 | } |
||
253 | |||
254 | public function registerCalendarProvider(string $class): void { |
||
255 | $this->context->registerCalendarProvider( |
||
256 | $this->appId, |
||
257 | $class |
||
258 | ); |
||
259 | } |
||
260 | |||
261 | public function registerProfileLinkAction(string $actionClass): void { |
||
262 | $this->context->registerProfileLinkAction( |
||
263 | $this->appId, |
||
264 | $actionClass |
||
265 | ); |
||
266 | } |
||
267 | |||
268 | public function registerTalkBackend(string $backend): void { |
||
269 | $this->context->registerTalkBackend( |
||
270 | $this->appId, |
||
271 | $backend |
||
272 | ); |
||
273 | } |
||
274 | }; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * @psalm-param class-string<ICapability> $capability |
||
279 | */ |
||
280 | public function registerCapability(string $appId, string $capability): void { |
||
281 | $this->capabilities[] = new ServiceRegistration($appId, $capability); |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * @psalm-param class-string<IReporter> $capability |
||
286 | */ |
||
287 | public function registerCrashReporter(string $appId, string $reporterClass): void { |
||
288 | $this->crashReporters[] = new ServiceRegistration($appId, $reporterClass); |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * @psalm-param class-string<IWidget> $capability |
||
293 | */ |
||
294 | public function registerDashboardPanel(string $appId, string $panelClass): void { |
||
295 | $this->dashboardPanels[] = new ServiceRegistration($appId, $panelClass); |
||
296 | } |
||
297 | |||
298 | public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void { |
||
299 | $this->services[] = new ServiceFactoryRegistration($appId, $name, $factory, $shared); |
||
300 | } |
||
301 | |||
302 | public function registerServiceAlias(string $appId, string $alias, string $target): void { |
||
303 | $this->aliases[] = new ServiceAliasRegistration($appId, $alias, $target); |
||
304 | } |
||
305 | |||
306 | public function registerParameter(string $appId, string $name, $value): void { |
||
307 | $this->parameters[] = new ParameterRegistration($appId, $name, $value); |
||
308 | } |
||
309 | |||
310 | public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void { |
||
311 | $this->eventListeners[] = new EventListenerRegistration($appId, $event, $listener, $priority); |
||
312 | } |
||
313 | |||
314 | /** |
||
315 | * @psalm-param class-string<Middleware> $class |
||
316 | */ |
||
317 | public function registerMiddleware(string $appId, string $class): void { |
||
318 | $this->middlewares[] = new ServiceRegistration($appId, $class); |
||
319 | } |
||
320 | |||
321 | public function registerSearchProvider(string $appId, string $class) { |
||
322 | $this->searchProviders[] = new ServiceRegistration($appId, $class); |
||
323 | } |
||
324 | |||
325 | public function registerAlternativeLogin(string $appId, string $class): void { |
||
326 | $this->alternativeLogins[] = new ServiceRegistration($appId, $class); |
||
327 | } |
||
328 | |||
329 | public function registerInitialState(string $appId, string $class): void { |
||
330 | $this->initialStates[] = new ServiceRegistration($appId, $class); |
||
331 | } |
||
332 | |||
333 | public function registerWellKnown(string $appId, string $class): void { |
||
334 | $this->wellKnownHandlers[] = new ServiceRegistration($appId, $class); |
||
335 | } |
||
336 | |||
337 | public function registerTemplateProvider(string $appId, string $class): void { |
||
338 | $this->templateProviders[] = new ServiceRegistration($appId, $class); |
||
339 | } |
||
340 | |||
341 | public function registerNotifierService(string $appId, string $class): void { |
||
342 | $this->notifierServices[] = new ServiceRegistration($appId, $class); |
||
343 | } |
||
344 | |||
345 | public function registerTwoFactorProvider(string $appId, string $class): void { |
||
346 | $this->twoFactorProviders[] = new ServiceRegistration($appId, $class); |
||
347 | } |
||
348 | |||
349 | public function registerPreviewProvider(string $appId, string $class, string $mimeTypeRegex): void { |
||
350 | $this->previewProviders[] = new PreviewProviderRegistration($appId, $class, $mimeTypeRegex); |
||
351 | } |
||
352 | |||
353 | public function registerCalendarProvider(string $appId, string $class): void { |
||
354 | $this->calendarProviders[] = new ServiceRegistration($appId, $class); |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @psalm-param class-string<ILinkAction> $actionClass |
||
359 | */ |
||
360 | public function registerProfileLinkAction(string $appId, string $actionClass): void { |
||
361 | $this->profileLinkActions[] = new ServiceRegistration($appId, $actionClass); |
||
362 | } |
||
363 | |||
364 | /** |
||
365 | * @psalm-param class-string<ITalkBackend> $backend |
||
366 | */ |
||
367 | public function registerTalkBackend(string $appId, string $backend) { |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param App[] $apps |
||
381 | */ |
||
382 | public function delegateCapabilityRegistrations(array $apps): void { |
||
383 | while (($registration = array_shift($this->capabilities)) !== null) { |
||
|
|||
384 | $appId = $registration->getAppId(); |
||
385 | if (!isset($apps[$appId])) { |
||
386 | // If we land here something really isn't right. But at least we caught the |
||
387 | // notice that is otherwise emitted for the undefined index |
||
388 | $this->logger->error("App $appId not loaded for the capability registration"); |
||
389 | |||
390 | continue; |
||
391 | } |
||
392 | |||
393 | try { |
||
394 | $apps[$appId] |
||
395 | ->getContainer() |
||
396 | ->registerCapability($registration->getService()); |
||
397 | } catch (Throwable $e) { |
||
398 | $this->logger->error("Error during capability registration of $appId: " . $e->getMessage(), [ |
||
399 | 'exception' => $e, |
||
400 | ]); |
||
401 | } |
||
402 | } |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param App[] $apps |
||
407 | */ |
||
408 | public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void { |
||
409 | while (($registration = array_shift($this->crashReporters)) !== null) { |
||
410 | try { |
||
411 | $registry->registerLazy($registration->getService()); |
||
412 | } catch (Throwable $e) { |
||
413 | $appId = $registration->getAppId(); |
||
414 | $this->logger->error("Error during crash reporter registration of $appId: " . $e->getMessage(), [ |
||
415 | 'exception' => $e, |
||
416 | ]); |
||
417 | } |
||
418 | } |
||
419 | } |
||
420 | |||
421 | /** |
||
422 | * @param App[] $apps |
||
423 | */ |
||
424 | public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void { |
||
425 | while (($panel = array_shift($this->dashboardPanels)) !== null) { |
||
426 | try { |
||
427 | $dashboardManager->lazyRegisterWidget($panel->getService()); |
||
428 | } catch (Throwable $e) { |
||
429 | $appId = $panel->getAppId(); |
||
430 | $this->logger->error("Error during dashboard registration of $appId: " . $e->getMessage(), [ |
||
431 | 'exception' => $e, |
||
432 | ]); |
||
433 | } |
||
434 | } |
||
435 | } |
||
436 | |||
437 | public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void { |
||
438 | while (($registration = array_shift($this->eventListeners)) !== null) { |
||
439 | try { |
||
440 | $eventDispatcher->addServiceListener( |
||
441 | $registration->getEvent(), |
||
442 | $registration->getService(), |
||
443 | $registration->getPriority() |
||
444 | ); |
||
445 | } catch (Throwable $e) { |
||
446 | $appId = $registration->getAppId(); |
||
447 | $this->logger->error("Error during event listener registration of $appId: " . $e->getMessage(), [ |
||
448 | 'exception' => $e, |
||
449 | ]); |
||
450 | } |
||
451 | } |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * @param App[] $apps |
||
456 | */ |
||
457 | public function delegateContainerRegistrations(array $apps): void { |
||
458 | while (($registration = array_shift($this->services)) !== null) { |
||
459 | $appId = $registration->getAppId(); |
||
460 | if (!isset($apps[$appId])) { |
||
461 | // If we land here something really isn't right. But at least we caught the |
||
462 | // notice that is otherwise emitted for the undefined index |
||
463 | $this->logger->error("App $appId not loaded for the container service registration"); |
||
464 | |||
465 | continue; |
||
466 | } |
||
467 | |||
468 | try { |
||
469 | /** |
||
470 | * Register the service and convert the callable into a \Closure if necessary |
||
471 | */ |
||
472 | $apps[$appId] |
||
473 | ->getContainer() |
||
474 | ->registerService( |
||
475 | $registration->getName(), |
||
476 | Closure::fromCallable($registration->getFactory()), |
||
477 | $registration->isShared() |
||
478 | ); |
||
479 | } catch (Throwable $e) { |
||
480 | $this->logger->error("Error during service registration of $appId: " . $e->getMessage(), [ |
||
481 | 'exception' => $e, |
||
482 | ]); |
||
483 | } |
||
484 | } |
||
485 | |||
486 | while (($registration = array_shift($this->aliases)) !== null) { |
||
487 | $appId = $registration->getAppId(); |
||
488 | if (!isset($apps[$appId])) { |
||
489 | // If we land here something really isn't right. But at least we caught the |
||
490 | // notice that is otherwise emitted for the undefined index |
||
491 | $this->logger->error("App $appId not loaded for the container alias registration"); |
||
492 | |||
493 | continue; |
||
494 | } |
||
495 | |||
496 | try { |
||
497 | $apps[$appId] |
||
498 | ->getContainer() |
||
499 | ->registerAlias( |
||
500 | $registration->getAlias(), |
||
501 | $registration->getTarget() |
||
502 | ); |
||
503 | } catch (Throwable $e) { |
||
504 | $this->logger->error("Error during service alias registration of $appId: " . $e->getMessage(), [ |
||
505 | 'exception' => $e, |
||
506 | ]); |
||
507 | } |
||
508 | } |
||
509 | |||
510 | while (($registration = array_shift($this->parameters)) !== null) { |
||
511 | $appId = $registration->getAppId(); |
||
512 | if (!isset($apps[$appId])) { |
||
513 | // If we land here something really isn't right. But at least we caught the |
||
514 | // notice that is otherwise emitted for the undefined index |
||
515 | $this->logger->error("App $appId not loaded for the container parameter registration"); |
||
516 | |||
517 | continue; |
||
518 | } |
||
519 | |||
520 | try { |
||
521 | $apps[$appId] |
||
522 | ->getContainer() |
||
523 | ->registerParameter( |
||
524 | $registration->getName(), |
||
525 | $registration->getValue() |
||
526 | ); |
||
527 | } catch (Throwable $e) { |
||
528 | $this->logger->error("Error during service parameter registration of $appId: " . $e->getMessage(), [ |
||
529 | 'exception' => $e, |
||
530 | ]); |
||
531 | } |
||
532 | } |
||
533 | } |
||
534 | |||
535 | /** |
||
536 | * @param App[] $apps |
||
537 | */ |
||
538 | public function delegateMiddlewareRegistrations(array $apps): void { |
||
539 | while (($middleware = array_shift($this->middlewares)) !== null) { |
||
540 | $appId = $middleware->getAppId(); |
||
541 | if (!isset($apps[$appId])) { |
||
542 | // If we land here something really isn't right. But at least we caught the |
||
543 | // notice that is otherwise emitted for the undefined index |
||
544 | $this->logger->error("App $appId not loaded for the container middleware registration"); |
||
545 | |||
546 | continue; |
||
547 | } |
||
548 | |||
549 | try { |
||
550 | $apps[$appId] |
||
551 | ->getContainer() |
||
552 | ->registerMiddleWare($middleware->getService()); |
||
553 | } catch (Throwable $e) { |
||
554 | $this->logger->error("Error during capability registration of $appId: " . $e->getMessage(), [ |
||
555 | 'exception' => $e, |
||
556 | ]); |
||
557 | } |
||
558 | } |
||
559 | } |
||
560 | |||
561 | /** |
||
562 | * @return ServiceRegistration<IProvider>[] |
||
563 | */ |
||
564 | public function getSearchProviders(): array { |
||
565 | return $this->searchProviders; |
||
566 | } |
||
567 | |||
568 | /** |
||
569 | * @return ServiceRegistration<IAlternativeLogin>[] |
||
570 | */ |
||
571 | public function getAlternativeLogins(): array { |
||
572 | return $this->alternativeLogins; |
||
573 | } |
||
574 | |||
575 | /** |
||
576 | * @return ServiceRegistration<InitialStateProvider>[] |
||
577 | */ |
||
578 | public function getInitialStates(): array { |
||
579 | return $this->initialStates; |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * @return ServiceRegistration<IHandler>[] |
||
584 | */ |
||
585 | public function getWellKnownHandlers(): array { |
||
587 | } |
||
588 | |||
589 | /** |
||
590 | * @return ServiceRegistration<ICustomTemplateProvider>[] |
||
591 | */ |
||
592 | public function getTemplateProviders(): array { |
||
593 | return $this->templateProviders; |
||
594 | } |
||
595 | |||
596 | /** |
||
597 | * @return ServiceRegistration<INotifier>[] |
||
598 | */ |
||
599 | public function getNotifierServices(): array { |
||
600 | return $this->notifierServices; |
||
601 | } |
||
602 | |||
603 | /** |
||
604 | * @return ServiceRegistration<\OCP\Authentication\TwoFactorAuth\IProvider>[] |
||
605 | */ |
||
606 | public function getTwoFactorProviders(): array { |
||
607 | return $this->twoFactorProviders; |
||
608 | } |
||
609 | |||
610 | /** |
||
611 | * @return PreviewProviderRegistration[] |
||
612 | */ |
||
613 | public function getPreviewProviders(): array { |
||
614 | return $this->previewProviders; |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * @return ServiceRegistration<ICalendarProvider>[] |
||
619 | */ |
||
620 | public function getCalendarProviders(): array { |
||
621 | return $this->calendarProviders; |
||
622 | } |
||
623 | |||
624 | /** |
||
625 | * @return ServiceRegistration<ILinkAction>[] |
||
626 | */ |
||
627 | public function getProfileLinkActions(): array { |
||
628 | return $this->profileLinkActions; |
||
629 | } |
||
630 | |||
631 | /** |
||
632 | * @return ServiceRegistration|null |
||
633 | * @psalm-return ServiceRegistration<ITalkBackend>|null |
||
634 | */ |
||
635 | public function getTalkBackendRegistration(): ?ServiceRegistration { |
||
637 | } |
||
638 | } |
||
639 |