Complex classes like Configuration 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Configuration, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | * Configuration options for an ORM Configuration |
||
20 | * |
||
21 | * @link http://www.doctrine-project.org/ |
||
22 | */ |
||
23 | class Configuration extends DBALConfiguration |
||
24 | { |
||
25 | /** |
||
26 | * Set the cache key for the metadata cache. Cache key |
||
27 | * is assembled as "doctrine.cache.{key}" and pulled from |
||
28 | * service locator. |
||
29 | */ |
||
30 | protected string $metadataCache = 'array'; |
||
|
|||
31 | |||
32 | /** |
||
33 | * Set the cache key for the query cache. Cache key |
||
34 | * is assembled as "doctrine.cache.{key}" and pulled from |
||
35 | * service locator. |
||
36 | */ |
||
37 | protected string $queryCache = 'array'; |
||
38 | |||
39 | /** |
||
40 | * Set the cache key for the result cache. Cache key |
||
41 | * is assembled as "doctrine.cache.{key}" and pulled from |
||
42 | * service locator. |
||
43 | */ |
||
44 | protected string $resultCache = 'array'; |
||
45 | |||
46 | /** |
||
47 | * Set the cache key for the hydration cache. Cache key |
||
48 | * is assembled as "doctrine.cache.{key}" and pulled from |
||
49 | * service locator. |
||
50 | */ |
||
51 | protected string $hydrationCache = 'array'; |
||
52 | |||
53 | /** |
||
54 | * Set the driver key for the metadata driver. Driver key |
||
55 | * is assembled as "doctrine.driver.{key}" and pulled from |
||
56 | * service locator. |
||
57 | */ |
||
58 | protected string $driver = 'orm_default'; |
||
59 | |||
60 | /** |
||
61 | * Automatic generation of proxies (disable for production!) |
||
62 | */ |
||
63 | protected bool $generateProxies = true; |
||
64 | |||
65 | /** |
||
66 | * Proxy directory. |
||
67 | */ |
||
68 | protected string $proxyDir = 'data'; |
||
69 | |||
70 | /** |
||
71 | * Proxy namespace. |
||
72 | */ |
||
73 | protected string $proxyNamespace = 'DoctrineORMModule\Proxy'; |
||
74 | |||
75 | /** |
||
76 | * Entity alias map. |
||
77 | * |
||
78 | * @var string[] |
||
79 | */ |
||
80 | protected array $entityNamespaces = []; |
||
81 | |||
82 | /** |
||
83 | * Keys must be function names and values the FQCN of the implementing class. |
||
84 | * The function names will be case-insensitive in DQL. |
||
85 | * |
||
86 | * @var string[] |
||
87 | */ |
||
88 | protected array $datetimeFunctions = []; |
||
89 | |||
90 | /** |
||
91 | * Keys must be function names and values the FQCN of the implementing class. |
||
92 | * The function names will be case-insensitive in DQL. |
||
93 | * |
||
94 | * @var string[] |
||
95 | */ |
||
96 | protected array $stringFunctions = []; |
||
97 | |||
98 | /** |
||
99 | * Keys must be function names and values the FQCN of the implementing class. |
||
100 | * The function names will be case-insensitive in DQL. |
||
101 | * |
||
102 | * @var string[] |
||
103 | */ |
||
104 | protected array $numericFunctions = []; |
||
105 | |||
106 | /** |
||
107 | * Keys must be the name of the custom filter and the value must be |
||
108 | * the class name for the custom filter. |
||
109 | * |
||
110 | * @var string[] |
||
111 | */ |
||
112 | protected array $filters = []; |
||
113 | |||
114 | /** |
||
115 | * Keys must be the name of the query and values the DQL query string. |
||
116 | * |
||
117 | * @var string[] |
||
118 | */ |
||
119 | protected array $namedQueries = []; |
||
120 | |||
121 | /** |
||
122 | * Keys must be the name of the query and the value is an array containing |
||
123 | * the keys 'sql' for native SQL query string and 'rsm' for the Query\ResultSetMapping. |
||
124 | * |
||
125 | * @var mixed[] |
||
126 | */ |
||
127 | protected array $namedNativeQueries = []; |
||
128 | |||
129 | /** |
||
130 | * Keys must be the name of the custom hydration method and the value must be |
||
131 | * the class name for the custom hydrator |
||
132 | * |
||
133 | * @var string[] |
||
134 | */ |
||
135 | protected array $customHydrationModes = []; |
||
136 | |||
137 | /** |
||
138 | * Naming strategy or name of the naming strategy service to be set in ORM |
||
139 | * configuration (if any) |
||
140 | * |
||
141 | * @var string|NamingStrategy|null |
||
142 | */ |
||
143 | protected $namingStrategy; |
||
144 | |||
145 | /** |
||
146 | * Quote strategy or name of the quote strategy service to be set in ORM |
||
147 | * configuration (if any) |
||
148 | * |
||
149 | * @var string|QuoteStrategy|null |
||
150 | */ |
||
151 | protected $quoteStrategy; |
||
152 | |||
153 | /** |
||
154 | * Default repository class |
||
155 | */ |
||
156 | protected ?string $defaultRepositoryClassName = null; |
||
157 | |||
158 | /** |
||
159 | * Repository factory or name of the repository factory service to be set in ORM |
||
160 | * configuration (if any) |
||
161 | * |
||
162 | * @var string|RepositoryFactory|null |
||
163 | */ |
||
164 | protected $repositoryFactory; |
||
165 | |||
166 | /** |
||
167 | * Class name of MetaData factory to be set in ORM. |
||
168 | * The entityManager will create a new instance on construction. |
||
169 | */ |
||
170 | protected ?string $classMetadataFactoryName = null; |
||
171 | |||
172 | /** |
||
173 | * Entity listener resolver or service name of the entity listener resolver |
||
174 | * to be set in ORM configuration (if any) |
||
175 | * |
||
176 | * @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html |
||
177 | * |
||
178 | * @var string|EntityListenerResolver|null |
||
179 | */ |
||
180 | protected $entityListenerResolver; |
||
181 | |||
182 | /** |
||
183 | * Configuration for second level cache |
||
184 | * |
||
185 | * @link http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/second-level-cache.html |
||
186 | */ |
||
187 | protected ?SecondLevelCacheConfiguration $secondLevelCache = null; |
||
188 | |||
189 | /** |
||
190 | * Configuration option for the filter schema assets expression |
||
191 | */ |
||
192 | protected ?string $filterSchemaAssetsExpression = null; |
||
193 | |||
194 | /** |
||
195 | * @param string[] $datetimeFunctions |
||
196 | */ |
||
197 | public function setDatetimeFunctions(array $datetimeFunctions) : self |
||
198 | { |
||
199 | $this->datetimeFunctions = $datetimeFunctions; |
||
200 | |||
201 | return $this; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @return string[] |
||
206 | */ |
||
207 | public function getDatetimeFunctions() : array |
||
208 | { |
||
209 | return $this->datetimeFunctions; |
||
210 | } |
||
211 | |||
212 | public function setDriver(string $driver) : self |
||
213 | { |
||
214 | $this->driver = $driver; |
||
215 | |||
216 | 72 | return $this; |
|
217 | } |
||
218 | 72 | ||
219 | public function getDriver() : string |
||
220 | 72 | { |
|
221 | return 'doctrine.driver.' . $this->driver; |
||
222 | } |
||
223 | |||
224 | /** |
||
225 | * @param string[] $entityNamespaces |
||
226 | 88 | */ |
|
227 | public function setEntityNamespaces(array $entityNamespaces) : self |
||
228 | 88 | { |
|
229 | $this->entityNamespaces = $entityNamespaces; |
||
230 | |||
231 | return $this; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | 72 | * @return string[] |
|
236 | */ |
||
237 | 72 | public function getEntityNamespaces() : array |
|
238 | { |
||
239 | 72 | return $this->entityNamespaces; |
|
240 | } |
||
241 | |||
242 | public function setGenerateProxies(bool $generateProxies) : self |
||
243 | { |
||
244 | $this->generateProxies = $generateProxies; |
||
245 | 88 | ||
246 | return $this; |
||
247 | 88 | } |
|
248 | |||
249 | public function getGenerateProxies() : bool |
||
250 | { |
||
251 | return $this->generateProxies; |
||
252 | } |
||
253 | |||
254 | public function setMetadataCache(string $metadataCache) : self |
||
255 | { |
||
256 | $this->metadataCache = $metadataCache; |
||
257 | |||
258 | return $this; |
||
259 | } |
||
260 | |||
261 | public function getMetadataCache() : string |
||
262 | { |
||
263 | return 'doctrine.cache.' . $this->metadataCache; |
||
264 | 88 | } |
|
265 | |||
266 | 88 | public function setResultCache(string $resultCache) : self |
|
267 | { |
||
268 | $this->resultCache = $resultCache; |
||
269 | |||
270 | return $this; |
||
271 | } |
||
272 | |||
273 | 72 | public function getResultCache() : string |
|
274 | { |
||
275 | 72 | return 'doctrine.cache.' . $this->resultCache; |
|
276 | } |
||
277 | 72 | ||
278 | public function setHydrationCache(string $hydrationCache) : self |
||
279 | { |
||
280 | $this->hydrationCache = $hydrationCache; |
||
281 | |||
282 | return $this; |
||
283 | 88 | } |
|
284 | |||
285 | 88 | public function getHydrationCache() : string |
|
286 | { |
||
287 | return 'doctrine.cache.' . $this->hydrationCache; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @param string[] $namedNativeQueries |
||
292 | 72 | */ |
|
293 | public function setNamedNativeQueries(array $namedNativeQueries) : self |
||
294 | 72 | { |
|
295 | $this->namedNativeQueries = $namedNativeQueries; |
||
296 | 72 | ||
297 | return $this; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @return string[] |
||
302 | 88 | */ |
|
303 | public function getNamedNativeQueries() : array |
||
304 | 88 | { |
|
305 | return $this->namedNativeQueries; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @param string[] $namedQueries |
||
310 | */ |
||
311 | 73 | public function setNamedQueries(array $namedQueries) : self |
|
312 | { |
||
313 | 73 | $this->namedQueries = $namedQueries; |
|
314 | |||
315 | 73 | return $this; |
|
316 | } |
||
317 | |||
318 | /** |
||
319 | * @return string[] |
||
320 | */ |
||
321 | 88 | public function getNamedQueries() : array |
|
322 | { |
||
323 | 88 | return $this->namedQueries; |
|
324 | } |
||
325 | |||
326 | /** |
||
327 | * @param string[] $numericFunctions |
||
328 | */ |
||
329 | public function setNumericFunctions(array $numericFunctions) : self |
||
330 | 74 | { |
|
331 | $this->numericFunctions = $numericFunctions; |
||
332 | 74 | ||
333 | return $this; |
||
334 | 74 | } |
|
335 | |||
336 | /** |
||
337 | * @return string[] |
||
338 | */ |
||
339 | public function getNumericFunctions() : array |
||
340 | 88 | { |
|
341 | return $this->numericFunctions; |
||
342 | 88 | } |
|
343 | |||
344 | /** |
||
345 | * @param string[] $filters |
||
346 | */ |
||
347 | public function setFilters(array $filters) : self |
||
348 | { |
||
349 | $this->filters = $filters; |
||
350 | |||
351 | return $this; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * @return string[] |
||
356 | */ |
||
357 | public function getFilters() : array |
||
358 | { |
||
359 | 88 | return $this->filters; |
|
360 | } |
||
361 | 88 | ||
362 | public function setProxyDir(string $proxyDir) : self |
||
363 | { |
||
364 | $this->proxyDir = $proxyDir; |
||
365 | |||
366 | return $this; |
||
367 | } |
||
368 | |||
369 | public function getProxyDir() : string |
||
370 | { |
||
371 | return $this->proxyDir; |
||
372 | } |
||
373 | |||
374 | public function setProxyNamespace(string $proxyNamespace) : self |
||
375 | { |
||
376 | $this->proxyNamespace = $proxyNamespace; |
||
377 | |||
378 | 88 | return $this; |
|
379 | } |
||
380 | 88 | ||
381 | public function getProxyNamespace() : string |
||
382 | { |
||
383 | return $this->proxyNamespace; |
||
384 | } |
||
385 | |||
386 | public function setQueryCache(string $queryCache) : self |
||
387 | 72 | { |
|
388 | $this->queryCache = $queryCache; |
||
389 | 72 | ||
390 | return $this; |
||
391 | 72 | } |
|
392 | |||
393 | public function getQueryCache() : string |
||
394 | { |
||
395 | return 'doctrine.cache.' . $this->queryCache; |
||
396 | } |
||
397 | 88 | ||
398 | /** |
||
399 | 88 | * @param string[] $stringFunctions |
|
400 | */ |
||
401 | public function setStringFunctions(array $stringFunctions) : self |
||
402 | { |
||
403 | $this->stringFunctions = $stringFunctions; |
||
404 | |||
405 | return $this; |
||
406 | } |
||
407 | 72 | ||
408 | /** |
||
409 | 72 | * @return string[] |
|
410 | */ |
||
411 | 72 | public function getStringFunctions() : array |
|
412 | { |
||
413 | return $this->stringFunctions; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param string[] $modes |
||
418 | 88 | */ |
|
419 | public function setCustomHydrationModes(array $modes) : self |
||
420 | 88 | { |
|
421 | $this->customHydrationModes = $modes; |
||
422 | |||
423 | return $this; |
||
424 | } |
||
425 | |||
426 | /** |
||
427 | 72 | * @return string[] |
|
428 | */ |
||
429 | 72 | public function getCustomHydrationModes() : array |
|
430 | { |
||
431 | 72 | return $this->customHydrationModes; |
|
432 | } |
||
433 | |||
434 | /** |
||
435 | * @param string|NamingStrategy|null $namingStrategy |
||
436 | * |
||
437 | 88 | * When the provided naming strategy does not fit the expected type |
|
438 | * |
||
439 | 88 | * @throws InvalidArgumentException |
|
440 | */ |
||
441 | public function setNamingStrategy($namingStrategy) : self |
||
442 | { |
||
443 | if ($namingStrategy === null |
||
444 | || is_string($namingStrategy) |
||
445 | || $namingStrategy instanceof NamingStrategy |
||
446 | 72 | ) { |
|
447 | $this->namingStrategy = $namingStrategy; |
||
448 | 72 | ||
449 | return $this; |
||
450 | 72 | } |
|
451 | |||
452 | throw new InvalidArgumentException( |
||
453 | sprintf( |
||
454 | 'namingStrategy must be either a string, a Doctrine\ORM\Mapping\NamingStrategy ' |
||
455 | . 'instance or null, %s given', |
||
456 | 88 | is_object($namingStrategy) ? get_class($namingStrategy) : gettype($namingStrategy) |
|
457 | ) |
||
458 | 88 | ); |
|
459 | } |
||
460 | |||
461 | /** |
||
462 | * @return string|NamingStrategy|null |
||
463 | */ |
||
464 | public function getNamingStrategy() |
||
465 | 72 | { |
|
466 | return $this->namingStrategy; |
||
467 | 72 | } |
|
468 | |||
469 | 72 | /** |
|
470 | * @param string|QuoteStrategy|null $quoteStrategy |
||
471 | * |
||
472 | * When the provided quote strategy does not fit the expected type |
||
473 | * |
||
474 | * @throws InvalidArgumentException |
||
475 | 88 | */ |
|
476 | public function setQuoteStrategy($quoteStrategy) : self |
||
477 | 88 | { |
|
478 | if ($quoteStrategy === null |
||
479 | || is_string($quoteStrategy) |
||
480 | || $quoteStrategy instanceof QuoteStrategy |
||
481 | ) { |
||
482 | $this->quoteStrategy = $quoteStrategy; |
||
483 | |||
484 | 72 | return $this; |
|
485 | } |
||
486 | 72 | ||
487 | throw new InvalidArgumentException( |
||
488 | 72 | sprintf( |
|
489 | 'quoteStrategy must be either a string, a Doctrine\ORM\Mapping\QuoteStrategy ' |
||
490 | . 'instance or null, %s given', |
||
491 | is_object($quoteStrategy) ? get_class($quoteStrategy) : gettype($quoteStrategy) |
||
492 | ) |
||
493 | ); |
||
494 | 88 | } |
|
495 | |||
496 | 88 | /** |
|
497 | * @return string|QuoteStrategy|null |
||
498 | */ |
||
499 | public function getQuoteStrategy() |
||
500 | { |
||
501 | return $this->quoteStrategy; |
||
502 | } |
||
503 | |||
504 | /** |
||
505 | * @param string|RepositoryFactory|null $repositoryFactory |
||
506 | * |
||
507 | * When the provided repository factory does not fit the expected type |
||
508 | * |
||
509 | * @throws InvalidArgumentException |
||
510 | */ |
||
511 | public function setRepositoryFactory($repositoryFactory) : self |
||
512 | { |
||
513 | 88 | if ($repositoryFactory === null |
|
514 | || is_string($repositoryFactory) |
||
515 | 88 | || $repositoryFactory instanceof RepositoryFactory |
|
516 | ) { |
||
517 | $this->repositoryFactory = $repositoryFactory; |
||
518 | |||
519 | return $this; |
||
520 | } |
||
521 | |||
522 | throw new InvalidArgumentException( |
||
523 | 4 | sprintf( |
|
524 | 'repositoryFactory must be either a string, a Doctrine\ORM\Repository\RepositoryFactory ' |
||
525 | 4 | . 'instance or null, %s given', |
|
526 | 4 | is_object($repositoryFactory) ? get_class($repositoryFactory) : gettype($repositoryFactory) |
|
527 | 4 | ) |
|
528 | ); |
||
529 | 4 | } |
|
530 | |||
531 | 4 | /** |
|
532 | * @return string|RepositoryFactory|null |
||
533 | */ |
||
534 | 1 | public function getRepositoryFactory() |
|
535 | 1 | { |
|
536 | return $this->repositoryFactory; |
||
537 | 1 | } |
|
538 | 1 | ||
539 | /** |
||
540 | * Set the metadata factory class name to use |
||
541 | * |
||
542 | * @see \Doctrine\ORM\Configuration::setClassMetadataFactoryName() |
||
543 | */ |
||
544 | public function setClassMetadataFactoryName(?string $factoryName) : self |
||
545 | { |
||
546 | 89 | $this->classMetadataFactoryName = $factoryName; |
|
547 | |||
548 | 89 | return $this; |
|
549 | } |
||
550 | |||
551 | public function getClassMetadataFactoryName() : ?string |
||
552 | { |
||
553 | return $this->classMetadataFactoryName; |
||
554 | } |
||
555 | |||
556 | 4 | /** |
|
557 | * @param string|EntityListenerResolver|null $entityListenerResolver |
||
558 | 4 | * |
|
559 | 4 | * When the provided entity listener resolver |
|
560 | 4 | * does not fit the expected type |
|
561 | * |
||
562 | 4 | * @throws InvalidArgumentException |
|
563 | */ |
||
564 | 4 | public function setEntityListenerResolver($entityListenerResolver) : self |
|
565 | { |
||
566 | if ($entityListenerResolver === null |
||
567 | 1 | || $entityListenerResolver instanceof EntityListenerResolver |
|
568 | 1 | || is_string($entityListenerResolver) |
|
569 | ) { |
||
570 | 1 | $this->entityListenerResolver = $entityListenerResolver; |
|
571 | 1 | ||
572 | return $this; |
||
573 | } |
||
574 | |||
575 | throw new InvalidArgumentException(sprintf( |
||
576 | 'entityListenerResolver must be either a string, a Doctrine\ORM\Mapping\EntityListenerResolver ' |
||
577 | . 'instance or null, %s given', |
||
578 | is_object($entityListenerResolver) ? get_class($entityListenerResolver) : gettype($entityListenerResolver) |
||
579 | 88 | )); |
|
580 | } |
||
581 | 88 | ||
582 | /** |
||
583 | * @return string|EntityListenerResolver|null |
||
584 | */ |
||
585 | public function getEntityListenerResolver() |
||
586 | { |
||
587 | return $this->entityListenerResolver; |
||
588 | } |
||
589 | 1 | ||
590 | /** |
||
591 | 1 | * @param mixed[] $secondLevelCache |
|
592 | 1 | */ |
|
593 | 1 | public function setSecondLevelCache(array $secondLevelCache) : self |
|
594 | { |
||
595 | 1 | $this->secondLevelCache = new SecondLevelCacheConfiguration($secondLevelCache); |
|
596 | |||
597 | 1 | return $this; |
|
598 | } |
||
599 | |||
600 | 1 | public function getSecondLevelCache() : SecondLevelCacheConfiguration |
|
601 | 1 | { |
|
602 | return $this->secondLevelCache ?: new SecondLevelCacheConfiguration(); |
||
603 | 1 | } |
|
604 | 1 | ||
605 | public function setFilterSchemaAssetsExpression(string $filterSchemaAssetsExpression) : self |
||
606 | { |
||
607 | $this->filterSchemaAssetsExpression = $filterSchemaAssetsExpression; |
||
608 | |||
609 | return $this; |
||
610 | } |
||
611 | |||
612 | 87 | public function getFilterSchemaAssetsExpression() : ?string |
|
613 | { |
||
614 | 87 | return $this->filterSchemaAssetsExpression; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * Sets default repository class. |
||
619 | */ |
||
620 | public function setDefaultRepositoryClassName(string $className) : self |
||
621 | { |
||
622 | $this->defaultRepositoryClassName = (string) $className; |
||
623 | |||
624 | return $this; |
||
625 | 1 | } |
|
626 | |||
627 | 1 | /** |
|
628 | * Get default repository class name. |
||
629 | 1 | */ |
|
630 | public function getDefaultRepositoryClassName() : ?string |
||
631 | { |
||
632 | return $this->defaultRepositoryClassName; |
||
633 | } |
||
634 | } |
||
635 |