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 |
||
32 | class HttplugExtension extends Extension |
||
33 | { |
||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 9 | public function load(array $configs, ContainerBuilder $container) |
|
38 | { |
||
39 | 9 | $configuration = $this->getConfiguration($configs, $container); |
|
40 | 9 | $config = $this->processConfiguration($configuration, $configs); |
|
1 ignored issue
–
show
|
|||
41 | |||
42 | 9 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
43 | |||
44 | 9 | $loader->load('services.xml'); |
|
45 | 9 | $loader->load('plugins.xml'); |
|
46 | |||
47 | // Register default services |
||
48 | 9 | foreach ($config['classes'] as $service => $class) { |
|
49 | 9 | if (!empty($class)) { |
|
50 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
51 | 1 | } |
|
52 | 9 | } |
|
53 | |||
54 | // Set main aliases |
||
55 | 9 | foreach ($config['main_alias'] as $type => $id) { |
|
56 | 9 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
57 | 9 | } |
|
58 | |||
59 | // Configure toolbar |
||
60 | 9 | if ($this->isConfigEnabled($container, $config['profiling'])) { |
|
61 | 6 | $loader->load('data-collector.xml'); |
|
62 | |||
63 | 6 | if (!empty($config['profiling']['formatter'])) { |
|
64 | // Add custom formatter |
||
65 | $container |
||
66 | ->getDefinition('httplug.collector.debug_collector') |
||
67 | ->replaceArgument(0, new Reference($config['profiling']['formatter'])) |
||
68 | ; |
||
69 | } |
||
70 | |||
71 | $container |
||
72 | 6 | ->getDefinition('httplug.formatter.full_http_message') |
|
73 | 6 | ->addArgument($config['profiling']['captured_body_length']) |
|
74 | ; |
||
75 | 6 | } |
|
76 | |||
77 | 9 | $this->configureClients($container, $config, $this->isConfigEnabled($container, $config['profiling'])); |
|
78 | 9 | $this->configureSharedPlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed |
|
79 | 9 | $this->configureAutoDiscoveryClients($container, $config); |
|
80 | 9 | } |
|
81 | |||
82 | /** |
||
83 | * Configure client services. |
||
84 | * |
||
85 | * @param ContainerBuilder $container |
||
86 | * @param array $config |
||
87 | * @param bool $profiling |
||
88 | */ |
||
89 | 9 | private function configureClients(ContainerBuilder $container, array $config, $profiling) |
|
90 | { |
||
91 | 9 | $first = null; |
|
92 | 9 | $clients = []; |
|
93 | |||
94 | 9 | foreach ($config['clients'] as $name => $arguments) { |
|
95 | 6 | if ($first === null) { |
|
96 | // Save the name of the first configured client. |
||
97 | 6 | $first = $name; |
|
98 | 6 | } |
|
99 | |||
100 | 6 | $this->configureClient($container, $name, $arguments, $this->isConfigEnabled($container, $config['profiling'])); |
|
101 | 6 | $clients[] = $name; |
|
102 | 9 | } |
|
103 | |||
104 | // If we have clients configured |
||
105 | 9 | if ($first !== null) { |
|
106 | // If we do not have a client named 'default' |
||
107 | 6 | if (!isset($config['clients']['default'])) { |
|
108 | // Alias the first client to httplug.client.default |
||
109 | 6 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
|
110 | 6 | } |
|
111 | 6 | } |
|
112 | |||
113 | 9 | if ($profiling) { |
|
114 | 6 | $container->getDefinition('httplug.collector.collector') |
|
115 | 6 | ->setArguments([$clients]) |
|
116 | ; |
||
117 | 6 | } |
|
118 | 9 | } |
|
119 | |||
120 | /** |
||
121 | * @param ContainerBuilder $container |
||
122 | * @param array $config |
||
123 | */ |
||
124 | 9 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
125 | { |
||
126 | 9 | if (!empty($config['authentication'])) { |
|
127 | $this->configureAuthentication($container, $config['authentication']); |
||
128 | } |
||
129 | 9 | unset($config['authentication']); |
|
130 | |||
131 | 9 | foreach ($config as $name => $pluginConfig) { |
|
132 | 9 | $pluginId = 'httplug.plugin.'.$name; |
|
133 | |||
134 | 9 | if ($this->isConfigEnabled($container, $pluginConfig)) { |
|
135 | 9 | $def = $container->getDefinition($pluginId); |
|
136 | 9 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
|
137 | 9 | } else { |
|
138 | 9 | $container->removeDefinition($pluginId); |
|
139 | } |
||
140 | 9 | } |
|
141 | 9 | } |
|
142 | |||
143 | /** |
||
144 | * @param string $name |
||
145 | * @param Definition $definition |
||
146 | * @param array $config |
||
147 | * @param ContainerBuilder $container In case we need to add additional services for this plugin |
||
148 | * @param string $serviceId Service id of the plugin, in case we need to add additional services for this plugin. |
||
149 | */ |
||
150 | 9 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
|
151 | { |
||
152 | switch ($name) { |
||
153 | 9 | case 'cache': |
|
154 | $definition |
||
155 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
156 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
157 | ->replaceArgument(2, $config['config']); |
||
158 | break; |
||
159 | 9 | case 'cookie': |
|
160 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
161 | break; |
||
162 | 9 | case 'decoder': |
|
163 | 9 | $definition->addArgument([ |
|
164 | 9 | 'use_content_encoding' => $config['use_content_encoding'], |
|
165 | 9 | ]); |
|
166 | 9 | break; |
|
167 | 9 | case 'history': |
|
168 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
169 | break; |
||
170 | 9 | case 'logger': |
|
171 | 9 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
172 | 9 | if (!empty($config['formatter'])) { |
|
173 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
174 | } |
||
175 | 9 | break; |
|
176 | 9 | case 'redirect': |
|
177 | 9 | $definition->addArgument([ |
|
178 | 9 | 'preserve_header' => $config['preserve_header'], |
|
179 | 9 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
180 | 9 | ]); |
|
181 | 9 | break; |
|
182 | 9 | case 'retry': |
|
183 | 9 | $definition->addArgument([ |
|
184 | 9 | 'retries' => $config['retry'], |
|
185 | 9 | ]); |
|
186 | 9 | break; |
|
187 | 9 | case 'stopwatch': |
|
188 | 9 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
|
189 | 9 | break; |
|
190 | |||
191 | /* client specific plugins */ |
||
192 | |||
193 | 3 | case 'add_host': |
|
194 | 3 | $uriService = $serviceId.'.host_uri'; |
|
195 | 3 | $this->createUri($container, $uriService, $config['host']); |
|
196 | 3 | $definition->replaceArgument(0, new Reference($uriService)); |
|
197 | 3 | $definition->replaceArgument(1, [ |
|
198 | 3 | 'replace' => $config['replace'], |
|
199 | 3 | ]); |
|
200 | 3 | break; |
|
201 | 1 | case 'header_append': |
|
202 | 1 | case 'header_defaults': |
|
203 | 1 | case 'header_set': |
|
204 | 1 | case 'header_remove': |
|
205 | 1 | $definition->replaceArgument(0, $config['headers']); |
|
206 | 1 | break; |
|
207 | |||
208 | default: |
||
209 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
210 | } |
||
211 | 9 | } |
|
212 | |||
213 | /** |
||
214 | * @param ContainerBuilder $container |
||
215 | * @param array $config |
||
216 | * |
||
217 | * @return array List of service ids for the authentication plugins. |
||
218 | */ |
||
219 | 3 | private function configureAuthentication(ContainerBuilder $container, array $config, $servicePrefix = 'httplug.plugin.authentication') |
|
220 | { |
||
221 | 3 | $pluginServices = []; |
|
222 | |||
223 | 3 | foreach ($config as $name => $values) { |
|
224 | 3 | $authServiceKey = sprintf($servicePrefix.'.%s.auth', $name); |
|
225 | 3 | switch ($values['type']) { |
|
226 | 3 | case 'bearer': |
|
227 | $container->register($authServiceKey, Bearer::class) |
||
228 | ->addArgument($values['token']); |
||
229 | break; |
||
230 | 3 | case 'basic': |
|
231 | $container->register($authServiceKey, BasicAuth::class) |
||
232 | 3 | ->addArgument($values['username']) |
|
233 | 3 | ->addArgument($values['password']); |
|
234 | 3 | break; |
|
235 | case 'wsse': |
||
236 | $container->register($authServiceKey, Wsse::class) |
||
237 | ->addArgument($values['username']) |
||
238 | ->addArgument($values['password']); |
||
239 | break; |
||
240 | case 'service': |
||
241 | $authServiceKey = $values['service']; |
||
242 | break; |
||
243 | default: |
||
244 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
245 | 3 | } |
|
246 | |||
247 | 3 | $pluginServiceKey = $servicePrefix.'.'.$name; |
|
248 | $container->register($pluginServiceKey, AuthenticationPlugin::class) |
||
249 | ->addArgument(new Reference($authServiceKey)) |
||
250 | ; |
||
251 | $pluginServices[] = $pluginServiceKey; |
||
252 | } |
||
253 | |||
254 | return $pluginServices; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * @param ContainerBuilder $container |
||
259 | * @param string $clientName |
||
260 | * @param array $arguments |
||
261 | * @param bool $profiling |
||
262 | */ |
||
263 | private function configureClient(ContainerBuilder $container, $clientName, array $arguments, $profiling) |
||
366 | |||
367 | /** |
||
368 | * Create a URI object with the default URI factory. |
||
369 | * |
||
370 | * @param ContainerBuilder $container |
||
371 | * @param string $serviceId Name of the private service to create |
||
372 | * @param string $uri String representation of the URI |
||
373 | */ |
||
374 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
383 | |||
384 | /** |
||
385 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
386 | * by finding a client using auto discovery. |
||
387 | * |
||
388 | * @param ContainerBuilder $container |
||
389 | * @param array $config |
||
390 | */ |
||
391 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
429 | |||
430 | /** |
||
431 | * Find a client with auto discovery and return a service Reference to it. |
||
432 | * |
||
433 | * @param ContainerBuilder $container |
||
434 | * @param string $name |
||
435 | * @param callable $factory |
||
436 | * @param bool $profiling |
||
437 | * |
||
438 | * @return string service id |
||
439 | */ |
||
440 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
462 | |||
463 | /** |
||
464 | * {@inheritdoc} |
||
465 | */ |
||
466 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
470 | } |
||
471 |
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: