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 |
||
| 32 | class HttplugExtension extends Extension |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * {@inheritdoc} |
||
| 36 | */ |
||
| 37 | 10 | public function load(array $configs, ContainerBuilder $container) |
|
| 38 | { |
||
| 39 | 10 | $configuration = $this->getConfiguration($configs, $container); |
|
| 40 | 10 | $config = $this->processConfiguration($configuration, $configs); |
|
|
1 ignored issue
–
show
|
|||
| 41 | |||
| 42 | 10 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 43 | |||
| 44 | 10 | $loader->load('services.xml'); |
|
| 45 | 10 | $loader->load('plugins.xml'); |
|
| 46 | |||
| 47 | // Register default services |
||
| 48 | 10 | foreach ($config['classes'] as $service => $class) { |
|
| 49 | 10 | if (!empty($class)) { |
|
| 50 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
| 51 | 1 | } |
|
| 52 | 10 | } |
|
| 53 | |||
| 54 | // Set main aliases |
||
| 55 | 10 | foreach ($config['main_alias'] as $type => $id) { |
|
| 56 | 10 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
| 57 | 10 | } |
|
| 58 | |||
| 59 | // Configure toolbar |
||
| 60 | 10 | if ($this->isConfigEnabled($container, $config['profiling'])) { |
|
| 61 | 7 | $loader->load('data-collector.xml'); |
|
| 62 | |||
| 63 | 7 | if (!empty($config['profiling']['formatter'])) { |
|
| 64 | // Add custom formatter |
||
| 65 | $container |
||
| 66 | 1 | ->getDefinition('httplug.collector.formatter') |
|
| 67 | 1 | ->replaceArgument(0, new Reference($config['profiling']['formatter'])) |
|
| 68 | ; |
||
| 69 | 1 | } |
|
| 70 | |||
| 71 | $container |
||
| 72 | 7 | ->getDefinition('httplug.formatter.full_http_message') |
|
| 73 | 7 | ->addArgument($config['profiling']['captured_body_length']) |
|
| 74 | ; |
||
| 75 | 7 | } |
|
| 76 | |||
| 77 | 10 | $this->configureClients($container, $config, $this->isConfigEnabled($container, $config['profiling'])); |
|
| 78 | 10 | $this->configureSharedPlugins($container, $config['plugins']); // must be after clients, as clients.X.plugins might use plugins as templates that will be removed |
|
| 79 | 10 | $this->configureAutoDiscoveryClients($container, $config); |
|
| 80 | 10 | } |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Configure client services. |
||
| 84 | * |
||
| 85 | * @param ContainerBuilder $container |
||
| 86 | * @param array $config |
||
| 87 | * @param bool $profiling |
||
| 88 | */ |
||
| 89 | 10 | private function configureClients(ContainerBuilder $container, array $config, $profiling) |
|
| 90 | { |
||
| 91 | 10 | $first = null; |
|
| 92 | 10 | $clients = []; |
|
| 93 | |||
| 94 | 10 | foreach ($config['clients'] as $name => $arguments) { |
|
| 95 | 6 | if ($first === null) { |
|
| 96 | // Save the name of the first configured client. |
||
| 97 | 6 | $first = $name; |
|
| 98 | 6 | } |
|
| 99 | |||
| 100 | 6 | $this->configureClient($container, $name, $arguments, $this->isConfigEnabled($container, $config['profiling'])); |
|
| 101 | 6 | $clients[] = $name; |
|
| 102 | 10 | } |
|
| 103 | |||
| 104 | // If we have clients configured |
||
| 105 | 10 | if ($first !== null) { |
|
| 106 | // If we do not have a client named 'default' |
||
| 107 | 6 | if (!isset($config['clients']['default'])) { |
|
| 108 | // Alias the first client to httplug.client.default |
||
| 109 | 6 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
|
| 110 | 6 | } |
|
| 111 | 6 | } |
|
| 112 | |||
| 113 | 10 | if ($profiling) { |
|
| 114 | 7 | $container->getDefinition('httplug.collector.collector') |
|
| 115 | 7 | ->setArguments([$clients]) |
|
| 116 | ; |
||
| 117 | 7 | } |
|
| 118 | 10 | } |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param ContainerBuilder $container |
||
| 122 | * @param array $config |
||
| 123 | */ |
||
| 124 | 10 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
| 125 | { |
||
| 126 | 10 | if (!empty($config['authentication'])) { |
|
| 127 | $this->configureAuthentication($container, $config['authentication']); |
||
| 128 | } |
||
| 129 | 10 | unset($config['authentication']); |
|
| 130 | |||
| 131 | 10 | foreach ($config as $name => $pluginConfig) { |
|
| 132 | 10 | $pluginId = 'httplug.plugin.'.$name; |
|
| 133 | |||
| 134 | 10 | if ($this->isConfigEnabled($container, $pluginConfig)) { |
|
| 135 | 10 | $def = $container->getDefinition($pluginId); |
|
| 136 | 10 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
|
| 137 | 10 | } else { |
|
| 138 | 10 | $container->removeDefinition($pluginId); |
|
| 139 | } |
||
| 140 | 10 | } |
|
| 141 | 10 | } |
|
| 142 | |||
| 143 | /** |
||
| 144 | * @param string $name |
||
| 145 | * @param Definition $definition |
||
| 146 | * @param array $config |
||
| 147 | * @param ContainerBuilder $container In case we need to add additional services for this plugin |
||
| 148 | * @param string $serviceId Service id of the plugin, in case we need to add additional services for this plugin. |
||
| 149 | */ |
||
| 150 | 10 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
|
| 151 | { |
||
| 152 | switch ($name) { |
||
| 153 | 10 | case 'cache': |
|
| 154 | $definition |
||
| 155 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
| 156 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
| 157 | ->replaceArgument(2, $config['config']); |
||
| 158 | break; |
||
| 159 | 10 | case 'cookie': |
|
| 160 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
| 161 | break; |
||
| 162 | 10 | case 'decoder': |
|
| 163 | 10 | $definition->addArgument([ |
|
| 164 | 10 | 'use_content_encoding' => $config['use_content_encoding'], |
|
| 165 | 10 | ]); |
|
| 166 | 10 | break; |
|
| 167 | 10 | case 'history': |
|
| 168 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
| 169 | break; |
||
| 170 | 10 | case 'logger': |
|
| 171 | 10 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
| 172 | 10 | if (!empty($config['formatter'])) { |
|
| 173 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
| 174 | } |
||
| 175 | 10 | break; |
|
| 176 | 10 | case 'redirect': |
|
| 177 | 10 | $definition->addArgument([ |
|
| 178 | 10 | 'preserve_header' => $config['preserve_header'], |
|
| 179 | 10 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
| 180 | 10 | ]); |
|
| 181 | 10 | break; |
|
| 182 | 10 | case 'retry': |
|
| 183 | 10 | $definition->addArgument([ |
|
| 184 | 10 | 'retries' => $config['retry'], |
|
| 185 | 10 | ]); |
|
| 186 | 10 | break; |
|
| 187 | 10 | case 'stopwatch': |
|
| 188 | 10 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
|
| 189 | 10 | break; |
|
| 190 | |||
| 191 | /* client specific plugins */ |
||
| 192 | |||
| 193 | 3 | case 'add_host': |
|
| 194 | 3 | $uriService = $serviceId.'.host_uri'; |
|
| 195 | 3 | $this->createUri($container, $uriService, $config['host']); |
|
| 196 | 3 | $definition->replaceArgument(0, new Reference($uriService)); |
|
| 197 | 3 | $definition->replaceArgument(1, [ |
|
| 198 | 3 | 'replace' => $config['replace'], |
|
| 199 | 3 | ]); |
|
| 200 | 3 | break; |
|
| 201 | 1 | case 'header_append': |
|
| 202 | 1 | case 'header_defaults': |
|
| 203 | 1 | case 'header_set': |
|
| 204 | 1 | case 'header_remove': |
|
| 205 | 1 | $definition->replaceArgument(0, $config['headers']); |
|
| 206 | 1 | break; |
|
| 207 | |||
| 208 | default: |
||
| 209 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
| 210 | } |
||
| 211 | 10 | } |
|
| 212 | |||
| 213 | /** |
||
| 214 | * @param ContainerBuilder $container |
||
| 215 | * @param array $config |
||
| 216 | * |
||
| 217 | * @return array List of service ids for the authentication plugins. |
||
| 218 | */ |
||
| 219 | 3 | private function configureAuthentication(ContainerBuilder $container, array $config, $servicePrefix = 'httplug.plugin.authentication') |
|
| 220 | { |
||
| 221 | 3 | $pluginServices = []; |
|
| 222 | |||
| 223 | 3 | foreach ($config as $name => $values) { |
|
| 224 | 3 | $authServiceKey = sprintf($servicePrefix.'.%s.auth', $name); |
|
| 225 | 3 | switch ($values['type']) { |
|
| 226 | 3 | case 'bearer': |
|
| 227 | $container->register($authServiceKey, Bearer::class) |
||
| 228 | ->addArgument($values['token']); |
||
| 229 | break; |
||
| 230 | 3 | case 'basic': |
|
| 231 | $container->register($authServiceKey, BasicAuth::class) |
||
| 232 | 3 | ->addArgument($values['username']) |
|
| 233 | 3 | ->addArgument($values['password']); |
|
| 234 | 3 | break; |
|
| 235 | case 'wsse': |
||
| 236 | $container->register($authServiceKey, Wsse::class) |
||
| 237 | ->addArgument($values['username']) |
||
| 238 | ->addArgument($values['password']); |
||
| 239 | break; |
||
| 240 | case 'service': |
||
| 241 | $authServiceKey = $values['service']; |
||
| 242 | break; |
||
| 243 | default: |
||
| 244 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
| 245 | 3 | } |
|
| 246 | |||
| 247 | 3 | $pluginServiceKey = $servicePrefix.'.'.$name; |
|
| 248 | $container->register($pluginServiceKey, AuthenticationPlugin::class) |
||
| 249 | ->addArgument(new Reference($authServiceKey)) |
||
| 250 | ; |
||
| 251 | $pluginServices[] = $pluginServiceKey; |
||
| 252 | } |
||
| 253 | |||
| 254 | return $pluginServices; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param ContainerBuilder $container |
||
| 259 | * @param string $clientName |
||
| 260 | * @param array $arguments |
||
| 261 | * @param bool $profiling |
||
| 262 | */ |
||
| 263 | private function configureClient(ContainerBuilder $container, $clientName, array $arguments, $profiling) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Create a URI object with the default URI factory. |
||
| 369 | * |
||
| 370 | * @param ContainerBuilder $container |
||
| 371 | * @param string $serviceId Name of the private service to create |
||
| 372 | * @param string $uri String representation of the URI |
||
| 373 | */ |
||
| 374 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
| 386 | * by finding a client using auto discovery. |
||
| 387 | * |
||
| 388 | * @param ContainerBuilder $container |
||
| 389 | * @param array $config |
||
| 390 | */ |
||
| 391 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Find a client with auto discovery and return a service Reference to it. |
||
| 432 | * |
||
| 433 | * @param ContainerBuilder $container |
||
| 434 | * @param string $name |
||
| 435 | * @param callable $factory |
||
| 436 | * @param bool $profiling |
||
| 437 | * |
||
| 438 | * @return string service id |
||
| 439 | */ |
||
| 440 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritdoc} |
||
| 465 | */ |
||
| 466 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
| 470 | } |
||
| 471 |
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: