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 |
||
24 | class HttplugExtension extends Extension |
||
25 | { |
||
26 | /** |
||
27 | * {@inheritdoc} |
||
28 | */ |
||
29 | 3 | public function load(array $configs, ContainerBuilder $container) |
|
30 | { |
||
31 | 3 | $configuration = $this->getConfiguration($configs, $container); |
|
32 | 3 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
33 | |||
34 | 3 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
35 | |||
36 | 3 | $loader->load('services.xml'); |
|
37 | 3 | $loader->load('plugins.xml'); |
|
38 | |||
39 | 3 | $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug'); |
|
40 | 3 | if ($enabled) { |
|
41 | $loader->load('data-collector.xml'); |
||
42 | $config['_inject_collector_plugin'] = true; |
||
43 | |||
44 | if (!empty($config['toolbar']['formatter'])) { |
||
45 | $container->getDefinition('httplug.collector.message_journal') |
||
46 | ->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | 3 | foreach ($config['classes'] as $service => $class) { |
|
51 | 3 | if (!empty($class)) { |
|
52 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
53 | 1 | } |
|
54 | 3 | } |
|
55 | |||
56 | 3 | foreach ($config['main_alias'] as $type => $id) { |
|
57 | 3 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
58 | 3 | } |
|
59 | |||
60 | 3 | $this->configurePlugins($container, $config['plugins']); |
|
61 | 3 | $this->configureClients($container, $config); |
|
62 | 3 | } |
|
63 | |||
64 | /** |
||
65 | * Configure client services. |
||
66 | * |
||
67 | * @param ContainerBuilder $container |
||
68 | * @param array $config |
||
69 | */ |
||
70 | 3 | private function configureClients(ContainerBuilder $container, array $config) |
|
71 | { |
||
72 | 3 | $first = isset($config['clients']['default']) ? 'default' : null; |
|
73 | 3 | foreach ($config['clients'] as $name => $arguments) { |
|
74 | if ($first === null) { |
||
75 | $first = $name; |
||
76 | } |
||
77 | |||
78 | if (isset($config['_inject_collector_plugin'])) { |
||
79 | array_unshift($arguments['plugins'], 'httplug.collector.history_plugin'); |
||
80 | } |
||
81 | |||
82 | $this->configureClient($container, $name, $arguments); |
||
83 | 3 | } |
|
84 | |||
85 | // If we have clients configured |
||
86 | 3 | if ($first !== null) { |
|
87 | if ($first !== 'default') { |
||
88 | // Alias the first client to httplug.client.default |
||
89 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
||
90 | } |
||
91 | 3 | } elseif (isset($config['_inject_collector_plugin'])) { |
|
92 | // No client was configured. Make sure to inject history plugin to the auto discovery client. |
||
93 | $container->register('httplug.client', PluginClient::class) |
||
94 | ->addArgument(new Reference('httplug.client.default')) |
||
95 | ->addArgument([new Reference('httplug.collector.history_plugin')]); |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @param ContainerBuilder $container |
||
101 | * @param array $config |
||
102 | */ |
||
103 | private function configurePlugins(ContainerBuilder $container, array $config) |
||
121 | |||
122 | /** |
||
123 | * @param string $name |
||
124 | * @param Definition $definition |
||
125 | * @param array $config |
||
126 | */ |
||
127 | private function configurePluginByName($name, Definition $definition, array $config) |
||
164 | |||
165 | /** |
||
166 | * @param ContainerBuilder $container |
||
167 | * @param array $config |
||
168 | */ |
||
169 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
199 | |||
200 | /** |
||
201 | * @param ContainerBuilder $container |
||
202 | * @param string $name |
||
203 | * @param array $arguments |
||
204 | */ |
||
205 | private function configureClient(ContainerBuilder $container, $name, array $arguments) |
||
250 | } |
||
251 |
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: