Total Complexity | 53 |
Total Lines | 423 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
42 | class RegistrationContext { |
||
43 | |||
44 | /** @var array[] */ |
||
45 | private $capabilities = []; |
||
46 | |||
47 | /** @var array[] */ |
||
48 | private $crashReporters = []; |
||
49 | |||
50 | /** @var array[] */ |
||
51 | private $dashboardPanels = []; |
||
52 | |||
53 | /** @var array[] */ |
||
54 | private $services = []; |
||
55 | |||
56 | /** @var array[] */ |
||
57 | private $aliases = []; |
||
58 | |||
59 | /** @var array[] */ |
||
60 | private $parameters = []; |
||
61 | |||
62 | /** @var array[] */ |
||
63 | private $eventListeners = []; |
||
64 | |||
65 | /** @var array[] */ |
||
66 | private $middlewares = []; |
||
67 | |||
68 | /** @var array[] */ |
||
69 | private $searchProviders = []; |
||
70 | |||
71 | /** @var array[] */ |
||
72 | private $alternativeLogins = []; |
||
73 | |||
74 | /** @var array[] */ |
||
75 | private $initialStates = []; |
||
76 | |||
77 | /** @var array[] */ |
||
78 | private $wellKnownHandlers = []; |
||
79 | |||
80 | /** @var ILogger */ |
||
81 | private $logger; |
||
82 | |||
83 | public function __construct(ILogger $logger) { |
||
84 | $this->logger = $logger; |
||
85 | } |
||
86 | |||
87 | public function for(string $appId): IRegistrationContext { |
||
88 | return new class($appId, $this) implements IRegistrationContext { |
||
89 | /** @var string */ |
||
90 | private $appId; |
||
91 | |||
92 | /** @var RegistrationContext */ |
||
93 | private $context; |
||
94 | |||
95 | public function __construct(string $appId, RegistrationContext $context) { |
||
96 | $this->appId = $appId; |
||
97 | $this->context = $context; |
||
98 | } |
||
99 | |||
100 | public function registerCapability(string $capability): void { |
||
101 | $this->context->registerCapability( |
||
102 | $this->appId, |
||
103 | $capability |
||
104 | ); |
||
105 | } |
||
106 | |||
107 | public function registerCrashReporter(string $reporterClass): void { |
||
108 | $this->context->registerCrashReporter( |
||
109 | $this->appId, |
||
110 | $reporterClass |
||
111 | ); |
||
112 | } |
||
113 | |||
114 | public function registerDashboardWidget(string $widgetClass): void { |
||
115 | $this->context->registerDashboardPanel( |
||
116 | $this->appId, |
||
117 | $widgetClass |
||
118 | ); |
||
119 | } |
||
120 | |||
121 | public function registerService(string $name, callable $factory, bool $shared = true): void { |
||
122 | $this->context->registerService( |
||
123 | $this->appId, |
||
124 | $name, |
||
125 | $factory, |
||
126 | $shared |
||
127 | ); |
||
128 | } |
||
129 | |||
130 | public function registerServiceAlias(string $alias, string $target): void { |
||
131 | $this->context->registerServiceAlias( |
||
132 | $this->appId, |
||
133 | $alias, |
||
134 | $target |
||
135 | ); |
||
136 | } |
||
137 | |||
138 | public function registerParameter(string $name, $value): void { |
||
139 | $this->context->registerParameter( |
||
140 | $this->appId, |
||
141 | $name, |
||
142 | $value |
||
143 | ); |
||
144 | } |
||
145 | |||
146 | public function registerEventListener(string $event, string $listener, int $priority = 0): void { |
||
147 | $this->context->registerEventListener( |
||
148 | $this->appId, |
||
149 | $event, |
||
150 | $listener, |
||
151 | $priority |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | public function registerMiddleware(string $class): void { |
||
156 | $this->context->registerMiddleware( |
||
157 | $this->appId, |
||
158 | $class |
||
159 | ); |
||
160 | } |
||
161 | |||
162 | public function registerSearchProvider(string $class): void { |
||
163 | $this->context->registerSearchProvider( |
||
164 | $this->appId, |
||
165 | $class |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | public function registerAlternativeLogin(string $class): void { |
||
170 | $this->context->registerAlternativeLogin( |
||
171 | $this->appId, |
||
172 | $class |
||
173 | ); |
||
174 | } |
||
175 | |||
176 | public function registerInitialStateProvider(string $class): void { |
||
177 | $this->context->registerInitialState( |
||
178 | $this->appId, |
||
179 | $class |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | public function registerWellKnownHandler(string $class): void { |
||
184 | $this->context->registerWellKnown( |
||
185 | $this->appId, |
||
186 | $class |
||
187 | ); |
||
188 | } |
||
189 | }; |
||
190 | } |
||
191 | |||
192 | public function registerCapability(string $appId, string $capability): void { |
||
193 | $this->capabilities[] = [ |
||
194 | 'appId' => $appId, |
||
195 | 'capability' => $capability |
||
196 | ]; |
||
197 | } |
||
198 | |||
199 | public function registerCrashReporter(string $appId, string $reporterClass): void { |
||
200 | $this->crashReporters[] = [ |
||
201 | 'appId' => $appId, |
||
202 | 'class' => $reporterClass, |
||
203 | ]; |
||
204 | } |
||
205 | |||
206 | public function registerDashboardPanel(string $appId, string $panelClass): void { |
||
207 | $this->dashboardPanels[] = [ |
||
208 | 'appId' => $appId, |
||
209 | 'class' => $panelClass |
||
210 | ]; |
||
211 | } |
||
212 | |||
213 | public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void { |
||
214 | $this->services[] = [ |
||
215 | "appId" => $appId, |
||
216 | "name" => $name, |
||
217 | "factory" => $factory, |
||
218 | "shared" => $shared, |
||
219 | ]; |
||
220 | } |
||
221 | |||
222 | public function registerServiceAlias(string $appId, string $alias, string $target): void { |
||
223 | $this->aliases[] = [ |
||
224 | "appId" => $appId, |
||
225 | "alias" => $alias, |
||
226 | "target" => $target, |
||
227 | ]; |
||
228 | } |
||
229 | |||
230 | public function registerParameter(string $appId, string $name, $value): void { |
||
231 | $this->parameters[] = [ |
||
232 | "appId" => $appId, |
||
233 | "name" => $name, |
||
234 | "value" => $value, |
||
235 | ]; |
||
236 | } |
||
237 | |||
238 | public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void { |
||
239 | $this->eventListeners[] = [ |
||
240 | "appId" => $appId, |
||
241 | "event" => $event, |
||
242 | "listener" => $listener, |
||
243 | "priority" => $priority, |
||
244 | ]; |
||
245 | } |
||
246 | |||
247 | public function registerMiddleware(string $appId, string $class): void { |
||
248 | $this->middlewares[] = [ |
||
249 | "appId" => $appId, |
||
250 | "class" => $class, |
||
251 | ]; |
||
252 | } |
||
253 | |||
254 | public function registerSearchProvider(string $appId, string $class) { |
||
255 | $this->searchProviders[] = [ |
||
256 | 'appId' => $appId, |
||
257 | 'class' => $class, |
||
258 | ]; |
||
259 | } |
||
260 | |||
261 | public function registerAlternativeLogin(string $appId, string $class): void { |
||
262 | $this->alternativeLogins[] = [ |
||
263 | 'appId' => $appId, |
||
264 | 'class' => $class, |
||
265 | ]; |
||
266 | } |
||
267 | |||
268 | public function registerInitialState(string $appId, string $class): void { |
||
269 | $this->initialStates[] = [ |
||
270 | 'appId' => $appId, |
||
271 | 'class' => $class, |
||
272 | ]; |
||
273 | } |
||
274 | |||
275 | public function registerWellKnown(string $appId, string $class): void { |
||
279 | ]; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @param App[] $apps |
||
284 | */ |
||
285 | public function delegateCapabilityRegistrations(array $apps): void { |
||
286 | while (($registration = array_shift($this->capabilities)) !== null) { |
||
287 | try { |
||
288 | $apps[$registration['appId']] |
||
289 | ->getContainer() |
||
290 | ->registerCapability($registration['capability']); |
||
291 | } catch (Throwable $e) { |
||
292 | $appId = $registration['appId']; |
||
293 | $this->logger->logException($e, [ |
||
294 | 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
||
295 | 'level' => ILogger::ERROR, |
||
|
|||
296 | ]); |
||
297 | } |
||
298 | } |
||
299 | } |
||
300 | |||
301 | /** |
||
302 | * @param App[] $apps |
||
303 | */ |
||
304 | public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void { |
||
305 | while (($registration = array_shift($this->crashReporters)) !== null) { |
||
306 | try { |
||
307 | $registry->registerLazy($registration['class']); |
||
308 | } catch (Throwable $e) { |
||
309 | $appId = $registration['appId']; |
||
310 | $this->logger->logException($e, [ |
||
311 | 'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(), |
||
312 | 'level' => ILogger::ERROR, |
||
313 | ]); |
||
314 | } |
||
315 | } |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * @param App[] $apps |
||
320 | */ |
||
321 | public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void { |
||
322 | while (($panel = array_shift($this->dashboardPanels)) !== null) { |
||
323 | try { |
||
324 | $dashboardManager->lazyRegisterWidget($panel['class']); |
||
325 | } catch (Throwable $e) { |
||
326 | $appId = $panel['appId']; |
||
327 | $this->logger->logException($e, [ |
||
328 | 'message' => "Error during dashboard registration of $appId: " . $e->getMessage(), |
||
329 | 'level' => ILogger::ERROR, |
||
330 | ]); |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void { |
||
336 | while (($registration = array_shift($this->eventListeners)) !== null) { |
||
337 | try { |
||
338 | if (isset($registration['priority'])) { |
||
339 | $eventDispatcher->addServiceListener( |
||
340 | $registration['event'], |
||
341 | $registration['listener'], |
||
342 | $registration['priority'] |
||
343 | ); |
||
344 | } else { |
||
345 | $eventDispatcher->addServiceListener( |
||
346 | $registration['event'], |
||
347 | $registration['listener'] |
||
348 | ); |
||
349 | } |
||
350 | } catch (Throwable $e) { |
||
351 | $appId = $registration['appId']; |
||
352 | $this->logger->logException($e, [ |
||
353 | 'message' => "Error during event listener registration of $appId: " . $e->getMessage(), |
||
354 | 'level' => ILogger::ERROR, |
||
355 | ]); |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * @param App[] $apps |
||
362 | */ |
||
363 | public function delegateContainerRegistrations(array $apps): void { |
||
364 | while (($registration = array_shift($this->services)) !== null) { |
||
365 | try { |
||
366 | /** |
||
367 | * Register the service and convert the callable into a \Closure if necessary |
||
368 | */ |
||
369 | $apps[$registration['appId']] |
||
370 | ->getContainer() |
||
371 | ->registerService( |
||
372 | $registration['name'], |
||
373 | Closure::fromCallable($registration['factory']), |
||
374 | $registration['shared'] ?? true |
||
375 | ); |
||
376 | } catch (Throwable $e) { |
||
377 | $appId = $registration['appId']; |
||
378 | $this->logger->logException($e, [ |
||
379 | 'message' => "Error during service registration of $appId: " . $e->getMessage(), |
||
380 | 'level' => ILogger::ERROR, |
||
381 | ]); |
||
382 | } |
||
383 | } |
||
384 | |||
385 | foreach ($this->aliases as $registration) { |
||
386 | try { |
||
387 | $apps[$registration['appId']] |
||
388 | ->getContainer() |
||
389 | ->registerAlias( |
||
390 | $registration['alias'], |
||
391 | $registration['target'] |
||
392 | ); |
||
393 | } catch (Throwable $e) { |
||
394 | $appId = $registration['appId']; |
||
395 | $this->logger->logException($e, [ |
||
396 | 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
||
397 | 'level' => ILogger::ERROR, |
||
398 | ]); |
||
399 | } |
||
400 | } |
||
401 | |||
402 | foreach ($this->parameters as $registration) { |
||
403 | try { |
||
404 | $apps[$registration['appId']] |
||
405 | ->getContainer() |
||
406 | ->registerParameter( |
||
407 | $registration['name'], |
||
408 | $registration['value'] |
||
409 | ); |
||
410 | } catch (Throwable $e) { |
||
411 | $appId = $registration['appId']; |
||
412 | $this->logger->logException($e, [ |
||
413 | 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
||
414 | 'level' => ILogger::ERROR, |
||
415 | ]); |
||
416 | } |
||
417 | } |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @param App[] $apps |
||
422 | */ |
||
423 | public function delegateMiddlewareRegistrations(array $apps): void { |
||
424 | while (($middleware = array_shift($this->middlewares)) !== null) { |
||
425 | try { |
||
426 | $apps[$middleware['appId']] |
||
427 | ->getContainer() |
||
428 | ->registerMiddleWare($middleware['class']); |
||
429 | } catch (Throwable $e) { |
||
430 | $appId = $middleware['appId']; |
||
431 | $this->logger->logException($e, [ |
||
432 | 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
||
433 | 'level' => ILogger::ERROR, |
||
434 | ]); |
||
435 | } |
||
436 | } |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * @return array[] |
||
441 | */ |
||
442 | public function getSearchProviders(): array { |
||
443 | return $this->searchProviders; |
||
444 | } |
||
445 | |||
446 | /** |
||
447 | * @return array[] |
||
448 | */ |
||
449 | public function getAlternativeLogins(): array { |
||
450 | return $this->alternativeLogins; |
||
451 | } |
||
452 | |||
453 | /** |
||
454 | * @erturn array[] |
||
455 | */ |
||
456 | public function getInitialStates(): array { |
||
457 | return $this->initialStates; |
||
458 | } |
||
459 | |||
460 | /** |
||
461 | * @return array[] |
||
462 | */ |
||
463 | public function getWellKnownHandlers(): array { |
||
465 | } |
||
466 | } |
||
467 |
This class constant has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.