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 |
||
| 27 | class HttplugExtension extends Extension |
||
| 28 | { |
||
| 29 | /** |
||
| 30 | * {@inheritdoc} |
||
| 31 | */ |
||
| 32 | 3 | public function load(array $configs, ContainerBuilder $container) |
|
| 33 | { |
||
| 34 | 3 | $configuration = $this->getConfiguration($configs, $container); |
|
| 35 | 3 | $config = $this->processConfiguration($configuration, $configs); |
|
|
1 ignored issue
–
show
|
|||
| 36 | |||
| 37 | 3 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 38 | |||
| 39 | 3 | $loader->load('services.xml'); |
|
| 40 | 3 | $loader->load('plugins.xml'); |
|
| 41 | |||
| 42 | 3 | // Register default services |
|
| 43 | foreach ($config['classes'] as $service => $class) { |
||
| 44 | 3 | if (!empty($class)) { |
|
| 45 | $container->register(sprintf('httplug.%s.default', $service), $class); |
||
| 46 | } |
||
| 47 | } |
||
| 48 | |||
| 49 | // Set main aliases |
||
| 50 | foreach ($config['main_alias'] as $type => $id) { |
||
| 51 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
||
| 52 | } |
||
| 53 | |||
| 54 | // Configure toolbar |
||
| 55 | if ($config['toolbar']['enabled']) { |
||
| 56 | $loader->load('data-collector.xml'); |
||
| 57 | 3 | ||
| 58 | 3 | if (!empty($config['toolbar']['formatter'])) { |
|
| 59 | 1 | // Add custom formatter |
|
| 60 | 1 | $container |
|
| 61 | 3 | ->getDefinition('httplug.collector.debug_collector') |
|
| 62 | ->replaceArgument(0, new Reference($config['toolbar']['formatter'])) |
||
| 63 | ; |
||
| 64 | 3 | } |
|
| 65 | 3 | ||
| 66 | 3 | $container |
|
| 67 | ->getDefinition('httplug.formatter.full_http_message') |
||
| 68 | 3 | ->addArgument($config['toolbar']['captured_body_length']) |
|
| 69 | 3 | ; |
|
| 70 | 3 | } |
|
| 71 | 3 | ||
| 72 | $this->configurePlugins($container, $config['plugins']); |
||
| 73 | $this->configureClients($container, $config); |
||
| 74 | $this->configureAutoDiscoveryClients($container, $config); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Configure client services. |
||
| 79 | * |
||
| 80 | 3 | * @param ContainerBuilder $container |
|
| 81 | * @param array $config |
||
| 82 | */ |
||
| 83 | 3 | private function configureClients(ContainerBuilder $container, array $config) |
|
| 84 | { |
||
| 85 | 3 | $first = null; |
|
| 86 | |||
| 87 | foreach ($config['clients'] as $name => $arguments) { |
||
| 88 | if ($first === null) { |
||
| 89 | // Save the name of the first configurated client. |
||
| 90 | $first = $name; |
||
| 91 | } |
||
| 92 | 3 | ||
| 93 | $this->configureClient($container, $name, $arguments, $config['toolbar']['enabled']); |
||
| 94 | } |
||
| 95 | 3 | ||
| 96 | // If we have clients configured |
||
| 97 | if ($first !== null) { |
||
| 98 | // If we do not have a client named 'default' |
||
| 99 | if (!isset($config['clients']['default'])) { |
||
| 100 | 3 | // Alias the first client to httplug.client.default |
|
| 101 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
||
| 102 | } |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param ContainerBuilder $container |
||
| 108 | * @param array $config |
||
| 109 | */ |
||
| 110 | private function configurePlugins(ContainerBuilder $container, array $config) |
||
| 111 | { |
||
| 112 | if (!empty($config['authentication'])) { |
||
| 113 | $this->configureAuthentication($container, $config['authentication']); |
||
| 114 | } |
||
| 115 | unset($config['authentication']); |
||
| 116 | |||
| 117 | foreach ($config as $name => $pluginConfig) { |
||
| 118 | $pluginId = 'httplug.plugin.'.$name; |
||
| 119 | |||
| 120 | if ($pluginConfig['enabled']) { |
||
| 121 | $def = $container->getDefinition($pluginId); |
||
| 122 | $this->configurePluginByName($name, $def, $pluginConfig); |
||
| 123 | } else { |
||
| 124 | $container->removeDefinition($pluginId); |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param string $name |
||
| 131 | * @param Definition $definition |
||
| 132 | * @param array $config |
||
| 133 | */ |
||
| 134 | private function configurePluginByName($name, Definition $definition, array $config) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * @param ContainerBuilder $container |
||
| 174 | * @param array $config |
||
| 175 | */ |
||
| 176 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param ContainerBuilder $container |
||
| 209 | * @param string $name |
||
| 210 | * @param array $arguments |
||
| 211 | * @param bool $profiling |
||
| 212 | */ |
||
| 213 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
| 272 | * by finding a client using auto discovery. |
||
| 273 | * |
||
| 274 | * @param ContainerBuilder $container |
||
| 275 | * @param array $config |
||
| 276 | */ |
||
| 277 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Find a client with auto discovery and return a service Reference to it. |
||
| 318 | * |
||
| 319 | * @param ContainerBuilder $container |
||
| 320 | * @param string $name |
||
| 321 | * @param callable $factory |
||
| 322 | * @param bool $profiling |
||
| 323 | * |
||
| 324 | * @return string service id |
||
| 325 | */ |
||
| 326 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Create a new plugin service for this client. |
||
| 349 | * |
||
| 350 | * @param ContainerBuilder $container |
||
| 351 | * @param string $serviceId |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
| 368 | |||
| 369 | /** |
||
| 370 | * {@inheritdoc} |
||
| 371 | */ |
||
| 372 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
| 376 | } |
||
| 377 |
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: