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