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 |
||
26 | class HttplugExtension extends Extension |
||
27 | { |
||
28 | /** |
||
29 | * {@inheritdoc} |
||
30 | */ |
||
31 | 3 | public function load(array $configs, ContainerBuilder $container) |
|
32 | { |
||
33 | 3 | $configuration = $this->getConfiguration($configs, $container); |
|
34 | 3 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
35 | |||
36 | 3 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
37 | |||
38 | 3 | $loader->load('services.xml'); |
|
39 | 3 | $loader->load('plugins.xml'); |
|
40 | |||
41 | 3 | $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug'); |
|
42 | 3 | if ($enabled) { |
|
43 | $loader->load('data-collector.xml'); |
||
44 | $config['_inject_collector_plugin'] = true; |
||
45 | |||
46 | if (!empty($config['toolbar']['formatter'])) { |
||
47 | // Add custom formatter |
||
48 | $container->getDefinition('httplug.collector.debug_collector') |
||
49 | ->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
||
50 | } |
||
51 | |||
52 | $container->getDefinition('httplug.formatter.full_http_message') |
||
53 | ->addArgument($config['toolbar']['captured_body_length']); |
||
54 | } |
||
55 | |||
56 | 3 | foreach ($config['classes'] as $service => $class) { |
|
57 | 3 | if (!empty($class)) { |
|
58 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
59 | 1 | } |
|
60 | 3 | } |
|
61 | |||
62 | // Set main aliases |
||
63 | 3 | foreach ($config['main_alias'] as $type => $id) { |
|
64 | 3 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
65 | 3 | } |
|
66 | |||
67 | 3 | $this->configurePlugins($container, $config['plugins']); |
|
68 | 3 | $this->configureClients($container, $config); |
|
69 | 3 | $this->configureAutoDiscoveryClients($container, $config); |
|
70 | 3 | } |
|
71 | |||
72 | /** |
||
73 | * Configure client services. |
||
74 | * |
||
75 | * @param ContainerBuilder $container |
||
76 | * @param array $config |
||
77 | */ |
||
78 | 3 | private function configureClients(ContainerBuilder $container, array $config) |
|
79 | { |
||
80 | // If we have a client named 'default' |
||
81 | 3 | $first = isset($config['clients']['default']) ? 'default' : null; |
|
82 | |||
83 | 3 | foreach ($config['clients'] as $name => $arguments) { |
|
84 | if ($first === null) { |
||
85 | // Save the name of the first configurated client. |
||
86 | $first = $name; |
||
87 | } |
||
88 | |||
89 | $this->configureClient($container, $name, $arguments, $config['_inject_collector_plugin']); |
||
90 | 3 | } |
|
91 | |||
92 | // If we have clients configured |
||
93 | 3 | if ($first !== null) { |
|
94 | if ($first !== 'default') { |
||
95 | // Alias the first client to httplug.client.default |
||
96 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
||
97 | } |
||
98 | 3 | } elseif (isset($config['_inject_collector_plugin'])) { |
|
99 | $serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'default'); |
||
100 | // No client was configured. Make sure to configure the auto discovery client with the PluginClient. |
||
101 | $container->register('httplug.client', PluginClient::class) |
||
102 | ->addArgument(new Reference('httplug.client.default')) |
||
103 | ->addArgument([]) |
||
104 | ->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @param ContainerBuilder $container |
||
110 | * @param array $config |
||
111 | */ |
||
112 | private function configurePlugins(ContainerBuilder $container, array $config) |
||
130 | |||
131 | /** |
||
132 | * @param string $name |
||
133 | * @param Definition $definition |
||
134 | * @param array $config |
||
135 | */ |
||
136 | private function configurePluginByName($name, Definition $definition, array $config) |
||
173 | |||
174 | /** |
||
175 | * @param ContainerBuilder $container |
||
176 | * @param array $config |
||
177 | */ |
||
178 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
208 | |||
209 | /** |
||
210 | * @param ContainerBuilder $container |
||
211 | * @param string $name |
||
212 | * @param array $arguments |
||
213 | * @param bool $enableCollector |
||
214 | */ |
||
215 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $enableCollector) |
||
264 | |||
265 | /** |
||
266 | * Create a new plugin service for this client. |
||
267 | * |
||
268 | * @param ContainerBuilder $container |
||
269 | * @param string $name |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | private function registerDebugPlugin(ContainerBuilder $container, $name) |
||
283 | |||
284 | /** |
||
285 | * Make sure we inject the debug plugin for clients found by auto discovery. |
||
286 | * |
||
287 | * @param ContainerBuilder $container |
||
288 | * @param array $config |
||
289 | */ |
||
290 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
310 | |||
311 | /** |
||
312 | * @param ContainerBuilder $container |
||
313 | * @param $name |
||
314 | * |
||
315 | * @return Reference |
||
316 | */ |
||
317 | private function registerAutoDiscoverableClientWithDebugPlugin(ContainerBuilder $container, $name) |
||
332 | } |
||
333 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: