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 | public function __construct($id = null, $instances = null) |
||
72 | |||
73 | private function buildAuthority(array $connection) |
||
74 | { |
||
75 | $authority = ''; |
||
76 | |||
77 | if (!empty($connection['password'])) { |
||
78 | $authority .= $connection['username'] . ':' .$connection['password'] . '@'; |
||
79 | } elseif (!empty($connection['username'])) { |
||
80 | $authority .= $connection['username'] . '@'; |
||
81 | } |
||
82 | |||
83 | $authority .= $this->buildHost($connection); |
||
84 | |||
85 | return $authority; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @return string |
||
90 | */ |
||
91 | private function buildConnectionString(array $connection) |
||
92 | { |
||
93 | $uri = '//'; |
||
94 | |||
95 | if (isset($connection[0]) && count($connection) == 1) { |
||
96 | $uri .= $this->sanitizeUri($connection[0]); |
||
97 | } else { |
||
98 | $uri .= $this->buildAuthority($connection); |
||
99 | |||
100 | if (!empty($connection['database'])) { |
||
101 | $uri .= '/' . $connection['database']; |
||
102 | |||
103 | if (!empty($connection['options'])) { |
||
104 | $uri .= '?' . http_build_query($connection['options']); |
||
105 | } |
||
106 | } elseif (!empty($connection['options'])) { |
||
107 | $uri .= '/?' . http_build_query($connection['options']); |
||
108 | } |
||
109 | } |
||
110 | |||
111 | return $uri; |
||
112 | } |
||
113 | |||
114 | private function buildHost(array $components) |
||
115 | { |
||
116 | $hosts = array(); |
||
117 | |||
118 | if (isset($components['hosts'])) { |
||
119 | $hosts[] = $this->buildHost($components['hosts']); |
||
120 | } else { |
||
121 | $hosts[] = empty($components['host']) ? '127.0.0.1' : $components['host']; |
||
122 | } |
||
123 | |||
124 | foreach ($components as $component) { |
||
125 | if (is_array($component)) { |
||
126 | $hosts = empty($component['host']) ? '127.0.0.1' : $component['host']; |
||
127 | |||
128 | if (!empty($component['port'])) { |
||
129 | $hosts .= ':' . $component['port']; |
||
130 | } |
||
131 | } else { |
||
132 | $hosts[] = $component; |
||
133 | } |
||
134 | } |
||
135 | |||
136 | return implode(',', $hosts); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | private function buildUri(array $connection) |
||
146 | |||
147 | /** |
||
148 | * @param \Pimple\Container $app |
||
149 | * @param string $name |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | private function getArg(Container $app, $name) |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | private function getArgUri(Container $app) |
||
171 | |||
172 | /** |
||
173 | * @param \Pimple\Container $app |
||
174 | * @param string $name |
||
175 | * |
||
176 | * @return string|array |
||
177 | */ |
||
178 | private function getDefaultArg(Container $app, $name) |
||
186 | |||
187 | private function loadParameters(Container $app) |
||
202 | |||
203 | private function loadSingletonParameters(Container $app) |
||
220 | |||
221 | /** |
||
222 | * @param string $uri |
||
223 | * |
||
224 | * @return string |
||
225 | */ |
||
226 | private function sanitizeUri($uri) |
||
234 | |||
235 | /** |
||
236 | * @param \Pimple\Container $app |
||
237 | * @param string $name |
||
238 | */ |
||
239 | private function setDefaultArg(Container $app, $name) |
||
245 | |||
246 | public function register(Container $app) |
||
280 | } |
||
281 |