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 |
||
28 | class HttplugExtension extends Extension |
||
29 | { |
||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | */ |
||
33 | 8 | public function load(array $configs, ContainerBuilder $container) |
|
34 | { |
||
35 | 8 | $configuration = $this->getConfiguration($configs, $container); |
|
36 | 8 | $config = $this->processConfiguration($configuration, $configs); |
|
1 ignored issue
–
show
|
|||
37 | |||
38 | 8 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
39 | |||
40 | 8 | $loader->load('services.xml'); |
|
41 | 8 | $loader->load('plugins.xml'); |
|
42 | |||
43 | // Register default services |
||
44 | 8 | foreach ($config['classes'] as $service => $class) { |
|
45 | 8 | if (!empty($class)) { |
|
46 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
47 | 1 | } |
|
48 | 8 | } |
|
49 | |||
50 | // Set main aliases |
||
51 | 8 | foreach ($config['main_alias'] as $type => $id) { |
|
52 | 8 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
53 | 8 | } |
|
54 | |||
55 | // Configure toolbar |
||
56 | 8 | if ($config['profiling']['enabled']) { |
|
57 | 5 | $loader->load('data-collector.xml'); |
|
58 | |||
59 | 5 | if (!empty($config['profiling']['formatter'])) { |
|
60 | // Add custom formatter |
||
61 | $container |
||
62 | ->getDefinition('httplug.collector.debug_collector') |
||
63 | ->replaceArgument(0, new Reference($config['profiling']['formatter'])) |
||
64 | ; |
||
65 | } |
||
66 | |||
67 | $container |
||
68 | 5 | ->getDefinition('httplug.formatter.full_http_message') |
|
69 | 5 | ->addArgument($config['profiling']['captured_body_length']) |
|
70 | ; |
||
71 | 5 | } |
|
72 | |||
73 | 8 | $this->configurePlugins($container, $config['plugins']); |
|
74 | 8 | $this->configureClients($container, $config); |
|
75 | 8 | $this->configureAutoDiscoveryClients($container, $config); |
|
76 | 8 | } |
|
77 | |||
78 | /** |
||
79 | * Configure client services. |
||
80 | * |
||
81 | * @param ContainerBuilder $container |
||
82 | * @param array $config |
||
83 | */ |
||
84 | 8 | private function configureClients(ContainerBuilder $container, array $config) |
|
106 | |||
107 | /** |
||
108 | * @param ContainerBuilder $container |
||
109 | * @param array $config |
||
110 | */ |
||
111 | 8 | private function configurePlugins(ContainerBuilder $container, array $config) |
|
129 | |||
130 | /** |
||
131 | * @param string $name |
||
132 | * @param Definition $definition |
||
133 | * @param array $config |
||
134 | */ |
||
135 | 8 | private function configurePluginByName($name, Definition $definition, array $config) |
|
136 | { |
||
137 | switch ($name) { |
||
138 | 8 | case 'cache': |
|
139 | $definition |
||
140 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
141 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
142 | ->replaceArgument(2, $config['config']); |
||
143 | break; |
||
144 | 8 | case 'cookie': |
|
145 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
146 | break; |
||
147 | 8 | case 'decoder': |
|
148 | 8 | $definition->addArgument([ |
|
149 | 8 | 'use_content_encoding' => $config['use_content_encoding'], |
|
150 | 8 | ]); |
|
151 | 8 | break; |
|
152 | 8 | case 'history': |
|
153 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
154 | break; |
||
155 | 8 | case 'logger': |
|
156 | 8 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
157 | 8 | if (!empty($config['formatter'])) { |
|
158 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
159 | } |
||
160 | 8 | break; |
|
161 | 8 | case 'redirect': |
|
162 | 8 | $definition->addArgument([ |
|
163 | 8 | 'preserve_header' => $config['preserve_header'], |
|
164 | 8 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
165 | 8 | ]); |
|
166 | 8 | break; |
|
167 | 8 | case 'retry': |
|
168 | 8 | $definition->addArgument([ |
|
169 | 8 | 'retries' => $config['retry'], |
|
170 | 8 | ]); |
|
171 | 8 | break; |
|
172 | 8 | case 'stopwatch': |
|
173 | 8 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
|
174 | 8 | break; |
|
175 | |||
176 | default: |
||
177 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
178 | } |
||
179 | 8 | } |
|
180 | |||
181 | /** |
||
182 | * @param ContainerBuilder $container |
||
183 | * @param array $config |
||
184 | */ |
||
185 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
186 | { |
||
187 | foreach ($config as $name => $values) { |
||
188 | $authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name); |
||
189 | switch ($values['type']) { |
||
190 | case 'bearer': |
||
191 | $container->register($authServiceKey, Bearer::class) |
||
192 | ->addArgument($values['token']); |
||
193 | break; |
||
194 | case 'basic': |
||
195 | $container->register($authServiceKey, BasicAuth::class) |
||
196 | ->addArgument($values['username']) |
||
197 | ->addArgument($values['password']); |
||
198 | break; |
||
199 | case 'wsse': |
||
200 | $container->register($authServiceKey, Wsse::class) |
||
201 | ->addArgument($values['username']) |
||
202 | ->addArgument($values['password']); |
||
203 | break; |
||
204 | case 'service': |
||
205 | $authServiceKey = $values['service']; |
||
206 | break; |
||
207 | default: |
||
208 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
209 | } |
||
210 | |||
211 | $container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class) |
||
212 | ->addArgument(new Reference($authServiceKey)); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * @param ContainerBuilder $container |
||
218 | * @param string $name |
||
219 | * @param array $arguments |
||
220 | * @param bool $profiling |
||
221 | */ |
||
222 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling) |
||
292 | |||
293 | /** |
||
294 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
295 | * by finding a client using auto discovery. |
||
296 | * |
||
297 | * @param ContainerBuilder $container |
||
298 | * @param array $config |
||
299 | */ |
||
300 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
338 | |||
339 | /** |
||
340 | * Find a client with auto discovery and return a service Reference to it. |
||
341 | * |
||
342 | * @param ContainerBuilder $container |
||
343 | * @param string $name |
||
344 | * @param callable $factory |
||
345 | * @param bool $profiling |
||
346 | * |
||
347 | * @return string service id |
||
348 | */ |
||
349 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
375 | |||
376 | /** |
||
377 | * Create a new plugin service for this client. |
||
378 | * |
||
379 | * @param ContainerBuilder $container |
||
380 | * @param string $serviceId |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
397 | |||
398 | /** |
||
399 | * {@inheritdoc} |
||
400 | */ |
||
401 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
405 | } |
||
406 |
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: