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 | */ |
||
| 86 | 9 | private function configureClients(ContainerBuilder $container, array $config) |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param ContainerBuilder $container |
||
| 111 | * @param array $config |
||
| 112 | */ |
||
| 113 | 9 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
| 114 | { |
||
| 115 | 9 | if (!empty($config['authentication'])) { |
|
| 116 | $this->configureAuthentication($container, $config['authentication']); |
||
| 117 | } |
||
| 118 | 9 | unset($config['authentication']); |
|
| 119 | |||
| 120 | 9 | foreach ($config as $name => $pluginConfig) { |
|
| 121 | 9 | $pluginId = 'httplug.plugin.'.$name; |
|
| 122 | |||
| 123 | 9 | if ($pluginConfig['enabled']) { |
|
| 124 | 9 | $def = $container->getDefinition($pluginId); |
|
| 125 | 9 | $this->configurePluginByName($name, $def, $pluginConfig, $container, $pluginId); |
|
| 126 | 9 | } else { |
|
| 127 | 9 | $container->removeDefinition($pluginId); |
|
| 128 | } |
||
| 129 | 9 | } |
|
| 130 | 9 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $name |
||
| 134 | * @param Definition $definition |
||
| 135 | * @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 | */ |
||
| 139 | 9 | private function configurePluginByName($name, Definition $definition, array $config, ContainerInterface $container, $serviceId) |
|
| 140 | { |
||
| 141 | switch ($name) { |
||
| 142 | 9 | case 'cache': |
|
| 143 | $definition |
||
| 144 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
| 145 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
| 146 | ->replaceArgument(2, $config['config']); |
||
| 147 | break; |
||
| 148 | 9 | case 'cookie': |
|
| 149 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
| 150 | break; |
||
| 151 | 9 | case 'decoder': |
|
| 152 | 9 | $definition->addArgument([ |
|
| 153 | 9 | 'use_content_encoding' => $config['use_content_encoding'], |
|
| 154 | 9 | ]); |
|
| 155 | 9 | break; |
|
| 156 | 9 | case 'history': |
|
| 157 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
| 158 | break; |
||
| 159 | 9 | case 'logger': |
|
| 160 | 9 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
| 161 | 9 | if (!empty($config['formatter'])) { |
|
| 162 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
| 163 | } |
||
| 164 | 9 | break; |
|
| 165 | 9 | case 'redirect': |
|
| 166 | 9 | $definition->addArgument([ |
|
| 167 | 9 | 'preserve_header' => $config['preserve_header'], |
|
| 168 | 9 | 'use_default_for_multiple' => $config['use_default_for_multiple'], |
|
| 169 | 9 | ]); |
|
| 170 | 9 | break; |
|
| 171 | 9 | case 'retry': |
|
| 172 | 9 | $definition->addArgument([ |
|
| 173 | 9 | 'retries' => $config['retry'], |
|
| 174 | 9 | ]); |
|
| 175 | 9 | break; |
|
| 176 | 9 | case 'stopwatch': |
|
| 177 | 9 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
|
| 178 | 9 | break; |
|
| 179 | |||
| 180 | /* client specific plugins */ |
||
| 181 | |||
| 182 | 3 | case 'add_host': |
|
| 183 | 3 | $uriService = $serviceId.'.host_uri'; |
|
| 184 | 3 | $this->createUri($container, $uriService, $config['host']); |
|
| 185 | 3 | $definition->replaceArgument(0, new Reference($uriService)); |
|
| 186 | 3 | $definition->replaceArgument(1, [ |
|
| 187 | 3 | 'replace' => $config['replace'], |
|
| 188 | 3 | ]); |
|
| 189 | 3 | break; |
|
| 190 | |||
| 191 | default: |
||
| 192 | throw new \InvalidArgumentException(sprintf('Internal exception: Plugin %s is not handled', $name)); |
||
| 193 | } |
||
| 194 | 9 | } |
|
| 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) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Create a URI object with the default URI factory. |
||
| 332 | * |
||
| 333 | * @param ContainerBuilder $container |
||
| 334 | * @param string $serviceId Name of the private service to create |
||
| 335 | * @param string $uri String representation of the URI |
||
| 336 | */ |
||
| 337 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
| 349 | * by finding a client using auto discovery. |
||
| 350 | * |
||
| 351 | * @param ContainerBuilder $container |
||
| 352 | * @param array $config |
||
| 353 | */ |
||
| 354 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Find a client with auto discovery and return a service Reference to it. |
||
| 395 | * |
||
| 396 | * @param ContainerBuilder $container |
||
| 397 | * @param string $name |
||
| 398 | * @param callable $factory |
||
| 399 | * @param bool $profiling |
||
| 400 | * |
||
| 401 | * @return string service id |
||
| 402 | */ |
||
| 403 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Create a new plugin service for this client. |
||
| 432 | * |
||
| 433 | * @param ContainerBuilder $container |
||
| 434 | * @param string $serviceId |
||
| 435 | * |
||
| 436 | * @return string |
||
| 437 | */ |
||
| 438 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
| 459 | } |
||
| 460 |
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: