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 | * {@inheritdoc} |
||
| 34 | */ |
||
| 35 | 9 | public function load(array $configs, ContainerBuilder $container) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Configure client services. |
||
| 82 | * |
||
| 83 | * @param ContainerBuilder $container |
||
| 84 | * @param array $config |
||
| 85 | * @param bool $profiling |
||
| 86 | */ |
||
| 87 | 9 | private function configureClients(ContainerBuilder $container, array $config, $profiling) |
|
| 88 | { |
||
| 89 | 9 | $first = null; |
|
| 90 | 9 | $clients = []; |
|
| 91 | |||
| 92 | 9 | foreach ($config['clients'] as $name => $arguments) { |
|
| 93 | 6 | if ($first === null) { |
|
| 94 | // Save the name of the first configured client. |
||
| 95 | 6 | $first = $name; |
|
| 96 | 6 | } |
|
| 97 | |||
| 98 | 6 | $this->configureClient($container, $name, $arguments, $this->isConfigEnabled($container, $config['profiling'])); |
|
| 99 | 6 | $clients[] = $name; |
|
| 100 | 9 | } |
|
| 101 | |||
| 102 | // If we have clients configured |
||
| 103 | 9 | if ($first !== null) { |
|
| 104 | // If we do not have a client named 'default' |
||
| 105 | 6 | if (!isset($config['clients']['default'])) { |
|
| 106 | // Alias the first client to httplug.client.default |
||
| 107 | 6 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
|
| 108 | 6 | } |
|
| 109 | 6 | } |
|
| 110 | |||
| 111 | 9 | if ($profiling) { |
|
| 112 | 6 | $container->getDefinition('httplug.collector.collector') |
|
| 113 | 6 | ->setArguments([$clients]) |
|
| 114 | ; |
||
| 115 | 6 | } |
|
| 116 | 9 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @param ContainerBuilder $container |
||
| 120 | * @param array $config |
||
| 121 | */ |
||
| 122 | 9 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
| 123 | { |
||
| 124 | 9 | if (!empty($config['authentication'])) { |
|
| 125 | $this->configureAuthentication($container, $config['authentication']); |
||
| 126 | } |
||
| 127 | 9 | unset($config['authentication']); |
|
| 128 | |||
| 129 | 9 | foreach ($config as $name => $pluginConfig) { |
|
| 130 | 9 | $pluginId = 'httplug.plugin.'.$name; |
|
| 131 | |||
| 132 | 9 | if ($this->isConfigEnabled($container, $pluginConfig)) { |
|
| 133 | 9 | $def = $container->getDefinition($pluginId); |
|
| 134 | 9 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
|
| 135 | 9 | } else { |
|
| 136 | 9 | $container->removeDefinition($pluginId); |
|
| 137 | } |
||
| 138 | 9 | } |
|
| 139 | 9 | } |
|
| 140 | |||
| 141 | /** |
||
| 142 | * @param string $name |
||
| 143 | * @param Definition $definition |
||
| 144 | * @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 | */ |
||
| 148 | 9 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
|
| 149 | { |
||
| 150 | switch ($name) { |
||
| 151 | 9 | case 'cache': |
|
| 152 | $definition |
||
| 153 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
| 154 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
| 155 | ->replaceArgument(2, $config['config']); |
||
| 156 | break; |
||
| 157 | 9 | case 'cookie': |
|
| 158 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
| 159 | break; |
||
| 160 | 9 | case 'decoder': |
|
| 161 | 9 | $definition->addArgument([ |
|
| 162 | 9 | 'use_content_encoding' => $config['use_content_encoding'], |
|
| 163 | 9 | ]); |
|
| 164 | 9 | break; |
|
| 165 | 9 | case 'history': |
|
| 166 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
| 167 | break; |
||
| 168 | 9 | case 'logger': |
|
| 169 | 9 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
| 170 | 9 | if (!empty($config['formatter'])) { |
|
| 171 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
| 172 | } |
||
| 173 | 9 | break; |
|
| 174 | 9 | case 'redirect': |
|
| 175 | 9 | $definition->addArgument([ |
|
| 176 | 9 | 'preserve_header' => $config['preserve_header'], |
|
| 177 | 9 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
| 178 | 9 | ]); |
|
| 179 | 9 | break; |
|
| 180 | 9 | case 'retry': |
|
| 181 | 9 | $definition->addArgument([ |
|
| 182 | 9 | 'retries' => $config['retry'], |
|
| 183 | 9 | ]); |
|
| 184 | 9 | break; |
|
| 185 | 9 | case 'stopwatch': |
|
| 186 | 9 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
|
| 187 | 9 | break; |
|
| 188 | |||
| 189 | /* client specific plugins */ |
||
| 190 | |||
| 191 | 3 | case 'add_host': |
|
| 192 | 3 | $uriService = $serviceId.'.host_uri'; |
|
| 193 | 3 | $this->createUri($container, $uriService, $config['host']); |
|
| 194 | 3 | $definition->replaceArgument(0, new Reference($uriService)); |
|
| 195 | 3 | $definition->replaceArgument(1, [ |
|
| 196 | 3 | 'replace' => $config['replace'], |
|
| 197 | 3 | ]); |
|
| 198 | 3 | break; |
|
| 199 | 1 | case 'header_append': |
|
| 200 | 1 | case 'header_defaults': |
|
| 201 | 1 | case 'header_set': |
|
| 202 | 1 | case 'header_remove': |
|
| 203 | 1 | $definition->replaceArgument(0, $config['headers']); |
|
| 204 | 1 | break; |
|
| 205 | |||
| 206 | default: |
||
| 207 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
| 208 | } |
||
| 209 | 9 | } |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param ContainerBuilder $container |
||
| 213 | * @param array $config |
||
| 214 | * |
||
| 215 | * @return array List of service ids for the authentication plugins. |
||
| 216 | */ |
||
| 217 | 3 | private function configureAuthentication(ContainerBuilder $container, array $config, $servicePrefix = 'httplug.plugin.authentication') |
|
| 218 | { |
||
| 219 | 3 | $pluginServices = []; |
|
| 220 | |||
| 221 | 3 | foreach ($config as $name => $values) { |
|
| 222 | 3 | $authServiceKey = sprintf($servicePrefix.'.%s.auth', $name); |
|
| 223 | 3 | switch ($values['type']) { |
|
| 224 | 3 | case 'bearer': |
|
| 225 | $container->register($authServiceKey, Bearer::class) |
||
| 226 | ->addArgument($values['token']); |
||
| 227 | break; |
||
| 228 | 3 | case 'basic': |
|
| 229 | $container->register($authServiceKey, BasicAuth::class) |
||
| 230 | 3 | ->addArgument($values['username']) |
|
| 231 | 3 | ->addArgument($values['password']); |
|
| 232 | 3 | break; |
|
| 233 | case 'wsse': |
||
| 234 | $container->register($authServiceKey, Wsse::class) |
||
| 235 | ->addArgument($values['username']) |
||
| 236 | ->addArgument($values['password']); |
||
| 237 | break; |
||
| 238 | case 'service': |
||
| 239 | $authServiceKey = $values['service']; |
||
| 240 | break; |
||
| 241 | default: |
||
| 242 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
| 243 | 3 | } |
|
| 244 | |||
| 245 | 3 | $pluginServiceKey = $servicePrefix.'.'.$name; |
|
| 246 | $container->register($pluginServiceKey, AuthenticationPlugin::class) |
||
| 247 | ->addArgument(new Reference($authServiceKey)) |
||
| 248 | ; |
||
| 249 | $pluginServices[] = $pluginServiceKey; |
||
| 250 | } |
||
| 251 | |||
| 252 | return $pluginServices; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @param ContainerBuilder $container |
||
| 257 | * @param string $clientName |
||
| 258 | * @param array $arguments |
||
| 259 | * @param bool $profiling |
||
| 260 | */ |
||
| 261 | private function configureClient(ContainerBuilder $container, $clientName, array $arguments, $profiling) |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Create a URI object with the default URI factory. |
||
| 360 | * |
||
| 361 | * @param ContainerBuilder $container |
||
| 362 | * @param string $serviceId Name of the private service to create |
||
| 363 | * @param string $uri String representation of the URI |
||
| 364 | */ |
||
| 365 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
| 377 | * by finding a client using auto discovery. |
||
| 378 | * |
||
| 379 | * @param ContainerBuilder $container |
||
| 380 | * @param array $config |
||
| 381 | */ |
||
| 382 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Find a client with auto discovery and return a service Reference to it. |
||
| 423 | * |
||
| 424 | * @param ContainerBuilder $container |
||
| 425 | * @param string $name |
||
| 426 | * @param callable $factory |
||
| 427 | * @param bool $profiling |
||
| 428 | * |
||
| 429 | * @return string service id |
||
| 430 | */ |
||
| 431 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * {@inheritdoc} |
||
| 456 | */ |
||
| 457 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
| 461 | } |
||
| 462 |
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: