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 | * @param string $idPrefix Start of service id for these plugins. |
||
113 | 8 | */ |
|
114 | private function configurePlugins(ContainerBuilder $container, array $config, $idPrefix = 'httplug.plugin') |
||
115 | { |
||
116 | 8 | $sharedPluginPrefix = 'httplug.plugin'; |
|
117 | $shared = $sharedPluginPrefix === $idPrefix; |
||
118 | 8 | if (!empty($config['authentication'])) { |
|
119 | 8 | // TODO: handle extra auth plugin on client |
|
120 | $this->configureAuthentication($container, $config['authentication']); |
||
121 | 8 | } |
|
122 | 8 | unset($config['authentication']); |
|
123 | 8 | ||
124 | 8 | foreach ($config as $name => $pluginConfig) { |
|
125 | 8 | $pluginId = $idPrefix.'.'.$name; |
|
126 | |||
127 | 8 | if ($pluginConfig['enabled']) { |
|
128 | 8 | $def = $container->getDefinition($sharedPluginPrefix.'.'.$name); |
|
129 | if (!$shared) { |
||
130 | $def = clone $def; |
||
131 | $def->setAbstract(false); |
||
132 | $container->setDefinition($pluginId, $def); |
||
133 | } |
||
134 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
||
135 | 8 | } elseif ($shared) { |
|
136 | $container->removeDefinition($pluginId); |
||
137 | } |
||
138 | 8 | } |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param string $name |
||
143 | * @param Definition $definition |
||
144 | 8 | * @param array $config |
|
145 | * @param ContainerBuilder $container In case we need to add additional services for this plugin |
||
146 | * @param string $serviceId Service id of the plugin, in case we need to add additional services for this plugin. |
||
147 | 8 | */ |
|
148 | 8 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
|
149 | 8 | { |
|
150 | 8 | switch ($name) { |
|
151 | case 'cache': |
||
152 | $definition |
||
153 | 8 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
|
154 | 8 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
|
155 | 8 | ->replaceArgument(2, $config['config']); |
|
156 | break; |
||
157 | case 'cookie': |
||
158 | 8 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
|
159 | 8 | break; |
|
160 | case 'decoder': |
||
161 | 8 | $definition->addArgument([ |
|
162 | 8 | 'use_content_encoding' => $config['use_content_encoding'], |
|
163 | 8 | ]); |
|
164 | 8 | break; |
|
165 | 8 | case 'history': |
|
166 | 8 | $definition->replaceArgument(0, new Reference($config['journal'])); |
|
167 | 8 | break; |
|
168 | 8 | case 'logger': |
|
169 | 8 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
170 | if (!empty($config['formatter'])) { |
||
171 | 8 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
|
172 | } |
||
173 | break; |
||
174 | case 'redirect': |
||
175 | $definition->addArgument([ |
||
176 | 'preserve_header' => $config['preserve_header'], |
||
177 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
||
178 | ]); |
||
179 | break; |
||
180 | case 'retry': |
||
181 | $definition->addArgument([ |
||
182 | 'retries' => $config['retry'], |
||
183 | ]); |
||
184 | break; |
||
185 | case 'stopwatch': |
||
186 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
||
187 | break; |
||
188 | |||
189 | // client specific plugins |
||
190 | case 'add_host': |
||
191 | $uriService = $serviceId.'.host_uri'; |
||
192 | $this->createUri($container, $uriService, $config['host']); |
||
193 | $definition->replaceArgument(0, new Reference($uriService)); |
||
194 | $definition->replaceArgument(1, [ |
||
195 | 'replace' => $config['replace'], |
||
196 | ]); |
||
197 | break; |
||
198 | |||
199 | default: |
||
200 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
201 | } |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * @param ContainerBuilder $container |
||
206 | * @param array $config |
||
207 | */ |
||
208 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
238 | |||
239 | /** |
||
240 | * @param ContainerBuilder $container |
||
241 | * @param string $name |
||
242 | * @param array $arguments |
||
243 | * @param bool $profiling |
||
244 | */ |
||
245 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling) |
||
246 | { |
||
247 | $serviceId = 'httplug.client.'.$name; |
||
248 | |||
249 | $plugins = $arguments['plugins']; |
||
250 | $pluginClientOptions = []; |
||
251 | |||
252 | if ($profiling) { |
||
253 | if (!in_array('httplug.plugin.stopwatch', $arguments['plugins'])) { |
||
254 | // Add the stopwatch plugin |
||
255 | array_unshift($arguments['plugins'], 'httplug.plugin.stopwatch'); |
||
256 | } |
||
257 | |||
258 | // Tell the plugin journal what plugins we used |
||
259 | $container |
||
260 | ->getDefinition('httplug.collector.plugin_journal') |
||
261 | ->addMethodCall('setPlugins', [$name, $arguments['plugins']]) |
||
262 | ; |
||
263 | |||
264 | $debugPluginServiceId = $this->registerDebugPlugin($container, $serviceId); |
||
265 | |||
266 | $pluginClientOptions['debug_plugins'] = [new Reference($debugPluginServiceId)]; |
||
267 | } |
||
268 | |||
269 | if (array_key_exists('extra_plugins', $arguments)) { |
||
270 | $this->configurePlugins($container, $arguments['extra_plugins'], $serviceId.'.plugin'); |
||
271 | |||
272 | // add to end of plugins list unless explicitly configured |
||
273 | foreach ($arguments['extra_plugins'] as $name => $config) { |
||
274 | if (!in_array($serviceId.'.plugin.'.$name, $plugins)) { |
||
275 | $plugins[] = $serviceId.'.plugin.'.$name; |
||
276 | } |
||
277 | } |
||
278 | } |
||
279 | |||
280 | $container |
||
281 | ->register($serviceId, DummyClient::class) |
||
282 | ->setFactory([PluginClientFactory::class, 'createPluginClient']) |
||
283 | ->addArgument( |
||
284 | array_map( |
||
285 | function ($id) { |
||
286 | return new Reference($id); |
||
287 | }, |
||
288 | $plugins |
||
289 | ) |
||
290 | ) |
||
291 | ->addArgument(new Reference($arguments['factory'])) |
||
292 | ->addArgument($arguments['config']) |
||
293 | ->addArgument($pluginClientOptions) |
||
294 | ; |
||
295 | |||
296 | |||
297 | /* |
||
298 | * Decorate the client with clients from client-common |
||
299 | */ |
||
300 | View Code Duplication | if ($arguments['flexible_client']) { |
|
301 | $container |
||
302 | ->register($serviceId.'.flexible', FlexibleHttpClient::class) |
||
303 | ->addArgument(new Reference($serviceId.'.flexible.inner')) |
||
304 | ->setPublic(false) |
||
305 | ->setDecoratedService($serviceId) |
||
306 | ; |
||
307 | } |
||
308 | |||
309 | View Code Duplication | if ($arguments['http_methods_client']) { |
|
310 | $container |
||
311 | ->register($serviceId.'.http_methods', HttpMethodsClient::class) |
||
312 | ->setArguments([new Reference($serviceId.'.http_methods.inner'), new Reference('httplug.message_factory')]) |
||
313 | ->setPublic(false) |
||
314 | ->setDecoratedService($serviceId) |
||
315 | ; |
||
316 | } |
||
317 | |||
318 | View Code Duplication | if ($arguments['batch_client']) { |
|
319 | $container |
||
320 | ->register($serviceId.'.batch_client', BatchClient::class) |
||
321 | ->setArguments([new Reference($serviceId.'.batch_client.inner')]) |
||
322 | ->setPublic(false) |
||
323 | ->setDecoratedService($serviceId) |
||
324 | ; |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Create a URI object with the default URI factory. |
||
330 | * |
||
331 | * @param ContainerBuilder $container |
||
332 | * @param string $serviceId Name of the private service to create |
||
333 | * @param string $uri String representation of the URI |
||
334 | */ |
||
335 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
336 | { |
||
337 | $container |
||
338 | ->register($serviceId, UriInterface::class) |
||
339 | ->setPublic(false) |
||
340 | ->setFactory([new Reference('httplug.uri_factory'), 'createUri']) |
||
341 | ->addArgument($uri) |
||
342 | ; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
347 | * by finding a client using auto discovery. |
||
348 | * |
||
349 | * @param ContainerBuilder $container |
||
350 | * @param array $config |
||
351 | */ |
||
352 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
390 | |||
391 | /** |
||
392 | * Find a client with auto discovery and return a service Reference to it. |
||
393 | * |
||
394 | * @param ContainerBuilder $container |
||
395 | * @param string $name |
||
396 | * @param callable $factory |
||
397 | * @param bool $profiling |
||
398 | * |
||
399 | * @return string service id |
||
400 | */ |
||
401 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
427 | |||
428 | /** |
||
429 | * Create a new plugin service for this client. |
||
430 | * |
||
431 | * @param ContainerBuilder $container |
||
432 | * @param string $serviceId |
||
433 | * |
||
434 | * @return string |
||
435 | */ |
||
436 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
449 | |||
450 | /** |
||
451 | * {@inheritdoc} |
||
452 | */ |
||
453 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
457 | } |
||
458 |
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: