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