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