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 |
||
| 30 | class Configuration |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Array of attributes for this configuration instance. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | private $attributes = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Never autogenerate a proxy/hydrator/persistent collection and rely that |
||
| 41 | * it was generated by some process before deployment. Copied from |
||
| 42 | * \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
| 43 | * |
||
| 44 | * @var integer |
||
| 45 | */ |
||
| 46 | const AUTOGENERATE_NEVER = 0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Always generates a new proxy/hydrator/persistent collection in every request. |
||
| 50 | * |
||
| 51 | * This is only sane during development. |
||
| 52 | * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
| 53 | * |
||
| 54 | * @var integer |
||
| 55 | */ |
||
| 56 | const AUTOGENERATE_ALWAYS = 1; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Autogenerate the proxy/hydrator/persistent collection class when the file does not exist. |
||
| 60 | * |
||
| 61 | * This strategy causes a file exists call whenever any proxy/hydrator is used the |
||
| 62 | * first time in a request. Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
| 63 | * |
||
| 64 | * @var integer |
||
| 65 | */ |
||
| 66 | const AUTOGENERATE_FILE_NOT_EXISTS = 2; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Generate the proxy/hydrator/persistent collection classes using eval(). |
||
| 70 | * |
||
| 71 | * This strategy is only sane for development. |
||
| 72 | * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
| 73 | * |
||
| 74 | * @var integer |
||
| 75 | */ |
||
| 76 | const AUTOGENERATE_EVAL = 3; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Adds a namespace under a certain alias. |
||
| 80 | * |
||
| 81 | * @param string $alias |
||
| 82 | * @param string $namespace |
||
| 83 | */ |
||
| 84 | public function addDocumentNamespace($alias, $namespace) |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Resolves a registered namespace alias to the full namespace. |
||
| 91 | * |
||
| 92 | * @param string $documentNamespaceAlias |
||
| 93 | * @return string |
||
| 94 | * @throws MongoDBException |
||
| 95 | */ |
||
| 96 | public function getDocumentNamespace($documentNamespaceAlias) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Retrieves the list of registered document namespace aliases. |
||
| 107 | * |
||
| 108 | * @return array |
||
| 109 | */ |
||
| 110 | public function getDocumentNamespaces() |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Set the document alias map |
||
| 117 | * |
||
| 118 | * @param array $documentNamespaces |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function setDocumentNamespaces(array $documentNamespaces) |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Sets the cache driver implementation that is used for metadata caching. |
||
| 128 | * |
||
| 129 | * @param MappingDriver $driverImpl |
||
| 130 | * @todo Force parameter to be a Closure to ensure lazy evaluation |
||
| 131 | * (as soon as a metadata cache is in effect, the driver never needs to initialize). |
||
| 132 | */ |
||
| 133 | 1681 | public function setMetadataDriverImpl(MappingDriver $driverImpl) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Add a new default annotation driver with a correctly configured annotation reader. |
||
| 140 | * |
||
| 141 | * @param array $paths |
||
| 142 | * @return Mapping\Driver\AnnotationDriver |
||
| 143 | */ |
||
| 144 | public function newDefaultAnnotationDriver($paths = array()) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Gets the cache driver implementation that is used for the mapping metadata. |
||
| 153 | * |
||
| 154 | * @return MappingDriver |
||
| 155 | */ |
||
| 156 | 1384 | public function getMetadataDriverImpl() |
|
| 157 | { |
||
| 158 | 1384 | return $this->attributes['metadataDriverImpl'] ?? null; |
|
| 159 | 1384 | } |
|
| 160 | |||
| 161 | /** |
||
| 162 | * Gets the cache driver implementation that is used for metadata caching. |
||
| 163 | * |
||
| 164 | * @return \Doctrine\Common\Cache\Cache |
||
| 165 | */ |
||
| 166 | public function getMetadataCacheImpl() |
||
| 170 | 1638 | ||
| 171 | /** |
||
| 172 | * Sets the cache driver implementation that is used for metadata caching. |
||
| 173 | * |
||
| 174 | * @param \Doctrine\Common\Cache\Cache $cacheImpl |
||
| 175 | */ |
||
| 176 | public function setMetadataCacheImpl(Cache $cacheImpl) |
||
| 177 | { |
||
| 178 | $this->attributes['metadataCacheImpl'] = $cacheImpl; |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Sets the directory where Doctrine generates any necessary proxy class files. |
||
| 183 | * |
||
| 184 | * @param string $dir |
||
| 185 | */ |
||
| 186 | public function setProxyDir($dir) |
||
| 187 | { |
||
| 188 | 1638 | $this->attributes['proxyDir'] = $dir; |
|
| 189 | } |
||
| 190 | 1638 | ||
| 191 | 1638 | /** |
|
| 192 | * Gets the directory where Doctrine generates any necessary proxy class files. |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getProxyDir() |
||
| 197 | { |
||
| 198 | 1638 | return $this->attributes['proxyDir'] ?? null; |
|
| 199 | } |
||
| 200 | 1638 | ||
| 201 | 1638 | /** |
|
| 202 | * Gets a boolean flag that indicates whether proxy classes should always be regenerated |
||
| 203 | * during each script execution. |
||
| 204 | * |
||
| 205 | * @return boolean|integer |
||
| 206 | */ |
||
| 207 | public function getAutoGenerateProxyClasses() |
||
| 208 | { |
||
| 209 | return $this->attributes['autoGenerateProxyClasses'] ?? true; |
||
| 210 | 1638 | } |
|
| 211 | |||
| 212 | 1638 | /** |
|
| 213 | 1638 | * Sets a boolean flag that indicates whether proxy classes should always be regenerated |
|
| 214 | * during each script execution. |
||
| 215 | * |
||
| 216 | * @param boolean|int $bool Possible values are constants of Doctrine\Common\Proxy\AbstractProxyFactory |
||
| 217 | */ |
||
| 218 | public function setAutoGenerateProxyClasses($bool) |
||
| 219 | { |
||
| 220 | $this->attributes['autoGenerateProxyClasses'] = $bool; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Gets the namespace where proxy classes reside. |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getProxyNamespace() |
||
| 229 | { |
||
| 230 | return $this->attributes['proxyNamespace'] ?? null; |
||
| 231 | } |
||
| 232 | 1638 | ||
| 233 | /** |
||
| 234 | 1638 | * Sets the namespace where proxy classes reside. |
|
| 235 | 1638 | * |
|
| 236 | * @param string $ns |
||
| 237 | */ |
||
| 238 | public function setProxyNamespace($ns) |
||
| 239 | { |
||
| 240 | $this->attributes['proxyNamespace'] = $ns; |
||
| 241 | } |
||
| 242 | |||
| 243 | 1638 | /** |
|
| 244 | * Sets the directory where Doctrine generates hydrator class files. |
||
| 245 | 1638 | * |
|
| 246 | 1638 | * @param string $dir |
|
| 247 | */ |
||
| 248 | public function setHydratorDir($dir) |
||
| 249 | { |
||
| 250 | $this->attributes['hydratorDir'] = $dir; |
||
| 251 | } |
||
| 252 | |||
| 253 | 1638 | /** |
|
| 254 | * Gets the directory where Doctrine generates hydrator class files. |
||
| 255 | 1638 | * |
|
| 256 | 1638 | * @return string |
|
| 257 | */ |
||
| 258 | public function getHydratorDir() |
||
| 259 | { |
||
| 260 | return $this->attributes['hydratorDir'] ?? null; |
||
| 261 | } |
||
| 262 | |||
| 263 | 1638 | /** |
|
| 264 | * Gets a boolean flag that indicates whether hydrator classes should always be regenerated |
||
| 265 | 1638 | * during each script execution. |
|
| 266 | 1638 | * |
|
| 267 | * @return boolean|integer Possible values are defined constants |
||
| 268 | */ |
||
| 269 | public function getAutoGenerateHydratorClasses() |
||
| 270 | { |
||
| 271 | return $this->attributes['autoGenerateHydratorClasses'] ?? true; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | 1638 | * Sets a boolean flag that indicates whether hydrator classes should always be regenerated |
|
| 276 | * during each script execution. |
||
| 277 | 1638 | * |
|
| 278 | 1638 | * @param boolean|integer $bool |
|
| 279 | */ |
||
| 280 | public function setAutoGenerateHydratorClasses($bool) |
||
| 281 | { |
||
| 282 | $this->attributes['autoGenerateHydratorClasses'] = $bool; |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Gets the namespace where hydrator classes reside. |
||
| 287 | * |
||
| 288 | * @return string |
||
| 289 | */ |
||
| 290 | public function getHydratorNamespace() |
||
| 291 | { |
||
| 292 | return $this->attributes['hydratorNamespace'] ?? null; |
||
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Sets the namespace where hydrator classes reside. |
||
| 297 | 1638 | * |
|
| 298 | * @param string $ns |
||
| 299 | 1638 | */ |
|
| 300 | 1638 | public function setHydratorNamespace($ns) |
|
| 301 | { |
||
| 302 | $this->attributes['hydratorNamespace'] = $ns; |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Sets the directory where Doctrine generates persistent collection class files. |
||
| 307 | * |
||
| 308 | 1638 | * @param string $dir |
|
| 309 | */ |
||
| 310 | 1638 | public function setPersistentCollectionDir($dir) |
|
| 311 | 1638 | { |
|
| 312 | $this->attributes['persistentCollectionDir'] = $dir; |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Gets the directory where Doctrine generates persistent collection class files. |
||
| 317 | * |
||
| 318 | 1638 | * @return string |
|
| 319 | */ |
||
| 320 | 1638 | public function getPersistentCollectionDir() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Gets a integer flag that indicates how and when persistent collection |
||
| 327 | * classes should be generated. |
||
| 328 | 11 | * |
|
| 329 | * @return integer Possible values are defined constants |
||
| 330 | 11 | */ |
|
| 331 | 11 | public function getAutoGeneratePersistentCollectionClasses() |
|
| 332 | { |
||
| 333 | return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS; |
||
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Sets a integer flag that indicates how and when persistent collection |
||
| 338 | * classes should be generated. |
||
| 339 | * |
||
| 340 | 7 | * @param integer $mode Possible values are defined constants |
|
| 341 | */ |
||
| 342 | 7 | public function setAutoGeneratePersistentCollectionClasses($mode) |
|
| 343 | 7 | { |
|
| 344 | $this->attributes['autoGeneratePersistentCollectionClasses'] = $mode; |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Gets the namespace where persistent collection classes reside. |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | public function getPersistentCollectionNamespace() |
||
| 353 | { |
||
| 354 | return $this->attributes['persistentCollectionNamespace'] ?? null; |
||
| 355 | } |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Sets the namespace where persistent collection classes reside. |
||
| 359 | * |
||
| 360 | * @param string $ns |
||
| 361 | */ |
||
| 362 | 11 | public function setPersistentCollectionNamespace($ns) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Sets the default DB to use for all Documents that do not specify |
||
| 369 | * a database. |
||
| 370 | * |
||
| 371 | * @param string $defaultDB |
||
| 372 | */ |
||
| 373 | 1638 | public function setDefaultDB($defaultDB) |
|
| 377 | |||
| 378 | /** |
||
| 379 | * Gets the default DB to use for all Documents that do not specify a database. |
||
| 380 | * |
||
| 381 | * @return string $defaultDB |
||
| 382 | */ |
||
| 383 | public function getDefaultDB() |
||
| 387 | 1638 | ||
| 388 | /** |
||
| 389 | * Set the class metadata factory class name. |
||
| 390 | * |
||
| 391 | * @param string $cmfName |
||
| 392 | */ |
||
| 393 | public function setClassMetadataFactoryName($cmfName) |
||
| 394 | 1263 | { |
|
| 395 | $this->attributes['classMetadataFactoryName'] = $cmfName; |
||
| 396 | 1263 | } |
|
| 397 | 1263 | ||
| 398 | /** |
||
| 399 | * Gets the class metadata factory class name. |
||
| 400 | * |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function getClassMetadataFactoryName() |
||
| 404 | { |
||
| 405 | if ( ! isset($this->attributes['classMetadataFactoryName'])) { |
||
| 406 | $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class; |
||
| 407 | } |
||
| 408 | return $this->attributes['classMetadataFactoryName']; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Gets array of default commit options. |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | 1638 | */ |
|
| 416 | public function getDefaultCommitOptions() |
||
| 420 | 1638 | ||
| 421 | /** |
||
| 422 | * Sets array of default commit options. |
||
| 423 | * |
||
| 424 | * @param array $defaultCommitOptions |
||
| 425 | */ |
||
| 426 | public function setDefaultCommitOptions($defaultCommitOptions) |
||
| 427 | { |
||
| 428 | 558 | $this->attributes['defaultCommitOptions'] = $defaultCommitOptions; |
|
| 429 | } |
||
| 430 | 558 | ||
| 431 | 1 | /** |
|
| 432 | * Add a filter to the list of possible filters. |
||
| 433 | * |
||
| 434 | 557 | * @param string $name The name of the filter. |
|
| 435 | * @param string $className The class name of the filter. |
||
| 436 | * @param array $parameters The parameters for the filter. |
||
| 437 | */ |
||
| 438 | public function addFilter($name, $className, array $parameters = array()) |
||
| 439 | { |
||
| 440 | $this->attributes['filters'][$name] = array( |
||
| 441 | 'class' => $className, |
||
| 442 | 1 | 'parameters' => $parameters |
|
| 443 | ); |
||
| 444 | 1 | } |
|
| 445 | 1 | ||
| 446 | /** |
||
| 447 | * Gets the class name for a given filter name. |
||
| 448 | * |
||
| 449 | * @param string $name The name of the filter. |
||
| 450 | * |
||
| 451 | * @return string|null The filter class name, or null if it is undefined |
||
| 452 | */ |
||
| 453 | public function getFilterClassName($name) |
||
| 459 | |||
| 460 | 1638 | /** |
|
| 461 | * Gets the parameters for a given filter name. |
||
| 462 | * |
||
| 463 | * @param string $name The name of the filter. |
||
| 464 | * |
||
| 465 | * @return array|null The filter parameters, or null if it is undefined |
||
| 466 | */ |
||
| 467 | public function getFilterParameters($name) |
||
| 468 | { |
||
| 469 | 24 | return isset($this->attributes['filters'][$name]) |
|
| 470 | ? $this->attributes['filters'][$name]['parameters'] |
||
| 471 | 24 | : null; |
|
| 472 | 24 | } |
|
| 473 | 24 | ||
| 474 | /** |
||
| 475 | * Sets default repository class. |
||
| 476 | * |
||
| 477 | * @param string $className |
||
| 478 | * |
||
| 479 | * @return void |
||
| 480 | * |
||
| 481 | * @throws MongoDBException If not is a ObjectRepository |
||
| 482 | */ |
||
| 483 | 23 | public function setDefaultRepositoryClassName($className) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Get default repository class. |
||
| 496 | * |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public function getDefaultRepositoryClassName() |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Set the document repository factory. |
||
| 506 | * |
||
| 507 | * @param RepositoryFactory $repositoryFactory |
||
| 508 | */ |
||
| 509 | public function setRepositoryFactory(RepositoryFactory $repositoryFactory) |
||
| 513 | |||
| 514 | /** |
||
| 515 | 321 | * Get the document repository factory. |
|
| 516 | * |
||
| 517 | 321 | * @return RepositoryFactory |
|
| 518 | */ |
||
| 519 | 321 | public function getRepositoryFactory() |
|
| 523 | |||
| 524 | /** |
||
| 525 | * Set the persistent collection factory. |
||
| 526 | * |
||
| 527 | 2 | * @param PersistentCollectionFactory $persistentCollectionFactory |
|
| 528 | */ |
||
| 529 | 2 | public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Get the persistent collection factory. |
||
| 536 | * |
||
| 537 | 1638 | * @return DefaultPersistentCollectionFactory |
|
| 538 | */ |
||
| 539 | 1638 | public function getPersistentCollectionFactory() |
|
| 546 | |||
| 547 | /** |
||
| 548 | * Set the persistent collection generator. |
||
| 549 | * |
||
| 550 | * @param PersistentCollectionGenerator $persistentCollectionGenerator |
||
| 551 | */ |
||
| 552 | public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Get the persistent collection generator. |
||
| 559 | 392 | * |
|
| 560 | * @return DefaultPersistentCollectionGenerator |
||
| 561 | 392 | */ |
|
| 562 | 392 | public function getPersistentCollectionGenerator() |
|
| 572 | } |
||
| 573 |