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 |
||
28 | class HttplugExtension extends Extension |
||
29 | { |
||
30 | /** |
||
31 | * {@inheritdoc} |
||
32 | 8 | */ |
|
33 | public function load(array $configs, ContainerBuilder $container) |
||
34 | 8 | { |
|
35 | 8 | $configuration = $this->getConfiguration($configs, $container); |
|
36 | $config = $this->processConfiguration($configuration, $configs); |
||
1 ignored issue
–
show
|
|||
37 | 8 | ||
38 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
||
39 | 8 | ||
40 | 8 | $loader->load('services.xml'); |
|
41 | $loader->load('plugins.xml'); |
||
42 | |||
43 | 8 | // Register default services |
|
44 | 8 | foreach ($config['classes'] as $service => $class) { |
|
45 | 1 | if (!empty($class)) { |
|
46 | 1 | $container->register(sprintf('httplug.%s.default', $service), $class); |
|
47 | 8 | } |
|
48 | } |
||
49 | |||
50 | 8 | // Set main aliases |
|
51 | 8 | foreach ($config['main_alias'] as $type => $id) { |
|
52 | 8 | $container->setAlias(sprintf('httplug.%s', $type), $id); |
|
53 | } |
||
54 | |||
55 | 8 | // Configure toolbar |
|
56 | 5 | if ($config['toolbar']['enabled']) { |
|
57 | $loader->load('data-collector.xml'); |
||
58 | 5 | ||
59 | if (!empty($config['toolbar']['formatter'])) { |
||
60 | // Add custom formatter |
||
61 | $container |
||
62 | ->getDefinition('httplug.collector.debug_collector') |
||
63 | ->replaceArgument(0, new Reference($config['toolbar']['formatter'])) |
||
64 | ; |
||
65 | } |
||
66 | |||
67 | 5 | $container |
|
68 | 5 | ->getDefinition('httplug.formatter.full_http_message') |
|
69 | ->addArgument($config['toolbar']['captured_body_length']) |
||
70 | 5 | ; |
|
71 | } |
||
72 | 8 | ||
73 | 8 | $this->configurePlugins($container, $config['plugins']); |
|
74 | 8 | $this->configureClients($container, $config); |
|
75 | 8 | $this->configureAutoDiscoveryClients($container, $config); |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * Configure client services. |
||
80 | * |
||
81 | * @param ContainerBuilder $container |
||
82 | * @param array $config |
||
83 | 8 | */ |
|
84 | private function configureClients(ContainerBuilder $container, array $config) |
||
85 | 8 | { |
|
86 | $first = null; |
||
87 | 8 | ||
88 | 3 | foreach ($config['clients'] as $name => $arguments) { |
|
89 | if ($first === null) { |
||
90 | 3 | // Save the name of the first configurated client. |
|
91 | 3 | $first = $name; |
|
92 | } |
||
93 | 3 | ||
94 | 8 | $this->configureClient($container, $name, $arguments, $config['toolbar']['enabled']); |
|
95 | } |
||
96 | |||
97 | 8 | // If we have clients configured |
|
98 | if ($first !== null) { |
||
99 | 3 | // If we do not have a client named 'default' |
|
100 | if (!isset($config['clients']['default'])) { |
||
101 | 3 | // Alias the first client to httplug.client.default |
|
102 | 3 | $container->setAlias('httplug.client.default', 'httplug.client.'.$first); |
|
103 | 3 | } |
|
104 | 8 | } |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param ContainerBuilder $container |
||
109 | * @param array $config |
||
110 | 8 | */ |
|
111 | private function configurePlugins(ContainerBuilder $container, array $config) |
||
112 | 8 | { |
|
113 | if (!empty($config['authentication'])) { |
||
114 | $this->configureAuthentication($container, $config['authentication']); |
||
115 | 8 | } |
|
116 | unset($config['authentication']); |
||
117 | 8 | ||
118 | 8 | foreach ($config as $name => $pluginConfig) { |
|
119 | $pluginId = 'httplug.plugin.'.$name; |
||
120 | 8 | ||
121 | 8 | if ($pluginConfig['enabled']) { |
|
122 | 8 | $def = $container->getDefinition($pluginId); |
|
123 | 8 | $this->configurePluginByName($name, $def, $pluginConfig); |
|
124 | 8 | } else { |
|
125 | $container->removeDefinition($pluginId); |
||
126 | 8 | } |
|
127 | 8 | } |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param string $name |
||
132 | * @param Definition $definition |
||
133 | * @param array $config |
||
134 | 8 | */ |
|
135 | private function configurePluginByName($name, Definition $definition, array $config) |
||
136 | { |
||
137 | 8 | switch ($name) { |
|
138 | case 'cache': |
||
139 | $definition |
||
140 | ->replaceArgument(0, new Reference($config['cache_pool'])) |
||
141 | ->replaceArgument(1, new Reference($config['stream_factory'])) |
||
142 | ->replaceArgument(2, $config['config']); |
||
143 | 8 | break; |
|
144 | case 'cookie': |
||
145 | $definition->replaceArgument(0, new Reference($config['cookie_jar'])); |
||
146 | 8 | break; |
|
147 | 8 | case 'decoder': |
|
148 | 8 | $definition->addArgument($config['use_content_encoding']); |
|
149 | 8 | break; |
|
150 | case 'history': |
||
151 | $definition->replaceArgument(0, new Reference($config['journal'])); |
||
152 | 8 | break; |
|
153 | 8 | case 'logger': |
|
154 | 8 | $definition->replaceArgument(0, new Reference($config['logger'])); |
|
155 | if (!empty($config['formatter'])) { |
||
156 | $definition->replaceArgument(1, new Reference($config['formatter'])); |
||
157 | 8 | } |
|
158 | 8 | break; |
|
159 | case 'redirect': |
||
160 | 8 | $definition |
|
161 | 8 | ->addArgument($config['preserve_header']) |
|
162 | 8 | ->addArgument($config['use_default_for_multiple']); |
|
163 | 8 | break; |
|
164 | 8 | case 'retry': |
|
165 | 8 | $definition->addArgument($config['retry']); |
|
166 | 8 | break; |
|
167 | 8 | case 'stopwatch': |
|
168 | 8 | $definition->replaceArgument(0, new Reference($config['stopwatch'])); |
|
169 | break; |
||
170 | 8 | } |
|
171 | } |
||
172 | |||
173 | /** |
||
174 | * @param ContainerBuilder $container |
||
175 | * @param array $config |
||
176 | */ |
||
177 | private function configureAuthentication(ContainerBuilder $container, array $config) |
||
178 | { |
||
179 | foreach ($config as $name => $values) { |
||
180 | $authServiceKey = sprintf('httplug.plugin.authentication.%s.auth', $name); |
||
181 | switch ($values['type']) { |
||
182 | case 'bearer': |
||
183 | $container->register($authServiceKey, Bearer::class) |
||
184 | ->addArgument($values['token']); |
||
185 | break; |
||
186 | case 'basic': |
||
187 | $container->register($authServiceKey, BasicAuth::class) |
||
188 | ->addArgument($values['username']) |
||
189 | ->addArgument($values['password']); |
||
190 | break; |
||
191 | case 'wsse': |
||
192 | $container->register($authServiceKey, Wsse::class) |
||
193 | ->addArgument($values['username']) |
||
194 | ->addArgument($values['password']); |
||
195 | break; |
||
196 | case 'service': |
||
197 | $authServiceKey = $values['service']; |
||
198 | break; |
||
199 | default: |
||
200 | throw new \LogicException(sprintf('Unknown authentication type: "%s"', $values['type'])); |
||
201 | } |
||
202 | |||
203 | $container->register('httplug.plugin.authentication.'.$name, AuthenticationPlugin::class) |
||
204 | ->addArgument(new Reference($authServiceKey)); |
||
205 | } |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * @param ContainerBuilder $container |
||
210 | * @param string $name |
||
211 | * @param array $arguments |
||
212 | * @param bool $profiling |
||
213 | */ |
||
214 | private function configureClient(ContainerBuilder $container, $name, array $arguments, $profiling) |
||
279 | |||
280 | /** |
||
281 | * Make the user can select what client is used for auto discovery. If none is provided, a service will be created |
||
282 | * by finding a client using auto discovery. |
||
283 | * |
||
284 | * @param ContainerBuilder $container |
||
285 | * @param array $config |
||
286 | */ |
||
287 | private function configureAutoDiscoveryClients(ContainerBuilder $container, array $config) |
||
325 | |||
326 | /** |
||
327 | * Find a client with auto discovery and return a service Reference to it. |
||
328 | * |
||
329 | * @param ContainerBuilder $container |
||
330 | * @param string $name |
||
331 | * @param callable $factory |
||
332 | * @param bool $profiling |
||
333 | * |
||
334 | * @return string service id |
||
335 | */ |
||
336 | private function registerAutoDiscoverableClient(ContainerBuilder $container, $name, $factory, $profiling) |
||
356 | |||
357 | /** |
||
358 | * Create a new plugin service for this client. |
||
359 | * |
||
360 | * @param ContainerBuilder $container |
||
361 | * @param string $serviceId |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | private function registerDebugPlugin(ContainerBuilder $container, $serviceId) |
||
378 | |||
379 | /** |
||
380 | * {@inheritdoc} |
||
381 | */ |
||
382 | public function getConfiguration(array $config, ContainerBuilder $container) |
||
386 | } |
||
387 |
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: