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 |
||
25 | class HttplugExtension extends Extension |
||
26 | { |
||
27 | /** |
||
28 | * {@inheritdoc} |
||
29 | */ |
||
30 | 3 | public function load(array $configs, ContainerBuilder $container) |
|
31 | { |
||
32 | 3 | $configuration = $this->getConfiguration($configs, $container); |
|
33 | 3 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
34 | |||
35 | 3 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
36 | |||
37 | 3 | $loader->load('services.xml'); |
|
38 | 3 | $loader->load('plugins.xml'); |
|
39 | |||
40 | 3 | $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug'); |
|
41 | 3 | if ($enabled) { |
|
42 | $loader->load('data-collector.xml'); |
||
43 | $config['_inject_collector_plugin'] = true; |
||
44 | |||
45 | if (!empty($config['toolbar']['formatter'])) { |
||
46 | // Add custom formatter |
||
47 | $container->getDefinition('httplug.collector.debug_collector') |
||
48 | ->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
||
49 | } |
||
50 | |||
51 | $container->getDefinition('httplug.formatter.full_http_message') |
||
52 | ->addArgument($config['toolbar']['captured_body_length']); |
||
53 | } |
||
54 | |||
55 | 3 | foreach ($config['classes'] as $service => $class) { |
|
56 | 3 | if (!empty($class)) { |
|
57 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
58 | 1 | } |
|
59 | 3 | } |
|
60 | |||
61 | // Set main aliases |
||
62 | 3 | foreach ($config['main_alias'] as $type => $id) { |
|
63 | 3 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
64 | 3 | } |
|
65 | |||
66 | 3 | $this->configurePlugins($container, $config['plugins']); |
|
67 | 3 | $this->configureClients($container, $config); |
|
68 | 3 | } |
|
69 | |||
70 | /** |
||
71 | * Configure client services. |
||
72 | * |
||
73 | * @param ContainerBuilder $container |
||
74 | * @param array $config |
||
75 | */ |
||
76 | 3 | private function configureClients(ContainerBuilder $container, array $config) |
|
77 | { |
||
78 | // If we have a client named 'default' |
||
79 | 3 | $first = isset($config['clients']['default']) ? 'default' : null; |
|
80 | |||
81 | 3 | foreach ($config['clients'] as $name => $arguments) { |
|
82 | if ($first === null) { |
||
83 | // Save the name of the first configurated client. |
||
84 | $first = $name; |
||
85 | } |
||
86 | |||
87 | $this->configureClient($container, $name, $arguments, $config['_inject_collector_plugin']); |
||
88 | 3 | } |
|
89 | |||
90 | // If we have clients configured |
||
91 | 3 | if ($first !== null) { |
|
92 | if ($first !== 'default') { |
||
93 | // Alias the first client to httplug.client.default |
||
94 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
||
95 | } |
||
96 | 3 | } elseif (isset($config['_inject_collector_plugin'])) { |
|
97 | $serviceIdDebugPlugin = $this->registerDebugPlugin($container, 'default'); |
||
98 | // No client was configured. Make sure to configure the auto discovery client with the PluginClient. |
||
99 | $container->register('httplug.client', PluginClient::class) |
||
100 | ->addArgument(new Reference('httplug.client.default')) |
||
101 | ->addArgument([]) |
||
102 | ->addArgument(['debug_plugins' => [new Reference($serviceIdDebugPlugin)]]); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param ContainerBuilder $container |
||
108 | * @param array $config |
||
109 | */ |
||
110 | private function configurePlugins(ContainerBuilder $container, array $config) |
||
128 | |||
129 | /** |
||
130 | * @param string $name |
||
131 | * @param Definition $definition |
||
132 | * @param array $config |
||
133 | */ |
||
134 | private function configurePluginByName($name, Definition $definition, array $config) |
||
171 | |||
172 | /** |
||
173 | * @param ContainerBuilder $container |
||
174 | * @param array $config |
||
175 | */ |
||
176 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
206 | |||
207 | /** |
||
208 | * @param ContainerBuilder $container |
||
209 | * @param string $name |
||
210 | * @param array $arguments |
||
211 | * @param bool $enableCollector |
||
212 | */ |
||
213 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $enableCollector) |
||
262 | |||
263 | /** |
||
264 | * Create a new plugin service for this client. |
||
265 | * |
||
266 | * @param ContainerBuilder $container |
||
267 | * @param string $name |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | private function registerDebugPlugin(ContainerBuilder $container, $name) |
||
281 | } |
||
282 |
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: