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