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 |
||
22 | class HttplugExtension extends Extension |
||
23 | { |
||
24 | /** |
||
25 | * {@inheritdoc} |
||
26 | */ |
||
27 | 3 | public function load(array $configs, ContainerBuilder $container) |
|
28 | { |
||
29 | 3 | $configuration = $this->getConfiguration($configs, $container); |
|
30 | 3 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
31 | |||
32 | 3 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
33 | |||
34 | 3 | $loader->load('services.xml'); |
|
35 | 3 | $loader->load('plugins.xml'); |
|
36 | |||
37 | 3 | $enabled = is_bool($config['toolbar']['enabled']) ? $config['toolbar']['enabled'] : $container->hasParameter('kernel.debug') && $container->getParameter('kernel.debug'); |
|
38 | 3 | if ($enabled) { |
|
39 | $loader->load('data-collector.xml'); |
||
40 | $config['_inject_collector_plugin'] = true; |
||
41 | |||
42 | if (!empty($config['toolbar']['formatter'])) { |
||
43 | $container->getDefinition('httplug.collector.message_journal') |
||
44 | ->replaceArgument(0, new Reference($config['toolbar']['formatter'])); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | 3 | foreach ($config['classes'] as $service => $class) { |
|
49 | 3 | if (!empty($class)) { |
|
50 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
51 | 1 | } |
|
52 | 3 | } |
|
53 | |||
54 | 3 | foreach ($config['main_alias'] as $type => $id) { |
|
55 | 3 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
56 | 3 | } |
|
57 | |||
58 | 3 | $this->configurePlugins($container, $config['plugins']); |
|
59 | 3 | $this->configureClients($container, $config); |
|
60 | 3 | } |
|
61 | |||
62 | /** |
||
63 | * Configure client services. |
||
64 | * |
||
65 | * @param ContainerBuilder $container |
||
66 | * @param array $config |
||
67 | */ |
||
68 | 3 | private function configureClients(ContainerBuilder $container, array $config) |
|
108 | |||
109 | /** |
||
110 | * @param ContainerBuilder $container |
||
111 | * @param array $config |
||
112 | */ |
||
113 | private function configurePlugins(ContainerBuilder $container, array $config) |
||
114 | { |
||
115 | if (!empty($config['authentication'])) { |
||
116 | $this->configureAuthentication($container, $config['authentication']); |
||
117 | } |
||
118 | unset($config['authentication']); |
||
119 | |||
120 | foreach ($config as $name => $pluginConfig) { |
||
121 | $pluginId = 'httplug.plugin.'.$name; |
||
122 | |||
123 | if ($pluginConfig['enabled']) { |
||
124 | $def = $container->getDefinition($pluginId); |
||
125 | $this->configurePluginByName($name, $def, $pluginConfig); |
||
126 | } else { |
||
127 | $container->removeDefinition($pluginId); |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $name |
||
134 | * @param Definition $definition |
||
135 | * @param array $config |
||
136 | */ |
||
137 | private function configurePluginByName($name, Definition $definition, array $config) |
||
138 | { |
||
139 | switch ($name) { |
||
140 | View Code Duplication | case 'cache': |
|
141 | if (class_exists('Http\Client\Common\Plugin\CachePlugin')) { |
||
142 | $definition->setClass('Http\Client\Common\Plugin\CachePlugin'); |
||
143 | } |
||
144 | $definition |
||
145 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
146 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
147 | ->replaceArgument(2, $config['config']); |
||
148 | break; |
||
149 | case 'cookie': |
||
150 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
151 | break; |
||
152 | case 'decoder': |
||
153 | $definition->addArgument($config['use_content_encoding']); |
||
154 | break; |
||
155 | case 'history': |
||
156 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
157 | break; |
||
158 | View Code Duplication | case 'logger': |
|
159 | if (class_exists('Http\Client\Common\Plugin\LoggerPlugin')) { |
||
160 | $definition->setClass('Http\Client\Common\Plugin\LoggerPlugin'); |
||
161 | } |
||
162 | $definition->replaceArgument(0, new Reference($config['logger'])); |
||
163 | if (!empty($config['formatter'])) { |
||
164 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
165 | } |
||
166 | break; |
||
167 | case 'redirect': |
||
168 | $definition |
||
169 | ->addArgument($config['preserve_header']) |
||
170 | ->addArgument($config['use_default_for_multiple']); |
||
171 | break; |
||
172 | case 'retry': |
||
173 | $definition->addArgument($config['retry']); |
||
174 | break; |
||
175 | case 'stopwatch': |
||
176 | if (class_exists('Http\Client\Common\Plugin\StopwatchPlugin')) { |
||
177 | $definition->setClass('Http\Client\Common\Plugin\StopwatchPlugin'); |
||
178 | } |
||
179 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
||
180 | break; |
||
181 | } |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param ContainerBuilder $container |
||
186 | * @param array $config |
||
187 | */ |
||
188 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
218 | } |
||
219 |
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: