Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
42 | class Configuration extends \Doctrine\DBAL\Configuration |
||
43 | { |
||
44 | /** |
||
45 | * @var ProxyManagerConfiguration|null |
||
46 | */ |
||
47 | private $proxyManagerConfiguration; |
||
48 | |||
49 | /** |
||
50 | * Sets the directory where Doctrine generates any necessary proxy class files. |
||
51 | * |
||
52 | * @param string $dir |
||
53 | * |
||
54 | * @return void |
||
55 | */ |
||
56 | public function setProxyDir($dir) |
||
57 | { |
||
58 | $this->attributes['proxyDir'] = $dir; |
||
|
|||
59 | |||
60 | $this->getProxyManagerConfiguration()->setProxiesTargetDir($dir); |
||
61 | $this->setAutoGenerateProxyClasses(ProxyFactory::AUTOGENERATE_FILE_NOT_EXISTS); |
||
62 | 2296 | } |
|
63 | |||
64 | 2296 | /** |
|
65 | 2296 | * Gets the directory where Doctrine generates any necessary proxy class files. |
|
66 | * |
||
67 | * @return string|null |
||
68 | * |
||
69 | * @deprecated please do not use this anymore |
||
70 | */ |
||
71 | public function getProxyDir() |
||
72 | 2293 | { |
|
73 | return isset($this->attributes['proxyDir']) |
||
74 | 2293 | ? $this->attributes['proxyDir'] |
|
75 | 2293 | : null; |
|
76 | 2293 | } |
|
77 | |||
78 | /** |
||
79 | * Gets the strategy for automatically generating proxy classes. |
||
80 | * |
||
81 | * @return int Possible values are constants of Doctrine\ORM\Proxy\Factory\ProxyFactory. |
||
82 | * |
||
83 | * @deprecated please do not use this anymore |
||
84 | 2295 | */ |
|
85 | public function getAutoGenerateProxyClasses() |
||
91 | |||
92 | /** |
||
93 | * Sets the strategy for automatically generating proxy classes. |
||
94 | * |
||
95 | * @param boolean|int $autoGenerate Possible values are constants of Doctrine\ORM\Proxy\Factory\ProxyFactory. |
||
96 | * True is converted to AUTOGENERATE_ALWAYS, false to AUTOGENERATE_NEVER. |
||
97 | * |
||
98 | * @return void |
||
99 | 14 | */ |
|
100 | public function setAutoGenerateProxyClasses($autoGenerate) |
||
122 | |||
123 | 2296 | /** |
|
124 | * Gets the namespace where proxy classes reside. |
||
125 | 2296 | * |
|
126 | 2296 | * @return string|null |
|
127 | * |
||
128 | * @deprecated please do not use this anymore |
||
129 | */ |
||
130 | public function getProxyNamespace() |
||
136 | |||
137 | /** |
||
138 | 2295 | * Sets the namespace where proxy classes reside. |
|
139 | * |
||
140 | 2295 | * @param string $ns |
|
141 | 2295 | * |
|
142 | * @return void |
||
143 | */ |
||
144 | public function setProxyNamespace($ns) |
||
150 | |||
151 | /** |
||
152 | 2275 | * Sets the cache driver implementation that is used for metadata caching. |
|
153 | * |
||
154 | 2275 | * @param MappingDriver $driverImpl |
|
155 | * |
||
156 | 2275 | * @return void |
|
157 | * |
||
158 | 2275 | * @todo Force parameter to be a Closure to ensure lazy evaluation |
|
159 | 2275 | * (as soon as a metadata cache is in effect, the driver never needs to initialize). |
|
160 | 2275 | */ |
|
161 | public function setMetadataDriverImpl(MappingDriver $driverImpl) |
||
165 | 1 | ||
166 | 1 | /** |
|
167 | 1 | * Adds a new default annotation driver with a correctly configured annotation reader. |
|
168 | * |
||
169 | * @param array $paths |
||
170 | * |
||
171 | * @return AnnotationDriver |
||
172 | */ |
||
173 | View Code Duplication | public function newDefaultAnnotationDriver($paths = []) |
|
181 | 8 | ||
182 | 8 | /** |
|
183 | * Adds a namespace under a certain alias. |
||
184 | * |
||
185 | * @param string $alias |
||
186 | * @param string $namespace |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | public function addEntityNamespace($alias, $namespace) |
||
194 | |||
195 | 13 | /** |
|
196 | 1 | * Resolves a registered namespace alias to the full namespace. |
|
197 | * |
||
198 | * @param string $entityNamespaceAlias |
||
199 | 13 | * |
|
200 | * @return string |
||
201 | * |
||
202 | * @throws ORMException |
||
203 | */ |
||
204 | public function getEntityNamespace($entityNamespaceAlias) |
||
212 | 92 | ||
213 | /** |
||
214 | * Sets the entity alias map. |
||
215 | * |
||
216 | * @param array $entityNamespaces |
||
217 | * |
||
218 | * @return void |
||
219 | 1 | */ |
|
220 | public function setEntityNamespaces(array $entityNamespaces) |
||
224 | |||
225 | /** |
||
226 | * Retrieves the list of registered entity namespace aliases. |
||
227 | * |
||
228 | * @return array |
||
229 | */ |
||
230 | public function getEntityNamespaces() |
||
234 | 1447 | ||
235 | 1447 | /** |
|
236 | * Gets the cache driver implementation that is used for the mapping metadata. |
||
237 | * |
||
238 | * @return MappingDriver|null |
||
239 | * |
||
240 | * @throws ORMException |
||
241 | */ |
||
242 | public function getMetadataDriverImpl() |
||
248 | |||
249 | /** |
||
250 | * Gets the cache driver implementation that is used for the query cache (SQL cache). |
||
251 | * |
||
252 | * @return \Doctrine\Common\Cache\Cache|null |
||
253 | */ |
||
254 | public function getQueryCacheImpl() |
||
260 | 2242 | ||
261 | /** |
||
262 | * Sets the cache driver implementation that is used for the query cache (SQL cache). |
||
263 | * |
||
264 | * @param \Doctrine\Common\Cache\Cache $cacheImpl |
||
265 | * |
||
266 | * @return void |
||
267 | 1 | */ |
|
268 | public function setQueryCacheImpl(CacheDriver $cacheImpl) |
||
272 | |||
273 | /** |
||
274 | * Gets the cache driver implementation that is used for the hydration cache (SQL cache). |
||
275 | * |
||
276 | * @return \Doctrine\Common\Cache\Cache|null |
||
277 | */ |
||
278 | public function getHydrationCacheImpl() |
||
284 | 1 | ||
285 | /** |
||
286 | * Sets the cache driver implementation that is used for the hydration cache (SQL cache). |
||
287 | * |
||
288 | * @param \Doctrine\Common\Cache\Cache $cacheImpl |
||
289 | * |
||
290 | * @return void |
||
291 | 2299 | */ |
|
292 | public function setHydrationCacheImpl(CacheDriver $cacheImpl) |
||
296 | |||
297 | /** |
||
298 | * Gets the cache driver implementation that is used for metadata caching. |
||
299 | * |
||
300 | * @return \Doctrine\Common\Cache\Cache|null |
||
301 | */ |
||
302 | public function getMetadataCacheImpl() |
||
308 | 2242 | ||
309 | /** |
||
310 | * Sets the cache driver implementation that is used for metadata caching. |
||
311 | * |
||
312 | * @param \Doctrine\Common\Cache\Cache $cacheImpl |
||
313 | * |
||
314 | * @return void |
||
315 | */ |
||
316 | public function setMetadataCacheImpl(CacheDriver $cacheImpl) |
||
320 | 1 | ||
321 | 1 | /** |
|
322 | * Adds a named DQL query to the configuration. |
||
323 | * |
||
324 | * @param string $name The name of the query. |
||
325 | * @param string $dql The DQL query string. |
||
326 | * |
||
327 | * @return void |
||
328 | */ |
||
329 | public function addNamedQuery($name, $dql) |
||
333 | |||
334 | 2 | /** |
|
335 | 2 | * Gets a previously registered named DQL query. |
|
336 | * |
||
337 | * @param string $name The name of the query. |
||
338 | 1 | * |
|
339 | * @return string The DQL query. |
||
340 | * |
||
341 | * @throws ORMException |
||
342 | */ |
||
343 | public function getNamedQuery($name) |
||
351 | |||
352 | 1 | /** |
|
353 | 1 | * Adds a named native query to the configuration. |
|
354 | * |
||
355 | * @param string $name The name of the query. |
||
356 | * @param string $sql The native SQL query string. |
||
357 | * @param Query\ResultSetMapping $rsm The ResultSetMapping used for the results of the SQL query. |
||
358 | * |
||
359 | * @return void |
||
360 | */ |
||
361 | public function addNamedNativeQuery($name, $sql, Query\ResultSetMapping $rsm) |
||
365 | 1 | ||
366 | /** |
||
367 | 1 | * Gets the components of a previously registered named native query. |
|
368 | * |
||
369 | * @param string $name The name of the query. |
||
370 | * |
||
371 | 1 | * @return array A tuple with the first element being the SQL string and the second |
|
372 | * element being the ResultSetMapping. |
||
373 | * |
||
374 | * @throws ORMException |
||
375 | */ |
||
376 | public function getNamedNativeQuery($name) |
||
384 | |||
385 | 8 | /** |
|
386 | * Ensures that this Configuration instance contains settings that are |
||
387 | 8 | * suitable for a production environment. |
|
388 | 1 | * |
|
389 | * @return void |
||
390 | * |
||
391 | 7 | * @throws ORMException If a configuration setting has a value that is not |
|
392 | 1 | * suitable for a production environment. |
|
393 | */ |
||
394 | public function ensureProductionSettings() |
||
420 | |||
421 | /** |
||
422 | * Registers a custom DQL function that produces a string value. |
||
423 | * Such a function can then be used in any DQL statement in any place where string |
||
424 | 3 | * functions are allowed. |
|
425 | * |
||
426 | 3 | * DQL function names are case-insensitive. |
|
427 | 1 | * |
|
428 | * @param string $name Function name. |
||
429 | * @param string|callable $className Class name or a callable that returns the function. |
||
430 | 3 | * |
|
431 | 3 | * @return void |
|
432 | */ |
||
433 | public function addCustomStringFunction($name, $className) |
||
437 | |||
438 | /** |
||
439 | * Gets the implementation class name of a registered custom string DQL function. |
||
440 | 4 | * |
|
441 | * @param string $name |
||
442 | 4 | * |
|
443 | * @return string|null |
||
444 | 4 | */ |
|
445 | 3 | View Code Duplication | public function getCustomStringFunction($name) |
453 | |||
454 | /** |
||
455 | * Sets a map of custom DQL string functions. |
||
456 | * |
||
457 | * Keys must be function names and values the FQCN of the implementing class. |
||
458 | * The function names will be case-insensitive in DQL. |
||
459 | * |
||
460 | * Any previously added string functions are discarded. |
||
461 | 1 | * |
|
462 | * @param array $functions The map of custom DQL string functions. |
||
463 | 1 | * |
|
464 | 1 | * @return void |
|
465 | */ |
||
466 | 1 | public function setCustomStringFunctions(array $functions) |
|
472 | |||
473 | /** |
||
474 | * Registers a custom DQL function that produces a numeric value. |
||
475 | * Such a function can then be used in any DQL statement in any place where numeric |
||
476 | * functions are allowed. |
||
477 | * |
||
478 | * DQL function names are case-insensitive. |
||
479 | * |
||
480 | * @param string $name Function name. |
||
481 | * @param string|callable $className Class name or a callable that returns the function. |
||
482 | 3 | * |
|
483 | * @return void |
||
484 | 3 | */ |
|
485 | 1 | public function addCustomNumericFunction($name, $className) |
|
489 | 3 | ||
490 | /** |
||
491 | * Gets the implementation class name of a registered custom numeric DQL function. |
||
492 | * |
||
493 | * @param string $name |
||
494 | * |
||
495 | * @return string|null |
||
496 | */ |
||
497 | View Code Duplication | public function getCustomNumericFunction($name) |
|
505 | |||
506 | /** |
||
507 | * Sets a map of custom DQL numeric functions. |
||
508 | * |
||
509 | * Keys must be function names and values the FQCN of the implementing class. |
||
510 | * The function names will be case-insensitive in DQL. |
||
511 | * |
||
512 | * Any previously added numeric functions are discarded. |
||
513 | * |
||
514 | * @param array $functions The map of custom DQL numeric functions. |
||
515 | * |
||
516 | * @return void |
||
517 | */ |
||
518 | public function setCustomNumericFunctions(array $functions) |
||
524 | 1 | ||
525 | /** |
||
526 | * Registers a custom DQL function that produces a date/time value. |
||
527 | * Such a function can then be used in any DQL statement in any place where date/time |
||
528 | * functions are allowed. |
||
529 | * |
||
530 | * DQL function names are case-insensitive. |
||
531 | * |
||
532 | * @param string $name Function name. |
||
533 | * @param string|callable $className Class name or a callable that returns the function. |
||
534 | * |
||
535 | * @return void |
||
536 | */ |
||
537 | public function addCustomDatetimeFunction($name, $className) |
||
541 | |||
542 | 1 | /** |
|
543 | 1 | * Gets the implementation class name of a registered custom date/time DQL function. |
|
544 | * |
||
545 | * @param string $name |
||
546 | 1 | * |
|
547 | 1 | * @return string|null |
|
548 | */ |
||
549 | View Code Duplication | public function getCustomDatetimeFunction($name) |
|
557 | |||
558 | 1 | /** |
|
559 | * Sets a map of custom DQL date/time functions. |
||
560 | 1 | * |
|
561 | 1 | * Keys must be function names and values the FQCN of the implementing class. |
|
562 | 1 | * The function names will be case-insensitive in DQL. |
|
563 | * |
||
564 | * Any previously added date/time functions are discarded. |
||
565 | * |
||
566 | * @param array $functions The map of custom DQL date/time functions. |
||
567 | * |
||
568 | * @return void |
||
569 | */ |
||
570 | public function setCustomDatetimeFunctions(array $functions) |
||
576 | |||
577 | 1 | /** |
|
578 | * Sets the custom hydrator modes in one pass. |
||
579 | 1 | * |
|
580 | 1 | * @param array $modes An array of ($modeName => $hydrator). |
|
581 | * |
||
582 | 1 | * @return void |
|
583 | */ |
||
584 | public function setCustomHydrationModes($modes) |
||
592 | |||
593 | 1 | /** |
|
594 | * Gets the hydrator class for the given hydration mode name. |
||
595 | 1 | * |
|
596 | 1 | * @param string $modeName The hydration mode name. |
|
597 | * |
||
598 | 1 | * @return string|null The hydrator class name. |
|
599 | */ |
||
600 | public function getCustomHydrationMode($modeName) |
||
606 | |||
607 | 3 | /** |
|
608 | * Adds a custom hydration mode. |
||
609 | 3 | * |
|
610 | 3 | * @param string $modeName The hydration mode name. |
|
611 | 3 | * @param string $hydrator The hydrator class name. |
|
612 | * |
||
613 | * @return void |
||
614 | */ |
||
615 | public function addCustomHydrationMode($modeName, $hydrator) |
||
619 | |||
620 | /** |
||
621 | * Sets a class metadata factory. |
||
622 | 3 | * |
|
623 | * @param string $cmfName |
||
624 | 3 | * |
|
625 | 3 | * @return void |
|
626 | */ |
||
627 | public function setClassMetadataFactoryName($cmfName) |
||
631 | |||
632 | /** |
||
633 | * @return string |
||
634 | 1 | */ |
|
635 | public function getClassMetadataFactoryName() |
||
643 | |||
644 | 2291 | /** |
|
645 | 2291 | * Adds a filter to the list of possible filters. |
|
646 | * |
||
647 | * @param string $name The name of the filter. |
||
648 | 2291 | * @param string $className The class name of the filter. |
|
649 | */ |
||
650 | public function addFilter($name, $className) |
||
654 | |||
655 | /** |
||
656 | * Gets the class name for a given filter name. |
||
657 | 46 | * |
|
658 | * @param string $name The name of the filter. |
||
659 | 46 | * |
|
660 | 46 | * @return string The class name of the filter, or null if it is not |
|
661 | * defined. |
||
662 | */ |
||
663 | public function getFilterClassName($name) |
||
669 | |||
670 | 45 | /** |
|
671 | * Sets default repository class. |
||
672 | 45 | * |
|
673 | 45 | * @since 2.2 |
|
674 | 45 | * |
|
675 | * @param string $className |
||
676 | * |
||
677 | * @return void |
||
678 | * |
||
679 | * @throws ORMException If not is a \Doctrine\Common\Persistence\ObjectRepository |
||
680 | */ |
||
681 | public function setDefaultRepositoryClassName($className) |
||
691 | |||
692 | 3 | /** |
|
693 | 1 | * Get default repository class. |
|
694 | * |
||
695 | * @since 2.2 |
||
696 | 2 | * |
|
697 | 2 | * @return string |
|
698 | */ |
||
699 | public function getDefaultRepositoryClassName() |
||
705 | |||
706 | 139 | /** |
|
707 | * Sets naming strategy. |
||
708 | 139 | * |
|
709 | 2 | * @since 2.3 |
|
710 | 139 | * |
|
711 | * @param NamingStrategy $namingStrategy |
||
712 | * |
||
713 | * @return void |
||
714 | */ |
||
715 | public function setNamingStrategy(NamingStrategy $namingStrategy) |
||
719 | |||
720 | /** |
||
721 | * Gets naming strategy.. |
||
722 | 5 | * |
|
723 | * @since 2.3 |
||
724 | 5 | * |
|
725 | 5 | * @return NamingStrategy |
|
726 | */ |
||
727 | public function getNamingStrategy() |
||
735 | |||
736 | 391 | /** |
|
737 | 391 | * Set the entity listener resolver. |
|
738 | * |
||
739 | * @since 2.4 |
||
740 | 391 | * @param \Doctrine\ORM\Mapping\EntityListenerResolver $resolver |
|
741 | */ |
||
742 | public function setEntityListenerResolver(EntityListenerResolver $resolver) |
||
746 | |||
747 | /** |
||
748 | * Get the entity listener resolver. |
||
749 | * |
||
750 | * @since 2.4 |
||
751 | * @return \Doctrine\ORM\Mapping\EntityListenerResolver |
||
752 | 1 | */ |
|
753 | public function getEntityListenerResolver() |
||
761 | |||
762 | /** |
||
763 | * Set the entity repository factory. |
||
764 | 1602 | * |
|
765 | * @since 2.4 |
||
766 | 1602 | * @param \Doctrine\ORM\Repository\RepositoryFactory $repositoryFactory |
|
767 | 1602 | */ |
|
768 | public function setRepositoryFactory(RepositoryFactory $repositoryFactory) |
||
772 | |||
773 | /** |
||
774 | * Get the entity repository factory. |
||
775 | * |
||
776 | * @since 2.4 |
||
777 | * @return \Doctrine\ORM\Repository\RepositoryFactory |
||
778 | */ |
||
779 | 1 | public function getRepositoryFactory() |
|
785 | |||
786 | /** |
||
787 | * @since 2.5 |
||
788 | * |
||
789 | * @return boolean |
||
790 | 2291 | */ |
|
791 | public function isSecondLevelCacheEnabled() |
||
797 | |||
798 | /** |
||
799 | * @since 2.5 |
||
800 | * |
||
801 | * @param boolean $flag |
||
802 | * |
||
803 | * @return void |
||
804 | */ |
||
805 | public function setSecondLevelCacheEnabled($flag = true) |
||
809 | |||
810 | /** |
||
811 | * @since 2.5 |
||
812 | * |
||
813 | * @param \Doctrine\ORM\Cache\CacheConfiguration $cacheConfig |
||
814 | * |
||
815 | * @return void |
||
816 | 2290 | */ |
|
817 | public function setSecondLevelCacheConfiguration(CacheConfiguration $cacheConfig) |
||
821 | |||
822 | /** |
||
823 | * @since 2.5 |
||
824 | * |
||
825 | * @return \Doctrine\ORM\Cache\CacheConfiguration|null |
||
826 | */ |
||
827 | public function getSecondLevelCacheConfiguration() |
||
837 | |||
838 | /** |
||
839 | * Returns query hints, which will be applied to every query in application |
||
840 | * |
||
841 | * @since 2.5 |
||
842 | 277 | * |
|
843 | * @return array |
||
844 | 277 | */ |
|
845 | 277 | public function getDefaultQueryHints() |
|
849 | |||
850 | /** |
||
851 | * Sets array of query hints, which will be applied to every query in application |
||
852 | * |
||
853 | * @since 2.5 |
||
854 | 278 | * |
|
855 | * @param array $defaultQueryHints |
||
856 | 278 | */ |
|
857 | 278 | public function setDefaultQueryHints(array $defaultQueryHints) |
|
861 | |||
862 | /** |
||
863 | * Gets the value of a default query hint. If the hint name is not recognized, FALSE is returned. |
||
864 | 278 | * |
|
865 | * @since 2.5 |
||
866 | 278 | * |
|
867 | * @param string $name The name of the hint. |
||
868 | * |
||
869 | * @return mixed The value of the hint or FALSE, if the hint name is not recognized. |
||
870 | 278 | */ |
|
871 | 278 | public function getDefaultQueryHint($name) |
|
877 | |||
878 | /** |
||
879 | * Sets a default query hint. If the hint name is not recognized, it is silently ignored. |
||
880 | * |
||
881 | * @since 2.5 |
||
882 | 945 | * |
|
883 | * @param string $name The name of the hint. |
||
884 | 945 | * @param mixed $value The value of the hint. |
|
885 | */ |
||
886 | public function setDefaultQueryHint($name, $value) |
||
890 | |||
891 | public function buildGhostObjectFactory() : LazyLoadingGhostFactory |
||
895 | |||
896 | 1 | public function getProxyManagerConfiguration() : ProxyManagerConfiguration |
|
901 | } |
||
902 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.