Complex classes like RelationalBuilder 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 RelationalBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
42 | class RelationalBuilder extends AbstractManagerBuilder |
||
43 | { |
||
44 | /** |
||
45 | * Query cache driver. |
||
46 | * |
||
47 | * @var CacheProvider |
||
48 | */ |
||
49 | protected $queryCacheDriver; |
||
50 | |||
51 | /** |
||
52 | * Result cache driver. |
||
53 | * |
||
54 | * @var CacheProvider |
||
55 | */ |
||
56 | protected $resultCacheDriver; |
||
57 | |||
58 | /** |
||
59 | * Hydrator cache driver. |
||
60 | * |
||
61 | * @var CacheProvider |
||
62 | */ |
||
63 | protected $hydratorCacheDriver; |
||
64 | |||
65 | /** |
||
66 | * Naming strategy. |
||
67 | * |
||
68 | * @var NamingStrategy |
||
69 | */ |
||
70 | protected $namingStrategy; |
||
71 | |||
72 | /** |
||
73 | * Quote strategy. |
||
74 | * |
||
75 | * @var QuoteStrategy |
||
76 | */ |
||
77 | protected $quoteStrategy; |
||
78 | |||
79 | /** |
||
80 | * Second level cache configuration. |
||
81 | * |
||
82 | * @var CacheConfiguration |
||
83 | */ |
||
84 | protected $secondCacheConfig; |
||
85 | |||
86 | /** |
||
87 | * SQL logger. |
||
88 | * |
||
89 | * @var SQLLogger |
||
90 | */ |
||
91 | protected $SQLLogger; |
||
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | protected function getDefaultOptions() |
||
97 | { |
||
98 | return [ |
||
99 | 'connection' => [], // Array or \Doctrine\DBAL\Connection |
||
100 | 'proxies_namespace' => 'DoctrineRDBMSORMProxy', |
||
101 | 'metadata_cache_namespace' => 'DoctrineRDBMSORMMetadataCache', |
||
102 | 'query_cache_namespace' => 'DoctrineRDBMSORMQueryCache', |
||
103 | 'result_cache_namespace' => 'DoctrineRDBMSORMResultCache', |
||
104 | 'hydrator_cache_namespace' => 'DoctrineRDBMSORMHydratorCache', |
||
105 | 'default_repository_class' => EntityRepository::class, |
||
106 | 'second_level_cache_enable' => false, |
||
107 | ]; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | protected function wipe() |
||
114 | { |
||
115 | $this->manager = null; |
||
116 | $this->mappingDriver = null; |
||
117 | $this->metadataCacheDriver = null; |
||
118 | $this->eventManager = null; |
||
119 | $this->queryCacheDriver = null; |
||
120 | $this->resultCacheDriver = null; |
||
121 | $this->namingStrategy = null; |
||
122 | $this->quoteStrategy = null; |
||
123 | $this->SQLLogger = null; |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | * |
||
129 | * @throws \Doctrine\DBAL\DBALException |
||
130 | * @throws \Doctrine\ORM\ORMException |
||
131 | * @throws \InvalidArgumentException |
||
132 | * @throws \RuntimeException |
||
133 | * @throws \UnexpectedValueException |
||
134 | * |
||
135 | * @return EntityManager |
||
136 | */ |
||
137 | protected function buildManager() |
||
138 | { |
||
139 | $config = new Configuration(); |
||
140 | |||
141 | $this->setUpGeneralConfigurations($config); |
||
142 | $this->setUpSpecificConfigurations($config); |
||
143 | |||
144 | $eventManager = $this->getEventManager(); |
||
145 | if ($this->getEventSubscribers() !== null) { |
||
146 | /* @var array $eventSubscribers */ |
||
147 | $eventSubscribers = $this->getEventSubscribers(); |
||
148 | |||
149 | foreach ($eventSubscribers as $eventSubscriber) { |
||
150 | $eventManager->addEventSubscriber($eventSubscriber); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | $entityManager = EntityManager::create($this->getOption('connection'), $config, $eventManager); |
||
155 | |||
156 | $customMappingTypes = $this->getCustomMappingTypes(); |
||
157 | |||
158 | $platform = $entityManager->getConnection()->getDatabasePlatform(); |
||
159 | foreach ($this->getCustomTypes() as $type => $class) { |
||
160 | if (Type::hasType($type)) { |
||
161 | Type::overrideType($type, $class); |
||
162 | } else { |
||
163 | Type::addType($type, $class); |
||
164 | } |
||
165 | |||
166 | $platform->registerDoctrineTypeMapping( |
||
167 | $type, |
||
168 | array_key_exists($type, $customMappingTypes) ? $customMappingTypes[$type] : $type |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | return $entityManager; |
||
173 | } |
||
174 | |||
175 | /** |
||
176 | * Set up general manager configurations. |
||
177 | * |
||
178 | * @param Configuration $config |
||
179 | */ |
||
180 | protected function setUpGeneralConfigurations(Configuration $config) |
||
199 | |||
200 | /** |
||
201 | * Set up manager specific configurations. |
||
202 | * |
||
203 | * @param Configuration $config |
||
204 | */ |
||
205 | protected function setUpSpecificConfigurations(Configuration $config) |
||
228 | |||
229 | /** |
||
230 | * {@inheritdoc} |
||
231 | */ |
||
232 | protected function getAnnotationMappingDriver(array $paths) |
||
236 | |||
237 | /** |
||
238 | * {@inheritdoc} |
||
239 | */ |
||
240 | protected function getXmlMappingDriver(array $paths, $extension = null) |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | */ |
||
250 | protected function getYamlMappingDriver(array $paths, $extension = null) |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | * |
||
260 | * @throws \InvalidArgumentException |
||
261 | * |
||
262 | * @return RepositoryFactory|null |
||
263 | */ |
||
264 | protected function getRepositoryFactory() |
||
281 | |||
282 | /** |
||
283 | * Retrieve query cache driver. |
||
284 | * |
||
285 | * @throws \InvalidArgumentException |
||
286 | * |
||
287 | * @return CacheProvider |
||
288 | */ |
||
289 | public function getQueryCacheDriver() |
||
300 | |||
301 | /** |
||
302 | * Set query cache driver. |
||
303 | * |
||
304 | * @param CacheProvider $queryCacheDriver |
||
305 | */ |
||
306 | public function setQueryCacheDriver(CacheProvider $queryCacheDriver) |
||
310 | |||
311 | /** |
||
312 | * Retrieve result cache driver. |
||
313 | * |
||
314 | * @throws \InvalidArgumentException |
||
315 | * |
||
316 | * @return CacheProvider |
||
317 | */ |
||
318 | public function getResultCacheDriver() |
||
329 | |||
330 | /** |
||
331 | * Set result cache driver. |
||
332 | * |
||
333 | * @param CacheProvider $resultCacheDriver |
||
334 | */ |
||
335 | public function setResultCacheDriver(CacheProvider $resultCacheDriver) |
||
339 | |||
340 | /** |
||
341 | * Retrieve hydrator cache driver. |
||
342 | * |
||
343 | * @throws \InvalidArgumentException |
||
344 | * |
||
345 | * @return CacheProvider |
||
346 | */ |
||
347 | public function getHydratorCacheDriver() |
||
358 | |||
359 | /** |
||
360 | * Set hydrator cache driver. |
||
361 | * |
||
362 | * @param CacheProvider $hydratorCacheDriver |
||
363 | */ |
||
364 | public function setHydratorCacheDriver(CacheProvider $hydratorCacheDriver) |
||
368 | |||
369 | /** |
||
370 | * Get cache driver. |
||
371 | * |
||
372 | * @param string $cacheNamespace |
||
373 | * @param CacheProvider|null $cacheDriver |
||
374 | * |
||
375 | * @return CacheProvider |
||
376 | */ |
||
377 | protected function getCacheDriver($cacheNamespace, CacheProvider $cacheDriver = null) |
||
390 | |||
391 | /** |
||
392 | * Retrieve naming strategy. |
||
393 | * |
||
394 | * @return NamingStrategy |
||
395 | */ |
||
396 | protected function getNamingStrategy() |
||
410 | |||
411 | /** |
||
412 | * Retrieve quote strategy. |
||
413 | * |
||
414 | * @throws \InvalidArgumentException |
||
415 | * |
||
416 | * @return QuoteStrategy |
||
417 | */ |
||
418 | protected function getQuoteStrategy() |
||
432 | |||
433 | /** |
||
434 | * Retrieve second level cache configuration. |
||
435 | * |
||
436 | * @return CacheConfiguration|null |
||
437 | */ |
||
438 | protected function getSecondLevelCacheConfiguration() |
||
450 | |||
451 | /** |
||
452 | * Retrieve SQL logger. |
||
453 | * |
||
454 | * @return SQLLogger|null |
||
455 | */ |
||
456 | protected function getSQLLogger() |
||
468 | |||
469 | /** |
||
470 | * Retrieve custom DQL string functions. |
||
471 | * |
||
472 | * @return array |
||
473 | */ |
||
474 | protected function getCustomStringFunctions() |
||
486 | |||
487 | /** |
||
488 | * Retrieve custom DQL numeric functions. |
||
489 | * |
||
490 | * @return array |
||
491 | */ |
||
492 | protected function getCustomNumericFunctions() |
||
504 | |||
505 | /** |
||
506 | * Retrieve custom DQL date time functions. |
||
507 | * |
||
508 | * @return array |
||
509 | */ |
||
510 | protected function getCustomDateTimeFunctions() |
||
522 | |||
523 | /** |
||
524 | * Retrieve custom DBAL types. |
||
525 | * |
||
526 | * @return array |
||
527 | */ |
||
528 | protected function getCustomTypes() |
||
540 | |||
541 | /** |
||
542 | * Retrieve custom DBAL mapping types. |
||
543 | * |
||
544 | * @return array |
||
545 | */ |
||
546 | protected function getCustomMappingTypes() |
||
558 | |||
559 | /** |
||
560 | * Get custom registered filters. |
||
561 | * |
||
562 | * @return array |
||
563 | */ |
||
564 | protected function getCustomFilters() |
||
576 | |||
577 | /** |
||
578 | * {@inheritdoc} |
||
579 | * |
||
580 | * @throws \Doctrine\DBAL\DBALException |
||
581 | * @throws \Doctrine\ORM\ORMException |
||
582 | * @throws \InvalidArgumentException |
||
583 | * @throws \RuntimeException |
||
584 | * @throws \Symfony\Component\Console\Exception\InvalidArgumentException |
||
585 | * @throws \Symfony\Component\Console\Exception\LogicException |
||
586 | * @throws \UnexpectedValueException |
||
587 | * |
||
588 | * @return Command[] |
||
589 | */ |
||
590 | public function getConsoleCommands() |
||
645 | |||
646 | /** |
||
647 | * Get console helper set. |
||
648 | * |
||
649 | * @return \Symfony\Component\Console\Helper\HelperSet |
||
650 | */ |
||
651 | protected function getConsoleHelperSet() |
||
661 | } |
||
662 |