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 |
||
35 | class Configuration |
||
36 | { |
||
37 | /** |
||
38 | * Array of attributes for this configuration instance. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | private $attributes = []; |
||
43 | |||
44 | /** |
||
45 | * Never autogenerate a proxy/hydrator/persistent collection and rely that |
||
46 | * it was generated by some process before deployment. Copied from |
||
47 | * \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
48 | * |
||
49 | * @var integer |
||
50 | */ |
||
51 | public const AUTOGENERATE_NEVER = 0; |
||
52 | |||
53 | /** |
||
54 | * Always generates a new proxy/hydrator/persistent collection in every request. |
||
55 | * |
||
56 | * This is only sane during development. |
||
57 | * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
58 | * |
||
59 | * @var integer |
||
60 | */ |
||
61 | public const AUTOGENERATE_ALWAYS = 1; |
||
62 | |||
63 | /** |
||
64 | * Autogenerate the proxy/hydrator/persistent collection class when the file does not exist. |
||
65 | * |
||
66 | * This strategy causes a file exists call whenever any proxy/hydrator is used the |
||
67 | * first time in a request. Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
68 | * |
||
69 | * @var integer |
||
70 | */ |
||
71 | public const AUTOGENERATE_FILE_NOT_EXISTS = 2; |
||
72 | |||
73 | /** |
||
74 | * Generate the proxy/hydrator/persistent collection classes using eval(). |
||
75 | * |
||
76 | * This strategy is only sane for development. |
||
77 | * Copied from \Doctrine\Common\Proxy\AbstractProxyFactory. |
||
78 | * |
||
79 | * @var integer |
||
80 | */ |
||
81 | public const AUTOGENERATE_EVAL = 3; |
||
82 | |||
83 | /** |
||
84 | * Adds a namespace under a certain alias. |
||
85 | */ |
||
86 | public function addDocumentNamespace(string $alias, string $namespace): void |
||
87 | { |
||
88 | $this->attributes['documentNamespaces'][$alias] = $namespace; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Resolves a registered namespace alias to the full namespace. |
||
93 | * |
||
94 | * @throws MongoDBException |
||
95 | */ |
||
96 | public function getDocumentNamespace(string $documentNamespaceAlias): string |
||
97 | { |
||
98 | if (! isset($this->attributes['documentNamespaces'][$documentNamespaceAlias])) { |
||
99 | throw MongoDBException::unknownDocumentNamespace($documentNamespaceAlias); |
||
100 | } |
||
101 | |||
102 | return trim($this->attributes['documentNamespaces'][$documentNamespaceAlias], '\\'); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Retrieves the list of registered document namespace aliases. |
||
107 | */ |
||
108 | public function getDocumentNamespaces(): array |
||
109 | { |
||
110 | return $this->attributes['documentNamespaces']; |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Set the document alias map |
||
115 | */ |
||
116 | public function setDocumentNamespaces(array $documentNamespaces): void |
||
117 | { |
||
118 | $this->attributes['documentNamespaces'] = $documentNamespaces; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * Sets the cache driver implementation that is used for metadata caching. |
||
123 | * |
||
124 | 1627 | * @todo Force parameter to be a Closure to ensure lazy evaluation |
|
125 | * (as soon as a metadata cache is in effect, the driver never needs to initialize). |
||
126 | 1627 | */ |
|
127 | 1627 | public function setMetadataDriverImpl(MappingDriver $driverImpl): void |
|
128 | { |
||
129 | $this->attributes['metadataDriverImpl'] = $driverImpl; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Add a new default annotation driver with a correctly configured annotation reader. |
||
134 | */ |
||
135 | public function newDefaultAnnotationDriver(array $paths = []): AnnotationDriver |
||
136 | { |
||
137 | $reader = new AnnotationReader(); |
||
138 | |||
139 | return new AnnotationDriver($reader, (array) $paths); |
||
140 | } |
||
141 | |||
142 | 1369 | /** |
|
143 | * Gets the cache driver implementation that is used for the mapping metadata. |
||
144 | 1369 | */ |
|
145 | public function getMetadataDriverImpl(): ?MappingDriver |
||
146 | { |
||
147 | 1584 | return $this->attributes['metadataDriverImpl'] ?? null; |
|
148 | } |
||
149 | 1584 | ||
150 | public function getMetadataCacheImpl(): ?Cache |
||
151 | { |
||
152 | return $this->attributes['metadataCacheImpl'] ?? null; |
||
153 | } |
||
154 | |||
155 | public function setMetadataCacheImpl(Cache $cacheImpl): void |
||
156 | { |
||
157 | $this->attributes['metadataCacheImpl'] = $cacheImpl; |
||
158 | } |
||
159 | |||
160 | 1584 | /** |
|
161 | * Sets the directory where Doctrine generates any necessary proxy class files. |
||
162 | 1584 | */ |
|
163 | 1584 | public function setProxyDir(string $dir): void |
|
164 | { |
||
165 | $this->attributes['proxyDir'] = $dir; |
||
166 | } |
||
167 | |||
168 | 1584 | /** |
|
169 | * Gets the directory where Doctrine generates any necessary proxy class files. |
||
170 | 1584 | */ |
|
171 | public function getProxyDir(): ?string |
||
172 | { |
||
173 | return $this->attributes['proxyDir'] ?? null; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | 1584 | * Gets an int flag that indicates whether proxy classes should always be regenerated |
|
178 | * during each script execution. |
||
179 | 1584 | */ |
|
180 | public function getAutoGenerateProxyClasses(): int |
||
181 | { |
||
182 | return $this->attributes['autoGenerateProxyClasses'] ?? self::AUTOGENERATE_ALWAYS; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Sets an int flag that indicates whether proxy classes should always be regenerated |
||
187 | * during each script execution. |
||
188 | */ |
||
189 | public function setAutoGenerateProxyClasses(int $mode): void |
||
190 | { |
||
191 | 1584 | $this->attributes['autoGenerateProxyClasses'] = $mode; |
|
192 | } |
||
193 | 1584 | ||
194 | public function getProxyNamespace(): ?string |
||
195 | { |
||
196 | 1584 | return $this->attributes['proxyNamespace'] ?? null; |
|
197 | } |
||
198 | 1584 | ||
199 | 1584 | public function setProxyNamespace(string $ns): void |
|
200 | { |
||
201 | 1584 | $this->attributes['proxyNamespace'] = $ns; |
|
202 | } |
||
203 | 1584 | ||
204 | 1584 | public function setHydratorDir(string $dir): void |
|
205 | { |
||
206 | 1584 | $this->attributes['hydratorDir'] = $dir; |
|
207 | } |
||
208 | 1584 | ||
209 | public function getHydratorDir(): ?string |
||
210 | { |
||
211 | return $this->attributes['hydratorDir'] ?? null; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | 1584 | * Gets an int flag that indicates whether hydrator classes should always be regenerated |
|
216 | * during each script execution. |
||
217 | 1584 | */ |
|
218 | public function getAutoGenerateHydratorClasses(): int |
||
219 | { |
||
220 | return $this->attributes['autoGenerateHydratorClasses'] ?? self::AUTOGENERATE_ALWAYS; |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Sets an int flag that indicates whether hydrator classes should always be regenerated |
||
225 | * during each script execution. |
||
226 | */ |
||
227 | public function setAutoGenerateHydratorClasses(int $mode): void |
||
228 | { |
||
229 | 1584 | $this->attributes['autoGenerateHydratorClasses'] = $mode; |
|
230 | } |
||
231 | 1584 | ||
232 | public function getHydratorNamespace(): ?string |
||
233 | { |
||
234 | 1584 | return $this->attributes['hydratorNamespace'] ?? null; |
|
235 | } |
||
236 | 1584 | ||
237 | 1584 | public function setHydratorNamespace(string $ns): void |
|
238 | { |
||
239 | 1584 | $this->attributes['hydratorNamespace'] = $ns; |
|
240 | } |
||
241 | 1584 | ||
242 | 1584 | public function setPersistentCollectionDir(string $dir): void |
|
243 | { |
||
244 | 11 | $this->attributes['persistentCollectionDir'] = $dir; |
|
245 | } |
||
246 | 11 | ||
247 | public function getPersistentCollectionDir(): ?string |
||
248 | { |
||
249 | return $this->attributes['persistentCollectionDir'] ?? null; |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | 7 | * Gets a integer flag that indicates how and when persistent collection |
|
254 | * classes should be generated. |
||
255 | 7 | */ |
|
256 | public function getAutoGeneratePersistentCollectionClasses(): int |
||
257 | { |
||
258 | return $this->attributes['autoGeneratePersistentCollectionClasses'] ?? self::AUTOGENERATE_ALWAYS; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Sets a integer flag that indicates how and when persistent collection |
||
263 | * classes should be generated. |
||
264 | */ |
||
265 | public function setAutoGeneratePersistentCollectionClasses(int $mode): void |
||
266 | { |
||
267 | 11 | $this->attributes['autoGeneratePersistentCollectionClasses'] = $mode; |
|
268 | } |
||
269 | 11 | ||
270 | public function getPersistentCollectionNamespace(): ?string |
||
271 | { |
||
272 | 1584 | return $this->attributes['persistentCollectionNamespace'] ?? null; |
|
273 | } |
||
274 | 1584 | ||
275 | 1584 | public function setPersistentCollectionNamespace(string $ns): void |
|
276 | { |
||
277 | $this->attributes['persistentCollectionNamespace'] = $ns; |
||
278 | } |
||
279 | |||
280 | /** |
||
281 | 1584 | * Sets the default DB to use for all Documents that do not specify |
|
282 | * a database. |
||
283 | 1584 | */ |
|
284 | 1584 | public function setDefaultDB(string $defaultDB): void |
|
285 | { |
||
286 | $this->attributes['defaultDB'] = $defaultDB; |
||
287 | } |
||
288 | |||
289 | 1252 | /** |
|
290 | * Gets the default DB to use for all Documents that do not specify a database. |
||
291 | 1252 | */ |
|
292 | public function getDefaultDB(): ?string |
||
293 | { |
||
294 | return $this->attributes['defaultDB'] ?? null; |
||
295 | } |
||
296 | |||
297 | public function setClassMetadataFactoryName(string $cmfName): void |
||
298 | { |
||
299 | 1584 | $this->attributes['classMetadataFactoryName'] = $cmfName; |
|
300 | } |
||
301 | 1584 | ||
302 | 1584 | public function getClassMetadataFactoryName(): string |
|
303 | { |
||
304 | 1584 | if (! isset($this->attributes['classMetadataFactoryName'])) { |
|
305 | $this->attributes['classMetadataFactoryName'] = ClassMetadataFactory::class; |
||
306 | } |
||
307 | 551 | return $this->attributes['classMetadataFactoryName']; |
|
308 | } |
||
309 | 551 | ||
310 | public function getDefaultCommitOptions(): array |
||
311 | { |
||
312 | 1 | return $this->attributes['defaultCommitOptions'] ?? ['w' => 1]; |
|
313 | } |
||
314 | 1 | ||
315 | 1 | public function setDefaultCommitOptions(array $defaultCommitOptions): void |
|
316 | { |
||
317 | $this->attributes['defaultCommitOptions'] = $defaultCommitOptions; |
||
318 | } |
||
319 | |||
320 | 1584 | /** |
|
321 | * Add a filter to the list of possible filters. |
||
322 | 1584 | */ |
|
323 | 1584 | public function addFilter(string $name, string $className, array $parameters = []): void |
|
324 | 1584 | { |
|
325 | $this->attributes['filters'][$name] = [ |
||
326 | 1584 | 'class' => $className, |
|
327 | 'parameters' => $parameters, |
||
328 | 24 | ]; |
|
329 | } |
||
330 | 24 | ||
331 | 24 | public function getFilterClassName(string $name): ?string |
|
332 | 24 | { |
|
333 | return isset($this->attributes['filters'][$name]) |
||
334 | ? $this->attributes['filters'][$name]['class'] |
||
335 | 23 | : null; |
|
336 | } |
||
337 | 23 | ||
338 | 23 | public function getFilterParameters(string $name): ?array |
|
339 | 23 | { |
|
340 | return isset($this->attributes['filters'][$name]) |
||
341 | ? $this->attributes['filters'][$name]['parameters'] |
||
342 | : null; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @throws MongoDBException If not is a ObjectRepository. |
||
347 | */ |
||
348 | public function setDefaultDocumentRepositoryClassName(string $className): void |
||
349 | { |
||
350 | $reflectionClass = new \ReflectionClass($className); |
||
351 | |||
352 | if (! $reflectionClass->implementsInterface(ObjectRepository::class)) { |
||
353 | throw MongoDBException::invalidDocumentRepository($className); |
||
354 | } |
||
355 | |||
356 | 319 | $this->attributes['defaultDocumentRepositoryClassName'] = $className; |
|
357 | } |
||
358 | 319 | ||
359 | public function getDefaultDocumentRepositoryClassName(): string |
||
360 | { |
||
361 | 2 | return $this->attributes['defaultDocumentRepositoryClassName'] ?? DocumentRepository::class; |
|
362 | } |
||
363 | 2 | ||
364 | 2 | /** |
|
365 | * @throws MongoDBException If the class does not implement the GridFSRepository interface. |
||
366 | 1584 | */ |
|
367 | public function setDefaultGridFSRepositoryClassName(string $className): void |
||
368 | 1584 | { |
|
369 | $reflectionClass = new \ReflectionClass($className); |
||
370 | |||
371 | if (! $reflectionClass->implementsInterface(GridFSRepository::class)) { |
||
372 | throw MongoDBException::invalidGridFSRepository($className); |
||
373 | } |
||
374 | |||
375 | $this->attributes['defaultGridFSRepositoryClassName'] = $className; |
||
376 | 390 | } |
|
377 | |||
378 | 390 | public function getDefaultGridFSRepositoryClassName(): string |
|
379 | 390 | { |
|
380 | return $this->attributes['defaultGridFSRepositoryClassName'] ?? DefaultGridFSRepository::class; |
||
381 | 390 | } |
|
382 | |||
383 | public function setRepositoryFactory(RepositoryFactory $repositoryFactory): void |
||
384 | { |
||
385 | $this->attributes['repositoryFactory'] = $repositoryFactory; |
||
386 | } |
||
387 | |||
388 | public function getRepositoryFactory(): RepositoryFactory |
||
392 | 8 | ||
393 | 8 | public function setPersistentCollectionFactory(PersistentCollectionFactory $persistentCollectionFactory): void |
|
394 | 8 | { |
|
395 | $this->attributes['persistentCollectionFactory'] = $persistentCollectionFactory; |
||
396 | } |
||
397 | 8 | ||
398 | public function getPersistentCollectionFactory(): PersistentCollectionFactory |
||
399 | { |
||
400 | if (! isset($this->attributes['persistentCollectionFactory'])) { |
||
401 | $this->attributes['persistentCollectionFactory'] = new DefaultPersistentCollectionFactory(); |
||
402 | } |
||
403 | return $this->attributes['persistentCollectionFactory']; |
||
404 | } |
||
405 | |||
406 | public function setPersistentCollectionGenerator(PersistentCollectionGenerator $persistentCollectionGenerator): void |
||
407 | { |
||
408 | $this->attributes['persistentCollectionGenerator'] = $persistentCollectionGenerator; |
||
409 | } |
||
410 | |||
411 | public function getPersistentCollectionGenerator(): PersistentCollectionGenerator |
||
412 | { |
||
413 | if (! isset($this->attributes['persistentCollectionGenerator'])) { |
||
414 | $this->attributes['persistentCollectionGenerator'] = new DefaultPersistentCollectionGenerator( |
||
415 | $this->getPersistentCollectionDir(), |
||
416 | $this->getPersistentCollectionNamespace() |
||
417 | ); |
||
418 | } |
||
419 | return $this->attributes['persistentCollectionGenerator']; |
||
420 | } |
||
421 | } |
||
422 |