| Total Complexity | 54 |
| Total Lines | 460 |
| Duplicated Lines | 0 % |
| Coverage | 95.71% |
| Changes | 9 | ||
| Bugs | 1 | Features | 0 |
Complex classes like SetupGacela 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 SetupGacela, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | final class SetupGacela extends AbstractSetupGacela |
||
| 23 | { |
||
| 24 | public const shouldResetInMemoryCache = 'shouldResetInMemoryCache'; |
||
| 25 | public const fileCacheEnabled = 'fileCacheEnabled'; |
||
| 26 | public const fileCacheDirectory = 'fileCacheDirectory'; |
||
| 27 | public const externalServices = 'externalServices'; |
||
| 28 | public const projectNamespaces = 'projectNamespaces'; |
||
| 29 | public const configKeyValues = 'configKeyValues'; |
||
| 30 | public const servicesToExtend = 'servicesToExtend'; |
||
| 31 | |||
| 32 | private const DEFAULT_ARE_EVENT_LISTENERS_ENABLED = true; |
||
| 33 | private const DEFAULT_SHOULD_RESET_IN_MEMORY_CACHE = false; |
||
| 34 | private const DEFAULT_FILE_CACHE_ENABLED = GacelaFileCache::DEFAULT_ENABLED_VALUE; |
||
| 35 | private const DEFAULT_FILE_CACHE_DIRECTORY = GacelaFileCache::DEFAULT_DIRECTORY_VALUE; |
||
| 36 | private const DEFAULT_PROJECT_NAMESPACES = []; |
||
| 37 | private const DEFAULT_CONFIG_KEY_VALUES = []; |
||
| 38 | private const DEFAULT_GENERIC_LISTENERS = []; |
||
| 39 | private const DEFAULT_SPECIFIC_LISTENERS = []; |
||
| 40 | private const DEFAULT_SERVICES_TO_EXTEND = []; |
||
| 41 | |||
| 42 | /** @var callable(ConfigBuilder):void */ |
||
| 43 | private $configFn; |
||
| 44 | |||
| 45 | /** @var callable(MappingInterfacesBuilder,array<string,mixed>):void */ |
||
| 46 | private $mappingInterfacesFn; |
||
| 47 | |||
| 48 | /** @var callable(SuffixTypesBuilder):void */ |
||
| 49 | private $suffixTypesFn; |
||
| 50 | |||
| 51 | /** @var ?array<string,class-string|object|callable> */ |
||
| 52 | private ?array $externalServices = null; |
||
| 53 | |||
| 54 | private ?ConfigBuilder $configBuilder = null; |
||
| 55 | |||
| 56 | private ?SuffixTypesBuilder $suffixTypesBuilder = null; |
||
| 57 | |||
| 58 | private ?MappingInterfacesBuilder $mappingInterfacesBuilder = null; |
||
| 59 | |||
| 60 | private ?bool $shouldResetInMemoryCache = null; |
||
| 61 | |||
| 62 | private ?bool $fileCacheEnabled = null; |
||
| 63 | |||
| 64 | private ?string $fileCacheDirectory = null; |
||
| 65 | |||
| 66 | /** @var ?list<string> */ |
||
| 67 | private ?array $projectNamespaces = null; |
||
| 68 | |||
| 69 | /** @var ?array<string,mixed> */ |
||
| 70 | private ?array $configKeyValues = null; |
||
| 71 | |||
| 72 | private ?bool $areEventListenersEnabled = null; |
||
| 73 | |||
| 74 | /** @var ?list<callable> */ |
||
| 75 | private ?array $genericListeners = null; |
||
| 76 | |||
| 77 | /** @var ?array<class-string,list<callable>> */ |
||
| 78 | private ?array $specificListeners = null; |
||
| 79 | |||
| 80 | private ?EventDispatcherInterface $eventDispatcher = null; |
||
| 81 | |||
| 82 | /** @var ?array<string,bool> */ |
||
| 83 | private ?array $changedProperties = null; |
||
| 84 | |||
| 85 | /** @var ?array<string,list<Closure>> */ |
||
| 86 | private ?array $servicesToExtend = null; |
||
| 87 | |||
| 88 | 109 | public function __construct() |
|
| 89 | { |
||
| 90 | 109 | $emptyFn = static function (): void { |
|
| 91 | 60 | }; |
|
| 92 | |||
| 93 | 109 | $this->configFn = $emptyFn; |
|
| 94 | 109 | $this->mappingInterfacesFn = $emptyFn; |
|
| 95 | 109 | $this->suffixTypesFn = $emptyFn; |
|
| 96 | } |
||
| 97 | |||
| 98 | public static function fromFile(string $gacelaFilePath): self |
||
| 99 | { |
||
| 100 | if (!is_file($gacelaFilePath)) { |
||
| 101 | throw new RuntimeException("Invalid file path: '{$gacelaFilePath}'"); |
||
| 102 | } |
||
| 103 | |||
| 104 | /** @var callable(GacelaConfig):void|null $setupGacelaFileFn */ |
||
| 105 | $setupGacelaFileFn = include $gacelaFilePath; |
||
| 106 | if (!is_callable($setupGacelaFileFn)) { |
||
| 107 | return new self(); |
||
| 108 | } |
||
| 109 | |||
| 110 | return self::fromCallable($setupGacelaFileFn); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param callable(GacelaConfig):void $setupGacelaFileFn |
||
| 115 | */ |
||
| 116 | 56 | public static function fromCallable(callable $setupGacelaFileFn): self |
|
| 117 | { |
||
| 118 | 56 | $gacelaConfig = new GacelaConfig(); |
|
| 119 | 56 | $setupGacelaFileFn($gacelaConfig); |
|
| 120 | |||
| 121 | 56 | return self::fromGacelaConfig($gacelaConfig); |
|
| 122 | } |
||
| 123 | |||
| 124 | 73 | public static function fromGacelaConfig(GacelaConfig $gacelaConfig): self |
|
| 125 | { |
||
| 126 | 73 | $build = $gacelaConfig->build(); |
|
| 127 | |||
| 128 | 73 | return (new self()) |
|
| 129 | 73 | ->setExternalServices($build['external-services']) |
|
| 130 | 73 | ->setConfigBuilder($build['config-builder']) |
|
| 131 | 73 | ->setSuffixTypesBuilder($build['suffix-types-builder']) |
|
| 132 | 73 | ->setMappingInterfacesBuilder($build['mapping-interfaces-builder']) |
|
| 133 | 73 | ->setShouldResetInMemoryCache($build['should-reset-in-memory-cache']) |
|
| 134 | 73 | ->setFileCacheEnabled($build['file-cache-enabled']) |
|
| 135 | 73 | ->setFileCacheDirectory($build['file-cache-directory']) |
|
| 136 | 73 | ->setProjectNamespaces($build['project-namespaces']) |
|
| 137 | 73 | ->setConfigKeyValues($build['config-key-values']) |
|
| 138 | 73 | ->setAreEventListenersEnabled($build['are-event-listeners-enabled']) |
|
| 139 | 73 | ->setGenericListeners($build['generic-listeners']) |
|
| 140 | 73 | ->setSpecificListeners($build['specific-listeners']) |
|
| 141 | 73 | ->setServicesToExtend($build['services-to-extend']); |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @param array<string,class-string|object|callable> $array |
||
| 146 | */ |
||
| 147 | 76 | public function setExternalServices(array $array): self |
|
| 148 | { |
||
| 149 | 76 | $this->markPropertyChanged(self::externalServices, true); |
|
| 150 | 76 | $this->externalServices = $array; |
|
| 151 | |||
| 152 | 76 | return $this; |
|
| 153 | } |
||
| 154 | |||
| 155 | 73 | public function setConfigBuilder(ConfigBuilder $builder): self |
|
| 156 | { |
||
| 157 | 73 | $this->configBuilder = $builder; |
|
| 158 | |||
| 159 | 73 | return $this; |
|
| 160 | } |
||
| 161 | |||
| 162 | 73 | public function setSuffixTypesBuilder(SuffixTypesBuilder $builder): self |
|
| 163 | { |
||
| 164 | 73 | $this->suffixTypesBuilder = $builder; |
|
| 165 | |||
| 166 | 73 | return $this; |
|
| 167 | } |
||
| 168 | |||
| 169 | 73 | public function setMappingInterfacesBuilder(MappingInterfacesBuilder $builder): self |
|
| 170 | { |
||
| 171 | 73 | $this->mappingInterfacesBuilder = $builder; |
|
| 172 | |||
| 173 | 73 | return $this; |
|
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param callable(ConfigBuilder):void $callable |
||
| 178 | */ |
||
| 179 | 3 | public function setConfigFn(callable $callable): self |
|
| 180 | { |
||
| 181 | 3 | $this->configFn = $callable; |
|
| 182 | |||
| 183 | 3 | return $this; |
|
| 184 | } |
||
| 185 | |||
| 186 | 61 | public function buildConfig(ConfigBuilder $builder): ConfigBuilder |
|
| 187 | { |
||
| 188 | 61 | if ($this->configBuilder) { |
|
| 189 | 53 | $builder = $this->configBuilder; |
|
| 190 | } |
||
| 191 | |||
| 192 | 61 | ($this->configFn)($builder); |
|
| 193 | |||
| 194 | 61 | return $builder; |
|
| 195 | } |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param callable(MappingInterfacesBuilder,array<string,mixed>):void $callable |
||
| 199 | */ |
||
| 200 | 3 | public function setMappingInterfacesFn(callable $callable): self |
|
| 201 | { |
||
| 202 | 3 | $this->mappingInterfacesFn = $callable; |
|
| 203 | |||
| 204 | 3 | return $this; |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Define the mapping between interfaces and concretions, so Gacela services will auto-resolve them automatically. |
||
| 209 | * |
||
| 210 | * @param array<string,class-string|object|callable> $externalServices |
||
| 211 | */ |
||
| 212 | 61 | public function buildMappingInterfaces( |
|
| 213 | MappingInterfacesBuilder $builder, |
||
| 214 | array $externalServices, |
||
| 215 | ): MappingInterfacesBuilder { |
||
| 216 | 61 | if ($this->mappingInterfacesBuilder) { |
|
| 217 | 53 | $builder = $this->mappingInterfacesBuilder; |
|
| 218 | } |
||
| 219 | |||
| 220 | 61 | ($this->mappingInterfacesFn)( |
|
| 221 | 61 | $builder, |
|
| 222 | 61 | array_merge($this->externalServices ?? [], $externalServices) |
|
| 223 | 61 | ); |
|
| 224 | |||
| 225 | 61 | return $builder; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param callable(SuffixTypesBuilder):void $callable |
||
| 230 | */ |
||
| 231 | 3 | public function setSuffixTypesFn(callable $callable): self |
|
| 232 | { |
||
| 233 | 3 | $this->suffixTypesFn = $callable; |
|
| 234 | |||
| 235 | 3 | return $this; |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Allow overriding gacela resolvable types. |
||
| 240 | */ |
||
| 241 | 61 | public function buildSuffixTypes(SuffixTypesBuilder $builder): SuffixTypesBuilder |
|
| 242 | { |
||
| 243 | 61 | if ($this->suffixTypesBuilder) { |
|
| 244 | 53 | $builder = $this->suffixTypesBuilder; |
|
| 245 | } |
||
| 246 | |||
| 247 | 61 | ($this->suffixTypesFn)($builder); |
|
| 248 | |||
| 249 | 61 | return $builder; |
|
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return array<string,class-string|object|callable> |
||
| 254 | */ |
||
| 255 | 26 | public function externalServices(): array |
|
| 256 | { |
||
| 257 | 26 | return (array)$this->externalServices; |
|
| 258 | } |
||
| 259 | |||
| 260 | 73 | public function setShouldResetInMemoryCache(?bool $flag): self |
|
| 261 | { |
||
| 262 | 73 | $this->markPropertyChanged(self::shouldResetInMemoryCache, $flag); |
|
| 263 | 73 | $this->shouldResetInMemoryCache = $flag ?? self::DEFAULT_SHOULD_RESET_IN_MEMORY_CACHE; |
|
| 264 | |||
| 265 | 73 | return $this; |
|
| 266 | } |
||
| 267 | |||
| 268 | 85 | public function shouldResetInMemoryCache(): bool |
|
| 269 | { |
||
| 270 | 85 | return (bool)$this->shouldResetInMemoryCache; |
|
| 271 | } |
||
| 272 | |||
| 273 | 39 | public function isFileCacheEnabled(): bool |
|
| 274 | { |
||
| 275 | 39 | return (bool)$this->fileCacheEnabled; |
|
| 276 | } |
||
| 277 | |||
| 278 | 6 | public function getFileCacheDirectory(): string |
|
| 279 | { |
||
| 280 | 6 | return (string)$this->fileCacheDirectory; |
|
| 281 | } |
||
| 282 | |||
| 283 | 73 | public function setFileCacheDirectory(?string $dir): self |
|
| 284 | { |
||
| 285 | 73 | $this->markPropertyChanged(self::fileCacheDirectory, $dir); |
|
| 286 | 73 | $this->fileCacheDirectory = $dir ?? self::DEFAULT_FILE_CACHE_DIRECTORY; |
|
| 287 | |||
| 288 | 73 | return $this; |
|
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @param ?list<string> $list |
||
| 293 | */ |
||
| 294 | 73 | public function setProjectNamespaces(?array $list): self |
|
| 295 | { |
||
| 296 | 73 | $this->markPropertyChanged(self::projectNamespaces, $list); |
|
| 297 | 73 | $this->projectNamespaces = $list ?? self::DEFAULT_PROJECT_NAMESPACES; |
|
|
1 ignored issue
–
show
|
|||
| 298 | |||
| 299 | 73 | return $this; |
|
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @return list<string> |
||
| 304 | */ |
||
| 305 | 38 | public function getProjectNamespaces(): array |
|
| 306 | { |
||
| 307 | 38 | return (array)$this->projectNamespaces; |
|
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * @return array<string,mixed> |
||
| 312 | */ |
||
| 313 | 85 | public function getConfigKeyValues(): array |
|
| 314 | { |
||
| 315 | 85 | return (array)$this->configKeyValues; |
|
| 316 | } |
||
| 317 | |||
| 318 | 49 | public function getEventDispatcher(): EventDispatcherInterface |
|
| 319 | { |
||
| 320 | 49 | if ($this->eventDispatcher !== null) { |
|
| 321 | 18 | return $this->eventDispatcher; |
|
| 322 | } |
||
| 323 | |||
| 324 | 35 | if ($this->canCreateEventDispatcher()) { |
|
| 325 | 11 | $this->eventDispatcher = new ConfigurableEventDispatcher(); |
|
| 326 | 11 | $this->eventDispatcher->registerGenericListeners($this->genericListeners ?? []); |
|
|
1 ignored issue
–
show
|
|||
| 327 | |||
| 328 | 11 | foreach ($this->specificListeners ?? [] as $event => $listeners) { |
|
| 329 | 5 | foreach ($listeners as $callable) { |
|
| 330 | 5 | $this->eventDispatcher->registerSpecificListener($event, $callable); |
|
| 331 | } |
||
| 332 | } |
||
| 333 | } else { |
||
| 334 | 24 | $this->eventDispatcher = new NullEventDispatcher(); |
|
| 335 | } |
||
| 336 | |||
| 337 | 35 | return $this->eventDispatcher; |
|
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @return array<string,list<Closure>> |
||
| 342 | */ |
||
| 343 | 35 | public function getServicesToExtend(): array |
|
| 344 | { |
||
| 345 | 35 | return (array)$this->servicesToExtend; |
|
| 346 | } |
||
| 347 | |||
| 348 | 73 | public function setFileCacheEnabled(?bool $flag): self |
|
| 349 | { |
||
| 350 | 73 | $this->markPropertyChanged(self::fileCacheEnabled, $flag); |
|
| 351 | 73 | $this->fileCacheEnabled = $flag ?? self::DEFAULT_FILE_CACHE_ENABLED; |
|
| 352 | |||
| 353 | 73 | return $this; |
|
| 354 | } |
||
| 355 | |||
| 356 | 58 | public function canCreateEventDispatcher(): bool |
|
| 357 | { |
||
| 358 | 58 | return $this->areEventListenersEnabled |
|
| 359 | 58 | && $this->hasEventListeners(); |
|
| 360 | } |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param ?array<string,mixed> $configKeyValues |
||
| 364 | */ |
||
| 365 | 73 | public function setConfigKeyValues(?array $configKeyValues): self |
|
| 366 | { |
||
| 367 | 73 | $this->markPropertyChanged(self::configKeyValues, $configKeyValues); |
|
| 368 | 73 | $this->configKeyValues = $configKeyValues ?? self::DEFAULT_CONFIG_KEY_VALUES; |
|
| 369 | |||
| 370 | 73 | return $this; |
|
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @return array<class-string,list<callable>>|null |
||
| 375 | */ |
||
| 376 | 3 | public function getSpecificListeners(): ?array |
|
| 377 | { |
||
| 378 | 3 | return $this->specificListeners; |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * @return list<callable>|null |
||
| 383 | */ |
||
| 384 | 3 | public function getGenericListeners(): ?array |
|
| 385 | { |
||
| 386 | 3 | return $this->genericListeners; |
|
| 387 | } |
||
| 388 | |||
| 389 | 26 | public function isPropertyChanged(string $name): bool |
|
| 390 | { |
||
| 391 | 26 | return $this->changedProperties[$name] ?? false; |
|
| 392 | } |
||
| 393 | |||
| 394 | 26 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): self |
|
| 395 | { |
||
| 396 | 26 | $this->eventDispatcher = $eventDispatcher; |
|
| 397 | |||
| 398 | 26 | return $this; |
|
| 399 | } |
||
| 400 | |||
| 401 | 26 | public function combine(self $other): self |
|
| 402 | { |
||
| 403 | 26 | return (new SetupCombinator($this))->combine($other); |
|
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param list<Closure> $servicesToExtend |
||
| 408 | */ |
||
| 409 | 1 | public function addServicesToExtend(string $serviceId, array $servicesToExtend): self |
|
| 410 | { |
||
| 411 | 1 | $this->servicesToExtend[$serviceId] ??= []; |
|
| 412 | 1 | $this->servicesToExtend[$serviceId] = array_merge( |
|
| 413 | 1 | $this->servicesToExtend[$serviceId], |
|
| 414 | 1 | $servicesToExtend, |
|
| 415 | 1 | ); |
|
| 416 | |||
| 417 | 1 | return $this; |
|
| 418 | } |
||
| 419 | |||
| 420 | 26 | public function combineExternalServices(array $list): void |
|
| 421 | { |
||
| 422 | 26 | $this->setExternalServices(array_merge($this->externalServices ?? [], $list)); |
|
| 423 | } |
||
| 424 | |||
| 425 | 1 | public function combineProjectNamespaces(array $list): void |
|
| 426 | { |
||
| 427 | 1 | $this->setProjectNamespaces(array_merge($this->projectNamespaces ?? [], $list)); |
|
| 428 | } |
||
| 429 | |||
| 430 | 1 | public function combineConfigKeyValues(array $list): void |
|
| 431 | { |
||
| 432 | 1 | $this->setConfigKeyValues(array_merge($this->configKeyValues ?? [], $list)); |
|
| 433 | } |
||
| 434 | |||
| 435 | 73 | private function setAreEventListenersEnabled(?bool $flag): self |
|
| 436 | { |
||
| 437 | 73 | $this->areEventListenersEnabled = $flag ?? self::DEFAULT_ARE_EVENT_LISTENERS_ENABLED; |
|
| 438 | |||
| 439 | 73 | return $this; |
|
| 440 | } |
||
| 441 | |||
| 442 | 57 | private function hasEventListeners(): bool |
|
| 443 | { |
||
| 444 | 57 | return !empty($this->genericListeners) |
|
| 445 | 57 | || !empty($this->specificListeners); |
|
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @param ?list<callable> $listeners |
||
| 450 | */ |
||
| 451 | 73 | private function setGenericListeners(?array $listeners): self |
|
| 452 | { |
||
| 453 | 73 | $this->genericListeners = $listeners ?? self::DEFAULT_GENERIC_LISTENERS; |
|
|
1 ignored issue
–
show
|
|||
| 454 | |||
| 455 | 73 | return $this; |
|
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * @param ?array<string,list<Closure>> $list |
||
| 460 | */ |
||
| 461 | 73 | private function setServicesToExtend(?array $list): self |
|
| 462 | { |
||
| 463 | 73 | $this->markPropertyChanged(self::servicesToExtend, $list); |
|
| 464 | 73 | $this->servicesToExtend = $list ?? self::DEFAULT_SERVICES_TO_EXTEND; |
|
| 465 | |||
| 466 | 73 | return $this; |
|
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param ?array<class-string,list<callable>> $listeners |
||
| 471 | */ |
||
| 472 | 73 | private function setSpecificListeners(?array $listeners): self |
|
| 473 | { |
||
| 474 | 73 | $this->specificListeners = $listeners ?? self::DEFAULT_SPECIFIC_LISTENERS; |
|
| 475 | |||
| 476 | 73 | return $this; |
|
| 477 | } |
||
| 478 | |||
| 479 | 76 | private function markPropertyChanged(string $name, mixed $value): void |
|
| 482 | } |
||
| 483 | } |
||
| 484 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..