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) |
|
| 119 | |||
| 120 | /** |
||
| 121 | * @param ContainerBuilder $container |
||
| 122 | * @param array $config |
||
| 123 | */ |
||
| 124 | 10 | private function configureSharedPlugins(ContainerBuilder $container, array $config) |
|
| 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) |
|
| 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') |
|
| 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) |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Create a URI object with the default URI factory. |
||
| 345 | * |
||
| 346 | * @param ContainerBuilder $container |
||
| 347 | * @param string $serviceId Name of the private service to create |
||
| 348 | * @param string $uri String representation of the URI |
||
| 349 | */ |
||
| 350 | private function createUri(ContainerBuilder $container, $serviceId, $uri) |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
| 362 | * by finding a client using auto discovery. |
||
| 363 | * |
||
| 364 | * @param ContainerBuilder $container |
||
| 365 | * @param array $config |
||
| 366 | */ |
||
| 367 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Find a client with auto discovery and return a service Reference to it. |
||
| 408 | * |
||
| 409 | * @param ContainerBuilder $container |
||
| 410 | * @param string $name |
||
| 411 | * @param callable $factory |
||
| 412 | * @param bool $profiling |
||
| 413 | * |
||
| 414 | * @return string service id |
||
| 415 | */ |
||
| 416 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * {@inheritdoc} |
||
| 449 | */ |
||
| 450 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param ContainerBuilder $container |
||
| 457 | * @param string $serviceId |
||
| 458 | * @param string $pluginName |
||
| 459 | * @param array $pluginConfig |
||
| 460 | * |
||
| 461 | * @return string |
||
| 462 | */ |
||
| 463 | private function configurePlugin(ContainerBuilder $container, $serviceId, $pluginName, array $pluginConfig) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @param ContainerBuilder $container |
||
| 479 | * @param $pluginServiceId |
||
| 480 | */ |
||
| 481 | private function decoratePluginWithProfilePlugin(ContainerBuilder $container, $pluginServiceId) |
||
| 493 | |||
| 494 | /** |
||
| 495 | * @param ContainerBuilder $container |
||
| 496 | * @param string $clientName |
||
| 497 | * @param string $serviceId |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | private function configureNewStackPlugin(ContainerBuilder $container, $clientName, $serviceId) |
||
| 514 | } |
||
| 515 |
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: