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 | 'uri_options' => array(), |
||
31 | 'driver_options' => array(), |
||
32 | ); |
||
33 | |||
34 | /** |
||
35 | * @var int|string |
||
36 | */ |
||
37 | private $defaultConnection = 0; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $id = 'mongodb'; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $instances = 'mongodb.clients'; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $isLoaded = false; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | private $parameters = array(); |
||
58 | |||
59 | /** |
||
60 | * @param string $id |
||
61 | * @param string $instances |
||
62 | */ |
||
63 | 6 | public function __construct($id = null, $instances = null) |
|
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 6 | private function buildConnectionString(array $connection) |
|
77 | { |
||
78 | 6 | if (isset($connection[0]) && count($connection) == 1) { |
|
79 | 1 | return '//' . $this->sanitizeUri($connection[0]); |
|
80 | } |
||
81 | |||
82 | 5 | $uri = '//' . $this->getArgConnectionAuthority($connection); |
|
83 | |||
84 | 5 | if (!empty($connection['db'])) { |
|
85 | 2 | $uri .= '/' . $connection['db']; |
|
86 | |||
87 | 2 | if (!empty($connection['options'])) { |
|
88 | $uri .= '?' . http_build_query($connection['options']); |
||
89 | } |
||
90 | 5 | } elseif (!empty($connection['options'])) { |
|
91 | 1 | $uri .= '/?' . http_build_query($connection['options']); |
|
92 | 1 | } |
|
93 | |||
94 | 5 | return $uri; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @param array|string $connection |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | 6 | private function buildUri($connection) |
|
106 | |||
107 | /** |
||
108 | * @param \Pimple\Container $app |
||
109 | * @param string $name |
||
110 | * |
||
111 | * @return string |
||
112 | */ |
||
113 | 6 | private function getArg(Container $app, $name) |
|
117 | |||
118 | 5 | private function getArgConnectionAuthority(array $connection) |
|
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | 5 | private function getArgConnectionHost(array $connection = array()) |
|
142 | |||
143 | private function getArgConnectionHosts(array $connection) |
||
153 | |||
154 | /** |
||
155 | * @return string |
||
156 | */ |
||
157 | 6 | private function getArgUri() |
|
165 | |||
166 | /** |
||
167 | * @param \Pimple\Container $app |
||
168 | * @param string $name |
||
169 | * |
||
170 | * @return string|array |
||
171 | */ |
||
172 | 6 | private function getDefaultArg(Container $app, $name) |
|
180 | |||
181 | 6 | private function loadParameters(Container $app) |
|
196 | |||
197 | 5 | private function loadSingletonParameters(Container $app) |
|
214 | |||
215 | /** |
||
216 | * @param string $uri |
||
217 | * |
||
218 | * @return string |
||
219 | */ |
||
220 | 1 | private function sanitizeUri($uri) |
|
228 | |||
229 | /** |
||
230 | * @param \Pimple\Container $app |
||
231 | * @param string $name |
||
232 | */ |
||
233 | private function setDefaultArg(Container $app, $name) |
||
239 | |||
240 | 6 | public function register(Container $app) |
|
270 | } |
||
271 |