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 |
||
30 | class HttplugExtension extends Extension |
||
31 | { |
||
32 | /** |
||
33 | 8 | * {@inheritdoc} |
|
34 | */ |
||
35 | 8 | public function load(array $configs, ContainerBuilder $container) |
|
79 | |||
80 | /** |
||
81 | * Configure client services. |
||
82 | * |
||
83 | * @param ContainerBuilder $container |
||
84 | 8 | * @param array $config |
|
85 | */ |
||
86 | 8 | private function configureClients(ContainerBuilder $container, array $config) |
|
108 | |||
109 | /** |
||
110 | * @param ContainerBuilder $container |
||
111 | 8 | * @param array $config |
|
112 | */ |
||
113 | 8 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
114 | { |
||
115 | if (!empty($config['authentication'])) { |
||
116 | 8 | $this->configureAuthentication($container, $config['authentication']); |
|
117 | } |
||
118 | 8 | unset($config['authentication']); |
|
119 | 8 | ||
120 | foreach ($config as $name => $pluginConfig) { |
||
121 | 8 | $pluginId = 'httplug.plugin.'.$name; |
|
122 | 8 | ||
123 | 8 | if ($pluginConfig['enabled']) { |
|
124 | 8 | $def = $container->getDefinition($pluginId); |
|
125 | 8 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
|
126 | } else { |
||
127 | 8 | $container->removeDefinition($pluginId); |
|
128 | 8 | } |
|
129 | } |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * @param string $name |
||
134 | * @param Definition $definition |
||
135 | 8 | * @param array $config |
|
136 | * @param ContainerBuilder $container In case we need to add additional services for this plugin |
||
137 | * @param string $serviceId Service id of the plugin, in case we need to add additional services for this plugin. |
||
138 | 8 | */ |
|
139 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
||
140 | { |
||
141 | switch ($name) { |
||
142 | case 'cache': |
||
143 | $definition |
||
144 | 8 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
|
145 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
146 | ->replaceArgument(2, $config['config']); |
||
147 | 8 | break; |
|
148 | 8 | case 'cookie': |
|
149 | 8 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
|
150 | 8 | break; |
|
151 | case 'decoder': |
||
152 | $definition->addArgument([ |
||
153 | 8 | 'use_content_encoding' => $config['use_content_encoding'], |
|
154 | 8 | ]); |
|
155 | 8 | break; |
|
156 | case 'history': |
||
157 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
158 | 8 | break; |
|
159 | 8 | case 'logger': |
|
160 | $definition->replaceArgument(0, new Reference($config['logger'])); |
||
161 | 8 | if (!empty($config['formatter'])) { |
|
162 | 8 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
|
163 | 8 | } |
|
164 | 8 | break; |
|
165 | 8 | case 'redirect': |
|
166 | 8 | $definition->addArgument([ |
|
167 | 8 | 'preserve_header' => $config['preserve_header'], |
|
168 | 8 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
169 | 8 | ]); |
|
170 | break; |
||
171 | 8 | case 'retry': |
|
172 | $definition->addArgument([ |
||
173 | 'retries' => $config['retry'], |
||
174 | ]); |
||
175 | break; |
||
176 | case 'stopwatch': |
||
177 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
||
178 | break; |
||
179 | |||
180 | /* client specific plugins */ |
||
181 | |||
182 | case 'add_host': |
||
183 | $uriService = $serviceId.'.host_uri'; |
||
184 | $this->createUri($container, $uriService, $config['host']); |
||
185 | $definition->replaceArgument(0, new Reference($uriService)); |
||
186 | $definition->replaceArgument(1, [ |
||
187 | 'replace' => $config['replace'], |
||
188 | ]); |
||
189 | break; |
||
190 | |||
191 | default: |
||
192 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
193 | } |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @param ContainerBuilder $container |
||
198 | * @param array $config |
||
199 | */ |
||
200 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
201 | { |
||
202 | foreach ($config as $name => $values) { |
||
203 | $authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name); |
||
204 | switch ($values['type']) { |
||
205 | case 'bearer': |
||
206 | $container->register($authServiceKey, Bearer::class) |
||
207 | ->addArgument($values['token']); |
||
208 | break; |
||
209 | case 'basic': |
||
210 | $container->register($authServiceKey, BasicAuth::class) |
||
211 | ->addArgument($values['username']) |
||
212 | ->addArgument($values['password']); |
||
213 | break; |
||
214 | case 'wsse': |
||
215 | $container->register($authServiceKey, Wsse::class) |
||
216 | ->addArgument($values['username']) |
||
217 | ->addArgument($values['password']); |
||
218 | break; |
||
219 | case 'service': |
||
220 | $authServiceKey = $values['service']; |
||
221 | break; |
||
222 | default: |
||
223 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
224 | } |
||
225 | |||
226 | $container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class) |
||
227 | ->addArgument(new Reference($authServiceKey)); |
||
228 | } |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * @param ContainerBuilder $container |
||
233 | * @param string $name |
||
234 | * @param array $arguments |
||
235 | * @param bool $profiling |
||
236 | */ |
||
237 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling) |
||
238 | { |
||
239 | $serviceId = 'httplug.client.'.$name; |
||
240 | |||
241 | $pluginClientOptions = []; |
||
242 | |||
243 | if ($profiling) { |
||
244 | if (!in_array('httplug.plugin.stopwatch', $arguments['plugins'])) { |
||
245 | // Add the stopwatch plugin |
||
246 | array_unshift($arguments['plugins'], [ |
||
247 | 'reference' => [ |
||
248 | 'id' => 'httplug.plugin.stopwatch', |
||
249 | ], |
||
250 | ]); |
||
251 | } |
||
252 | |||
253 | // Tell the plugin journal what plugins we used |
||
254 | $container |
||
255 | ->getDefinition('httplug.collector.plugin_journal') |
||
256 | ->addMethodCall('setPlugins', [$name, $arguments['plugins']]) |
||
257 | ; |
||
258 | |||
259 | $debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId); |
||
260 | |||
261 | $pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)]; |
||
262 | } |
||
263 | |||
264 | $plugins = []; |
||
265 | foreach ($arguments['plugins'] as $plugin) { |
||
266 | list($name, $pluginConfig) = each($plugin); |
||
267 | if ('reference' === $name) { |
||
268 | $plugins[] = $pluginConfig['id']; |
||
269 | } elseif ('authentication' === $name) { |
||
270 | // TODO handle custom authentication |
||
271 | } else { |
||
272 | $pluginServiceId = $serviceId.'.plugin.'.$name; |
||
273 | $def = clone $container->getDefinition('httplug.plugin'.'.'.$name); |
||
274 | $def->setAbstract(false); |
||
275 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginServiceId); |
||
276 | $container->setDefinition($pluginServiceId, $def); |
||
277 | $plugins[] = $pluginServiceId; |
||
278 | } |
||
279 | } |
||
280 | |||
281 | $container |
||
282 | ->register($serviceId, DummyClient::class) |
||
283 | ->setFactory([PluginClientFactory::class, 'createPluginClient']) |
||
284 | ->addArgument( |
||
285 | array_map( |
||
286 | function ($id) { |
||
287 | return new Reference($id); |
||
288 | }, |
||
289 | $plugins |
||
290 | ) |
||
291 | ) |
||
292 | ->addArgument(new Reference($arguments['factory'])) |
||
293 | ->addArgument($arguments['config']) |
||
294 | ->addArgument($pluginClientOptions) |
||
295 | ; |
||
296 | |||
297 | |||
298 | /* |
||
299 | * Decorate the client with clients from client-common |
||
300 | */ |
||
301 | View Code Duplication | if ($arguments['flexible_client']) { |
|
302 | $container |
||
303 | ->register($serviceId.'.flexible', FlexibleHttpClient::class) |
||
304 | ->addArgument(new Reference($serviceId.'.flexible.inner')) |
||
305 | ->setPublic(false) |
||
306 | ->setDecoratedService($serviceId) |
||
307 | ; |
||
308 | } |
||
309 | |||
310 | View Code Duplication | if ($arguments['http_methods_client']) { |
|
311 | $container |
||
312 | ->register($serviceId.'.http_methods', HttpMethodsClient::class) |
||
313 | ->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')]) |
||
314 | ->setPublic(false) |
||
315 | ->setDecoratedService($serviceId) |
||
316 | ; |
||
317 | } |
||
318 | |||
319 | View Code Duplication | if ($arguments['batch_client']) { |
|
320 | $container |
||
321 | ->register($serviceId.'.batch_client', BatchClient::class) |
||
322 | ->setArguments([new Reference($serviceId.'.batch_client.inner')]) |
||
323 | ->setPublic(false) |
||
324 | ->setDecoratedService($serviceId) |
||
325 | ; |
||
326 | } |
||
327 | } |
||
328 | |||
329 | /** |
||
330 | * Create a URI object with the default URI factory. |
||
331 | * |
||
332 | * @param ContainerBuilder $container |
||
333 | * @param string $serviceId Name of the private service to create |
||
334 | * @param string $uri String representation of the URI |
||
335 | */ |
||
336 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
345 | |||
346 | /** |
||
347 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
348 | * by finding a client using auto discovery. |
||
349 | * |
||
350 | * @param ContainerBuilder $container |
||
351 | * @param array $config |
||
352 | */ |
||
353 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
391 | |||
392 | /** |
||
393 | * Find a client with auto discovery and return a service Reference to it. |
||
394 | * |
||
395 | * @param ContainerBuilder $container |
||
396 | * @param string $name |
||
397 | * @param callable $factory |
||
398 | * @param bool $profiling |
||
399 | * |
||
400 | * @return string service id |
||
401 | */ |
||
402 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
428 | |||
429 | /** |
||
430 | * Create a new plugin service for this client. |
||
431 | * |
||
432 | * @param ContainerBuilder $container |
||
433 | * @param string $serviceId |
||
434 | * |
||
435 | * @return string |
||
436 | */ |
||
437 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
450 | |||
451 | /** |
||
452 | * {@inheritdoc} |
||
453 | */ |
||
454 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
458 | } |
||
459 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: