| Total Complexity | 44 |
| Total Lines | 351 |
| 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 |
||
| 37 | class RegistrationContext { |
||
| 38 | |||
| 39 | /** @var array[] */ |
||
| 40 | private $capabilities = []; |
||
| 41 | |||
| 42 | /** @var array[] */ |
||
| 43 | private $crashReporters = []; |
||
| 44 | |||
| 45 | /** @var array[] */ |
||
| 46 | private $dashboardPanels = []; |
||
| 47 | |||
| 48 | /** @var array[] */ |
||
| 49 | private $services = []; |
||
| 50 | |||
| 51 | /** @var array[] */ |
||
| 52 | private $aliases = []; |
||
| 53 | |||
| 54 | /** @var array[] */ |
||
| 55 | private $parameters = []; |
||
| 56 | |||
| 57 | /** @var array[] */ |
||
| 58 | private $eventListeners = []; |
||
| 59 | |||
| 60 | /** @var array[] */ |
||
| 61 | private $middlewares = []; |
||
| 62 | |||
| 63 | /** @var array[] */ |
||
| 64 | private $searchProviders = []; |
||
| 65 | |||
| 66 | /** @var ILogger */ |
||
| 67 | private $logger; |
||
| 68 | |||
| 69 | public function __construct(ILogger $logger) { |
||
| 71 | } |
||
| 72 | |||
| 73 | public function for(string $appId): IRegistrationContext { |
||
| 74 | return new class($appId, $this) implements IRegistrationContext { |
||
| 75 | /** @var string */ |
||
| 76 | private $appId; |
||
| 77 | |||
| 78 | /** @var RegistrationContext */ |
||
| 79 | private $context; |
||
| 80 | |||
| 81 | public function __construct(string $appId, RegistrationContext $context) { |
||
| 82 | $this->appId = $appId; |
||
| 83 | $this->context = $context; |
||
| 84 | } |
||
| 85 | |||
| 86 | public function registerCapability(string $capability): void { |
||
| 87 | $this->context->registerCapability( |
||
| 88 | $this->appId, |
||
| 89 | $capability |
||
| 90 | ); |
||
| 91 | } |
||
| 92 | |||
| 93 | public function registerCrashReporter(string $reporterClass): void { |
||
| 94 | $this->context->registerCrashReporter( |
||
| 95 | $this->appId, |
||
| 96 | $reporterClass |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function registerDashboardPanel(string $panelClass): void { |
||
| 101 | $this->context->registerDashboardPanel( |
||
| 102 | $this->appId, |
||
| 103 | $panelClass |
||
| 104 | ); |
||
| 105 | } |
||
| 106 | |||
| 107 | public function registerService(string $name, callable $factory, bool $shared = true): void { |
||
| 108 | $this->context->registerService( |
||
| 109 | $this->appId, |
||
| 110 | $name, |
||
| 111 | $factory, |
||
| 112 | $shared |
||
| 113 | ); |
||
| 114 | } |
||
| 115 | |||
| 116 | public function registerServiceAlias(string $alias, string $target): void { |
||
| 117 | $this->context->registerServiceAlias( |
||
| 118 | $this->appId, |
||
| 119 | $alias, |
||
| 120 | $target |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function registerParameter(string $name, $value): void { |
||
| 125 | $this->context->registerParameter( |
||
| 126 | $this->appId, |
||
| 127 | $name, |
||
| 128 | $value |
||
| 129 | ); |
||
| 130 | } |
||
| 131 | |||
| 132 | public function registerEventListener(string $event, string $listener, int $priority = 0): void { |
||
| 133 | $this->context->registerEventListener( |
||
| 134 | $this->appId, |
||
| 135 | $event, |
||
| 136 | $listener, |
||
| 137 | $priority |
||
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function registerMiddleware(string $class): void { |
||
| 142 | $this->context->registerMiddleware( |
||
| 143 | $this->appId, |
||
| 144 | $class |
||
| 145 | ); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function registerSearchProvider(string $class): void { |
||
| 149 | $this->context->registerSearchProvider( |
||
| 150 | $this->appId, |
||
| 151 | $class |
||
| 152 | ); |
||
| 153 | } |
||
| 154 | }; |
||
| 155 | } |
||
| 156 | |||
| 157 | public function registerCapability(string $appId, string $capability): void { |
||
| 158 | $this->capabilities[] = [ |
||
| 159 | 'appId' => $appId, |
||
| 160 | 'capability' => $capability |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function registerCrashReporter(string $appId, string $reporterClass): void { |
||
| 165 | $this->crashReporters[] = [ |
||
| 166 | 'appId' => $appId, |
||
| 167 | 'class' => $reporterClass, |
||
| 168 | ]; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function registerDashboardPanel(string $appId, string $panelClass): void { |
||
| 172 | $this->dashboardPanels[] = [ |
||
| 173 | 'appId' => $appId, |
||
| 174 | 'class' => $panelClass |
||
| 175 | ]; |
||
| 176 | } |
||
| 177 | |||
| 178 | public function registerService(string $appId, string $name, callable $factory, bool $shared = true): void { |
||
| 179 | $this->services[] = [ |
||
| 180 | "appId" => $appId, |
||
| 181 | "name" => $name, |
||
| 182 | "factory" => $factory, |
||
| 183 | "sharred" => $shared, |
||
| 184 | ]; |
||
| 185 | } |
||
| 186 | |||
| 187 | public function registerServiceAlias(string $appId, string $alias, string $target): void { |
||
| 188 | $this->aliases[] = [ |
||
| 189 | "appId" => $appId, |
||
| 190 | "alias" => $alias, |
||
| 191 | "target" => $target, |
||
| 192 | ]; |
||
| 193 | } |
||
| 194 | |||
| 195 | public function registerParameter(string $appId, string $name, $value): void { |
||
| 196 | $this->parameters[] = [ |
||
| 197 | "appId" => $appId, |
||
| 198 | "name" => $name, |
||
| 199 | "value" => $value, |
||
| 200 | ]; |
||
| 201 | } |
||
| 202 | |||
| 203 | public function registerEventListener(string $appId, string $event, string $listener, int $priority = 0): void { |
||
| 204 | $this->eventListeners[] = [ |
||
| 205 | "appId" => $appId, |
||
| 206 | "event" => $event, |
||
| 207 | "listener" => $listener, |
||
| 208 | "priority" => $priority, |
||
| 209 | ]; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function registerMiddleware(string $appId, string $class): void { |
||
| 213 | $this->middlewares[] = [ |
||
| 214 | "appId" => $appId, |
||
| 215 | "class" => $class, |
||
| 216 | ]; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function registerSearchProvider(string $appId, string $class) { |
||
| 220 | $this->searchProviders[] = [ |
||
| 221 | 'appId' => $appId, |
||
| 222 | 'class' => $class, |
||
| 223 | ]; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param App[] $apps |
||
| 228 | */ |
||
| 229 | public function delegateCapabilityRegistrations(array $apps): void { |
||
| 230 | foreach ($this->capabilities as $registration) { |
||
| 231 | try { |
||
| 232 | $apps[$registration['appId']] |
||
| 233 | ->getContainer() |
||
| 234 | ->registerCapability($registration['capability']); |
||
| 235 | } catch (Throwable $e) { |
||
| 236 | $appId = $registration['appId']; |
||
| 237 | $this->logger->logException($e, [ |
||
| 238 | 'message' => "Error during capability registration of $appId: " . $e->getMessage(), |
||
| 239 | 'level' => ILogger::ERROR, |
||
| 240 | ]); |
||
| 241 | } |
||
| 242 | } |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param App[] $apps |
||
| 247 | */ |
||
| 248 | public function delegateCrashReporterRegistrations(array $apps, Registry $registry): void { |
||
|
|
|||
| 249 | foreach ($this->crashReporters as $registration) { |
||
| 250 | try { |
||
| 251 | $registry->registerLazy($registration['class']); |
||
| 252 | } catch (Throwable $e) { |
||
| 253 | $appId = $registration['appId']; |
||
| 254 | $this->logger->logException($e, [ |
||
| 255 | 'message' => "Error during crash reporter registration of $appId: " . $e->getMessage(), |
||
| 256 | 'level' => ILogger::ERROR, |
||
| 257 | ]); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @param App[] $apps |
||
| 264 | */ |
||
| 265 | public function delegateDashboardPanelRegistrations(array $apps, IManager $dashboardManager): void { |
||
| 274 | ]); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | public function delegateEventListenerRegistrations(IEventDispatcher $eventDispatcher): void { |
||
| 280 | foreach ($this->eventListeners as $registration) { |
||
| 281 | try { |
||
| 282 | if (isset($registration['priority'])) { |
||
| 283 | $eventDispatcher->addServiceListener( |
||
| 284 | $registration['event'], |
||
| 285 | $registration['listener'], |
||
| 286 | $registration['priority'] |
||
| 287 | ); |
||
| 288 | } else { |
||
| 289 | $eventDispatcher->addServiceListener( |
||
| 290 | $registration['event'], |
||
| 291 | $registration['listener'] |
||
| 292 | ); |
||
| 293 | } |
||
| 294 | } catch (Throwable $e) { |
||
| 295 | $appId = $registration['appId']; |
||
| 296 | $this->logger->logException($e, [ |
||
| 297 | 'message' => "Error during event listener registration of $appId: " . $e->getMessage(), |
||
| 298 | 'level' => ILogger::ERROR, |
||
| 299 | ]); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @param App[] $apps |
||
| 306 | */ |
||
| 307 | public function delegateContainerRegistrations(array $apps): void { |
||
| 308 | foreach ($this->services as $registration) { |
||
| 309 | try { |
||
| 310 | /** |
||
| 311 | * Register the service and convert the callable into a \Closure if necessary |
||
| 312 | */ |
||
| 313 | $apps[$registration['appId']] |
||
| 314 | ->getContainer() |
||
| 315 | ->registerService( |
||
| 316 | $registration['name'], |
||
| 317 | Closure::fromCallable($registration['factory']), |
||
| 318 | $registration['shared'] ?? true |
||
| 319 | ); |
||
| 320 | } catch (Throwable $e) { |
||
| 321 | $appId = $registration['appId']; |
||
| 322 | $this->logger->logException($e, [ |
||
| 323 | 'message' => "Error during service registration of $appId: " . $e->getMessage(), |
||
| 324 | 'level' => ILogger::ERROR, |
||
| 325 | ]); |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | foreach ($this->aliases as $registration) { |
||
| 330 | try { |
||
| 331 | $apps[$registration['appId']] |
||
| 332 | ->getContainer() |
||
| 333 | ->registerAlias( |
||
| 334 | $registration['alias'], |
||
| 335 | $registration['target'] |
||
| 336 | ); |
||
| 337 | } catch (Throwable $e) { |
||
| 338 | $appId = $registration['appId']; |
||
| 339 | $this->logger->logException($e, [ |
||
| 340 | 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
||
| 341 | 'level' => ILogger::ERROR, |
||
| 342 | ]); |
||
| 343 | } |
||
| 344 | } |
||
| 345 | |||
| 346 | foreach ($this->parameters as $registration) { |
||
| 347 | try { |
||
| 348 | $apps[$registration['appId']] |
||
| 349 | ->getContainer() |
||
| 350 | ->registerParameter( |
||
| 351 | $registration['name'], |
||
| 352 | $registration['value'] |
||
| 353 | ); |
||
| 354 | } catch (Throwable $e) { |
||
| 355 | $appId = $registration['appId']; |
||
| 356 | $this->logger->logException($e, [ |
||
| 357 | 'message' => "Error during service alias registration of $appId: " . $e->getMessage(), |
||
| 358 | 'level' => ILogger::ERROR, |
||
| 359 | ]); |
||
| 360 | } |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @param App[] $apps |
||
| 366 | */ |
||
| 367 | public function delegateMiddlewareRegistrations(array $apps): void { |
||
| 378 | ]); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * @return array[] |
||
| 385 | */ |
||
| 386 | public function getSearchProviders(): array { |
||
| 388 | } |
||
| 389 | } |
||
| 390 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.