Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like HttplugExtension 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 HttplugExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | class HttplugExtension extends Extension |
||
31 | { |
||
32 | /** |
||
33 | 8 | * {@inheritdoc} |
|
34 | */ |
||
35 | 8 | public function load(array $configs, ContainerBuilder $container) |
|
36 | 8 | { |
|
37 | $configuration = $this->getConfiguration($configs, $container); |
||
38 | 8 | $config = $this->processConfiguration($configuration, $configs); |
|
1 ignored issue
–
show
|
|||
39 | |||
40 | 8 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
41 | 8 | ||
42 | $loader->load('services.xml'); |
||
43 | $loader->load('plugins.xml'); |
||
44 | 8 | ||
45 | 8 | // Register default services |
|
46 | 1 | foreach ($config['classes'] as $service => $class) { |
|
47 | 1 | if (!empty($class)) { |
|
48 | 8 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
49 | } |
||
50 | } |
||
51 | 8 | ||
52 | 8 | // Set main aliases |
|
53 | 8 | foreach ($config['main_alias'] as $type => $id) { |
|
54 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
||
55 | } |
||
56 | 8 | ||
57 | 5 | // Configure toolbar |
|
58 | if ($this->isConfigEnabled($container, $config['profiling'])) { |
||
59 | 5 | $loader->load('data-collector.xml'); |
|
60 | |||
61 | if (!empty($config['profiling']['formatter'])) { |
||
62 | // Add custom formatter |
||
63 | $container |
||
64 | ->getDefinition('httplug.collector.debug_collector') |
||
65 | ->replaceArgument(0, new Reference($config['profiling']['formatter'])) |
||
66 | ; |
||
67 | } |
||
68 | 5 | ||
69 | 5 | $container |
|
70 | ->getDefinition('httplug.formatter.full_http_message') |
||
71 | 5 | ->addArgument($config['profiling']['captured_body_length']) |
|
72 | ; |
||
73 | 8 | } |
|
74 | 8 | ||
75 | 8 | $this->configureClients($container, $config); |
|
76 | 8 | $this->configureSharedPlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed |
|
77 | $this->configureAutoDiscoveryClients($container, $config); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Configure client services. |
||
82 | * |
||
83 | * @param ContainerBuilder $container |
||
84 | 8 | * @param array $config |
|
85 | */ |
||
86 | 8 | private function configureClients(ContainerBuilder $container, array $config) |
|
108 | |||
109 | /** |
||
110 | * @param ContainerBuilder $container |
||
111 | 8 | * @param array $config |
|
112 | */ |
||
113 | 8 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
114 | { |
||
115 | if (!empty($config['authentication'])) { |
||
116 | 8 | $this->configureAuthentication($container, $config['authentication']); |
|
117 | } |
||
118 | 8 | unset($config['authentication']); |
|
119 | 8 | ||
120 | foreach ($config as $name => $pluginConfig) { |
||
121 | 8 | $pluginId = 'httplug.plugin.'.$name; |
|
122 | 8 | ||
123 | 8 | if ($this->isConfigEnabled($container, $pluginConfig)) { |
|
124 | 8 | $def = $container->getDefinition($pluginId); |
|
125 | 8 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
|
126 | } else { |
||
127 | 8 | $container->removeDefinition($pluginId); |
|
128 | 8 | } |
|
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $name |
||
134 | * @param Definition $definition |
||
135 | 8 | * @param array $config |
|
136 | * @param ContainerBuilder $container In case we need to add additional services for this plugin |
||
137 | * @param string $serviceId Service id of the plugin, in case we need to add additional services for this plugin. |
||
138 | 8 | */ |
|
139 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
||
140 | { |
||
141 | switch ($name) { |
||
142 | case 'cache': |
||
143 | $definition |
||
144 | 8 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
|
145 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
146 | ->replaceArgument(2, $config['config']); |
||
147 | 8 | break; |
|
148 | 8 | case 'cookie': |
|
149 | 8 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
|
150 | 8 | break; |
|
151 | 8 | case 'decoder': |
|
152 | 8 | $definition->addArgument([ |
|
153 | 'use_content_encoding' => $config['use_content_encoding'], |
||
154 | ]); |
||
155 | 8 | break; |
|
156 | 8 | case 'history': |
|
157 | 8 | $definition->replaceArgument(0, new Reference($config['journal'])); |
|
158 | break; |
||
159 | case 'logger': |
||
160 | 8 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
161 | 8 | if (!empty($config['formatter'])) { |
|
162 | 8 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
|
163 | 8 | } |
|
164 | 8 | break; |
|
165 | 8 | case 'redirect': |
|
166 | 8 | $definition->addArgument([ |
|
167 | 8 | 'preserve_header' => $config['preserve_header'], |
|
168 | 8 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
169 | 8 | ]); |
|
170 | 8 | break; |
|
171 | 8 | case 'retry': |
|
172 | 8 | $definition->addArgument([ |
|
173 | 8 | 'retries' => $config['retry'], |
|
174 | 8 | ]); |
|
175 | break; |
||
176 | case 'stopwatch': |
||
177 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
||
178 | break; |
||
179 | 8 | ||
180 | /* client specific plugins */ |
||
181 | |||
182 | case 'add_host': |
||
183 | $uriService = $serviceId.'.host_uri'; |
||
184 | $this->createUri($container, $uriService, $config['host']); |
||
185 | $definition->replaceArgument(0, new Reference($uriService)); |
||
186 | $definition->replaceArgument(1, [ |
||
187 | 'replace' => $config['replace'], |
||
188 | ]); |
||
189 | break; |
||
190 | case 'header_append': |
||
191 | case 'header_defaults': |
||
192 | case 'header_set': |
||
193 | case 'header_remove': |
||
194 | $definition->replaceArgument(0, $config['headers']); |
||
195 | break; |
||
196 | |||
197 | default: |
||
198 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
199 | } |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * @param ContainerBuilder $container |
||
204 | * @param array $config |
||
205 | * |
||
206 | * @return array List of service ids for the authentication plugins. |
||
207 | */ |
||
208 | private function configureAuthentication(ContainerBuilder $container, array $config, $servicePrefix = 'httplug.plugin.authentication') |
||
209 | { |
||
210 | $pluginServices = []; |
||
211 | |||
212 | foreach ($config as $name => $values) { |
||
213 | $authServiceKey = sprintf($servicePrefix.'.%s.auth', $name); |
||
214 | switch ($values['type']) { |
||
215 | case 'bearer': |
||
216 | $container->register($authServiceKey, Bearer::class) |
||
217 | ->addArgument($values['token']); |
||
218 | break; |
||
219 | case 'basic': |
||
220 | $container->register($authServiceKey, BasicAuth::class) |
||
221 | ->addArgument($values['username']) |
||
222 | ->addArgument($values['password']); |
||
223 | break; |
||
224 | case 'wsse': |
||
225 | $container->register($authServiceKey, Wsse::class) |
||
226 | ->addArgument($values['username']) |
||
227 | ->addArgument($values['password']); |
||
228 | break; |
||
229 | case 'service': |
||
230 | $authServiceKey = $values['service']; |
||
231 | break; |
||
232 | default: |
||
233 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
234 | } |
||
235 | |||
236 | $pluginServiceKey = $servicePrefix.'.'.$name; |
||
237 | $container->register($pluginServiceKey, AuthenticationPlugin::class) |
||
238 | ->addArgument(new Reference($authServiceKey)) |
||
239 | ; |
||
240 | $pluginServices[] = $pluginServiceKey; |
||
241 | } |
||
242 | |||
243 | return $pluginServices; |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * @param ContainerBuilder $container |
||
248 | * @param string $clientName |
||
249 | * @param array $arguments |
||
250 | * @param bool $profiling |
||
251 | */ |
||
252 | private function configureClient(ContainerBuilder $container, $clientName, array $arguments, $profiling) |
||
253 | { |
||
254 | $serviceId = 'httplug.client.'.$clientName; |
||
255 | |||
256 | $plugins = []; |
||
257 | foreach ($arguments['plugins'] as $plugin) { |
||
258 | list($pluginName, $pluginConfig) = each($plugin); |
||
259 | if ('reference' === $pluginName) { |
||
260 | $plugins[] = $pluginConfig['id']; |
||
261 | } elseif ('authentication' === $pluginName) { |
||
262 | $plugins = array_merge($plugins, $this->configureAuthentication($container, $pluginConfig, $serviceId.'.authentication')); |
||
263 | } else { |
||
264 | $pluginServiceId = $serviceId.'.plugin.'.$pluginName; |
||
265 | $def = clone $container->getDefinition('httplug.plugin'.'.'.$pluginName); |
||
266 | $def->setAbstract(false); |
||
267 | $this->configurePluginByName($pluginName, $def, $pluginConfig, $container, $pluginServiceId); |
||
268 | $container->setDefinition($pluginServiceId, $def); |
||
269 | $plugins[] = $pluginServiceId; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | $pluginClientOptions = []; |
||
274 | if ($profiling) { |
||
275 | // Add the stopwatch plugin |
||
276 | if (!in_array('httplug.plugin.stopwatch', $arguments['plugins'])) { |
||
277 | array_unshift($plugins, 'httplug.plugin.stopwatch'); |
||
278 | } |
||
279 | |||
280 | // Tell the plugin journal what plugins we used |
||
281 | $container |
||
282 | ->getDefinition('httplug.collector.plugin_journal') |
||
283 | ->addMethodCall('setPlugins', [$clientName, $plugins]) |
||
284 | ; |
||
285 | |||
286 | $debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId); |
||
287 | |||
288 | $pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)]; |
||
289 | } |
||
290 | |||
291 | $container |
||
292 | ->register($serviceId, DummyClient::class) |
||
293 | ->setFactory([PluginClientFactory::class, 'createPluginClient']) |
||
294 | ->addArgument( |
||
295 | array_map( |
||
296 | function ($id) { |
||
297 | return new Reference($id); |
||
298 | }, |
||
299 | $plugins |
||
300 | ) |
||
301 | ) |
||
302 | ->addArgument(new Reference($arguments['factory'])) |
||
303 | ->addArgument($arguments['config']) |
||
304 | ->addArgument($pluginClientOptions) |
||
305 | ; |
||
306 | |||
307 | |||
308 | /* |
||
309 | * Decorate the client with clients from client-common |
||
310 | */ |
||
311 | View Code Duplication | if ($arguments['flexible_client']) { |
|
312 | $container |
||
313 | ->register($serviceId.'.flexible', FlexibleHttpClient::class) |
||
314 | ->addArgument(new Reference($serviceId.'.flexible.inner')) |
||
315 | ->setPublic(false) |
||
316 | ->setDecoratedService($serviceId) |
||
317 | ; |
||
318 | } |
||
319 | |||
320 | View Code Duplication | if ($arguments['http_methods_client']) { |
|
321 | $container |
||
322 | ->register($serviceId.'.http_methods', HttpMethodsClient::class) |
||
323 | ->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')]) |
||
324 | ->setPublic(false) |
||
325 | ->setDecoratedService($serviceId) |
||
326 | ; |
||
327 | } |
||
328 | |||
329 | View Code Duplication | if ($arguments['batch_client']) { |
|
330 | $container |
||
331 | ->register($serviceId.'.batch_client', BatchClient::class) |
||
332 | ->setArguments([new Reference($serviceId.'.batch_client.inner')]) |
||
333 | ->setPublic(false) |
||
334 | ->setDecoratedService($serviceId) |
||
335 | ; |
||
336 | } |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Create a URI object with the default URI factory. |
||
341 | * |
||
342 | * @param ContainerBuilder $container |
||
343 | * @param string $serviceId Name of the private service to create |
||
344 | * @param string $uri String representation of the URI |
||
345 | */ |
||
346 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
347 | { |
||
348 | $container |
||
349 | ->register($serviceId, UriInterface::class) |
||
350 | ->setPublic(false) |
||
351 | ->setFactory([new Reference('httplug.uri_factory'), 'createUri']) |
||
352 | ->addArgument($uri) |
||
353 | ; |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
358 | * by finding a client using auto discovery. |
||
359 | * |
||
360 | * @param ContainerBuilder $container |
||
361 | * @param array $config |
||
362 | */ |
||
363 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
364 | { |
||
365 | $httpClient = $config['discovery']['client']; |
||
366 | |||
367 | View Code Duplication | if (!empty($httpClient)) { |
|
368 | if ($httpClient === 'auto') { |
||
369 | $httpClient = $this->registerAutoDiscoverableClient( |
||
370 | $container, |
||
371 | 'auto_discovered_client', |
||
372 | [HttpClientDiscovery::class, 'find'], |
||
373 | $this->isConfigEnabled($container, $config['profiling']) |
||
374 | ); |
||
375 | } |
||
376 | |||
377 | $httpClient = new Reference($httpClient); |
||
378 | } |
||
379 | |||
380 | $asyncHttpClient = $config['discovery']['async_client']; |
||
381 | |||
382 | View Code Duplication | if (!empty($asyncHttpClient)) { |
|
383 | if ($asyncHttpClient === 'auto') { |
||
384 | $asyncHttpClient = $this->registerAutoDiscoverableClient( |
||
385 | $container, |
||
386 | 'auto_discovered_async', |
||
387 | [HttpAsyncClientDiscovery::class, 'find'], |
||
388 | $this->isConfigEnabled($container, $config['profiling']) |
||
389 | ); |
||
390 | } |
||
391 | |||
392 | $asyncHttpClient = new Reference($asyncHttpClient); |
||
393 | } |
||
394 | |||
395 | $container |
||
396 | ->getDefinition('httplug.strategy') |
||
397 | ->addArgument($httpClient) |
||
398 | ->addArgument($asyncHttpClient) |
||
399 | ; |
||
400 | } |
||
401 | |||
402 | /** |
||
403 | * Find a client with auto discovery and return a service Reference to it. |
||
404 | * |
||
405 | * @param ContainerBuilder $container |
||
406 | * @param string $name |
||
407 | * @param callable $factory |
||
408 | * @param bool $profiling |
||
409 | * |
||
410 | * @return string service id |
||
411 | */ |
||
412 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
438 | |||
439 | /** |
||
440 | * Create a new plugin service for this client. |
||
441 | * |
||
442 | * @param ContainerBuilder $container |
||
443 | * @param string $serviceId |
||
444 | * |
||
445 | * @return string |
||
446 | */ |
||
447 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
460 | |||
461 | /** |
||
462 | * {@inheritdoc} |
||
463 | */ |
||
464 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
468 | } |
||
469 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: