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