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 |
||
20 | class Configuration |
||
21 | { |
||
22 | //begin of properties |
||
23 | /** @var string */ |
||
24 | private $extends; |
||
25 | |||
26 | /** @var array */ |
||
27 | private $instances = array(); |
||
28 | |||
29 | /** @var array */ |
||
30 | private $implements = array(); |
||
31 | |||
32 | /** @var string */ |
||
33 | private $className; |
||
34 | |||
35 | /** @var bool */ |
||
36 | private $createLocatorGeneratorInterface = false; |
||
37 | |||
38 | /** @var FetchFromFactoryInstancePoolBuilder */ |
||
39 | private $fetchFromFactoryInstancePoolBuilder; |
||
40 | |||
41 | /** @var FetchFromSharedInstancePoolBuilder */ |
||
42 | private $fetchFromSharedInstancePoolBuilder; |
||
43 | |||
44 | /** @var FetchFromSharedInstancePoolOrCreateByFactoryBuilder */ |
||
45 | private $fetchFromSharedInstancePoolOrCreateByFactoryBuilder; |
||
46 | |||
47 | /** @var bool */ |
||
48 | private $hasFactoryInstances = false; |
||
49 | |||
50 | /** @var bool */ |
||
51 | private $hasSharedInstances = false; |
||
52 | |||
53 | /** @var Instance */ |
||
54 | private $instance; |
||
55 | |||
56 | /** @var string */ |
||
57 | private $namespace; |
||
58 | |||
59 | /** @var NewInstanceBuilder */ |
||
60 | private $newInstanceBuilder; |
||
61 | |||
62 | /** @var string */ |
||
63 | private $methodPrefix; |
||
64 | |||
65 | /** @var array */ |
||
66 | private $methodBodyBuilderInstancePool = array(); |
||
67 | |||
68 | /** @var string */ |
||
69 | private $fileName; |
||
70 | |||
71 | /** @var string */ |
||
72 | private $filePath; |
||
73 | |||
74 | /** @var array */ |
||
75 | private $useCollection = array(); |
||
76 | |||
77 | /** @var Uses */ |
||
78 | private $uses; |
||
79 | //end of properties |
||
80 | |||
81 | //begin of public methods |
||
82 | /** |
||
83 | * @return null|string |
||
84 | */ |
||
85 | public function getClassName() |
||
89 | |||
90 | /** |
||
91 | * @param string $name |
||
92 | * @return $this |
||
93 | */ |
||
94 | public function setClassName($name) |
||
101 | |||
102 | /** |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function createLocatorGeneratorInterface() |
||
109 | |||
110 | /** |
||
111 | * @param bool $flag |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function setCreateLocatorGeneratorInterface($flag = false) |
||
120 | |||
121 | /** |
||
122 | * @return null|string |
||
123 | */ |
||
124 | public function getFileName() |
||
128 | |||
129 | /** |
||
130 | * @return string |
||
131 | */ |
||
132 | public function getFileNameExtension() |
||
136 | |||
137 | /** |
||
138 | * @return null|string |
||
139 | */ |
||
140 | public function getFilePath() |
||
144 | |||
145 | /** |
||
146 | * @param string $path |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function setFilePath($path) |
||
155 | |||
156 | /** |
||
157 | * @param Instance $instance |
||
158 | * @return $this |
||
159 | */ |
||
160 | public function setInstance(Instance $instance) |
||
166 | |||
167 | /** |
||
168 | * @param Uses $uses |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function setUses(Uses $uses) |
||
177 | |||
178 | /** |
||
179 | * @return null|string |
||
180 | */ |
||
181 | public function getNamespace() |
||
185 | |||
186 | /** |
||
187 | * @return bool |
||
188 | */ |
||
189 | public function hasNamespace() |
||
193 | |||
194 | /** |
||
195 | * @param string $namespace |
||
196 | * @return $this |
||
197 | */ |
||
198 | public function setNamespace($namespace) |
||
208 | |||
209 | /** |
||
210 | * @param string $methodPrefix |
||
211 | * @return $this |
||
212 | */ |
||
213 | public function setMethodPrefix($methodPrefix) |
||
219 | |||
220 | /** |
||
221 | * @return null|string |
||
222 | */ |
||
223 | public function getMethodPrefix() |
||
227 | |||
228 | /** |
||
229 | * @param string $className |
||
230 | * @param bool $isFactory |
||
231 | * @param bool $isShared |
||
232 | * @param string $returnValue |
||
233 | * @param string $alias |
||
234 | * @param null|string $methodBodyBuilderClassName |
||
235 | * @return $this |
||
236 | * @throws RuntimeException |
||
237 | */ |
||
238 | public function addInstance($className, $isFactory, $isShared, $returnValue, $alias, $methodBodyBuilderClassName = null) |
||
268 | |||
269 | /** |
||
270 | * @return array|Instance[] |
||
271 | */ |
||
272 | public function getInstances() |
||
276 | |||
277 | /** |
||
278 | * @return boolean |
||
279 | */ |
||
280 | public function hasInstances() |
||
284 | |||
285 | /** |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function hasFactoryInstances() |
||
292 | |||
293 | /** |
||
294 | * @return bool |
||
295 | */ |
||
296 | public function hasSharedInstances() |
||
300 | |||
301 | /** |
||
302 | * @param string $interfaceName |
||
303 | * @return $this |
||
304 | */ |
||
305 | public function addImplements($interfaceName) |
||
311 | |||
312 | /** |
||
313 | * @return array |
||
314 | */ |
||
315 | public function getImplements() |
||
319 | |||
320 | /** |
||
321 | * @return boolean |
||
322 | */ |
||
323 | public function hasImplements() |
||
327 | |||
328 | /** |
||
329 | * @param string $className |
||
330 | * @return $this |
||
331 | */ |
||
332 | public function setExtends($className) |
||
338 | |||
339 | /** |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function hasExtends() |
||
346 | |||
347 | /** |
||
348 | * @return null|string |
||
349 | */ |
||
350 | public function getExtends() |
||
354 | |||
355 | /** |
||
356 | * @param string $className |
||
357 | * @param string $alias |
||
358 | * @return $this |
||
359 | */ |
||
360 | public function addUses($className, $alias = '') |
||
371 | |||
372 | /** |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function hasUses() |
||
379 | |||
380 | /** |
||
381 | * @return array|Uses[] |
||
382 | */ |
||
383 | public function getUseCollection() |
||
387 | |||
388 | /** |
||
389 | * @param FetchFromFactoryInstancePoolBuilder $fetchFromFactoryInstancePoolBuilder |
||
390 | * @return $this |
||
391 | */ |
||
392 | public function setFetchFromFactoryInstancePoolBuilder(FetchFromFactoryInstancePoolBuilder $fetchFromFactoryInstancePoolBuilder) |
||
398 | |||
399 | /** |
||
400 | * @param FetchFromSharedInstancePoolBuilder $fetchFromSharedInstancePoolBuilder |
||
401 | * @return $this |
||
402 | */ |
||
403 | public function setFetchFromSharedInstancePoolBuilder(FetchFromSharedInstancePoolBuilder $fetchFromSharedInstancePoolBuilder) |
||
409 | |||
410 | /** |
||
411 | * @param FetchFromSharedInstancePoolOrCreateByFactoryBuilder $fetchFromSharedInstancePoolOrCreateByFactoryBuilder |
||
412 | * @return $this |
||
413 | */ |
||
414 | public function setFetchFromSharedInstancePoolOrCreateByFactoryBuilder(FetchFromSharedInstancePoolOrCreateByFactoryBuilder $fetchFromSharedInstancePoolOrCreateByFactoryBuilder) |
||
420 | |||
421 | /** |
||
422 | * @param NewInstanceBuilder $newInstanceBuilder |
||
423 | * @return $this |
||
424 | */ |
||
425 | public function setNewInstanceBuilder(NewInstanceBuilder $newInstanceBuilder) |
||
431 | //end of public methods |
||
432 | |||
433 | //begin of private methods |
||
434 | /** |
||
435 | * @return Uses |
||
436 | */ |
||
437 | private function getNewUses() |
||
441 | |||
442 | /** |
||
443 | * @return Instance |
||
444 | */ |
||
445 | private function getNewInstance() |
||
449 | |||
450 | /** |
||
451 | * @param Instance $instance |
||
452 | * @return Instance |
||
453 | * @throws RuntimeException |
||
454 | */ |
||
455 | private function tryToDetermineMethodBodyBuilder(Instance $instance) |
||
480 | |||
481 | /** |
||
482 | * @param $methodBodyBuilderClassName |
||
483 | * @return \Net\Bazzline\Component\Locator\MethodBodyBuilder\MethodBodyBuilderInterface |
||
484 | * @throws RuntimeException |
||
485 | */ |
||
486 | private function fetchFromMethodBodyBuilderInstancePool($methodBodyBuilderClassName) |
||
509 | |||
510 | private function enableHasFactoryInstances() |
||
517 | //end of private methods |
||
518 | } |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.