Complex classes like MongoDbServiceProvider 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 MongoDbServiceProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class MongoDbServiceProvider implements ServiceProviderInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var array Buffer. |
||
23 | */ |
||
24 | private $args = array(); |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $defaultArgs = array(); |
||
30 | |||
31 | /** |
||
32 | * @var int|string |
||
33 | */ |
||
34 | private $defaultConnection = 0; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $id = 'mongodb'; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $instances = 'mongodb.clients'; |
||
45 | |||
46 | /** |
||
47 | * @var bool |
||
48 | */ |
||
49 | private $isLoaded = false; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $parameters = array(); |
||
55 | |||
56 | /** |
||
57 | * @param string $id |
||
58 | * @param string $instances |
||
59 | */ |
||
60 | 12 | public function __construct($id = null, $instances = null) |
|
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | 5 | private function buildConnectionString(array $connection) |
|
86 | |||
87 | /** |
||
88 | * @param array|string $connection |
||
89 | * |
||
90 | * @return string |
||
91 | */ |
||
92 | 12 | private function buildUri($connection) |
|
100 | |||
101 | /** |
||
102 | * @param \Pimple\Container $app |
||
103 | * @param string $name |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 12 | private function getArg(Container $app, $name) |
|
111 | |||
112 | 5 | private function getArgConnectionAuthority(array $connection) |
|
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 5 | private function getArgConnectionHost(array $connection = array()) |
|
137 | |||
138 | 1 | private function getArgConnectionHosts(array $connection) |
|
144 | |||
145 | /** |
||
146 | * @return string |
||
147 | */ |
||
148 | 12 | private function getArgUri() |
|
156 | |||
157 | /** |
||
158 | * @param \Pimple\Container $app |
||
159 | * @param string $name |
||
160 | * |
||
161 | * @return string|array |
||
162 | */ |
||
163 | 11 | private function getDefaultArg(Container $app, $name) |
|
171 | |||
172 | 12 | private function loadParameters(Container $app) |
|
187 | |||
188 | 11 | private function loadSingletonParameters(Container $app) |
|
205 | |||
206 | /** |
||
207 | * @param string $uri |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | 5 | private function sanitizeUri($uri) |
|
219 | |||
220 | /** |
||
221 | * @param \Pimple\Container $app |
||
222 | * @param string $name |
||
223 | */ |
||
224 | 11 | private function setDefaultArg(Container $app, $name) |
|
230 | |||
231 | 12 | public function register(Container $app) |
|
261 | } |
||
262 |