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