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 FOSHttpCacheExtension 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 FOSHttpCacheExtension, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class FOSHttpCacheExtension extends Extension |
||
30 | { |
||
31 | /** |
||
32 | * {@inheritdoc} |
||
33 | */ |
||
34 | 23 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
38 | |||
39 | /** |
||
40 | * {@inheritdoc} |
||
41 | */ |
||
42 | 23 | public function load(array $configs, ContainerBuilder $container) |
|
43 | { |
||
44 | 23 | $configuration = $this->getConfiguration($configs, $container); |
|
45 | 23 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
46 | |||
47 | 23 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
48 | 23 | $loader->load('matcher.xml'); |
|
49 | |||
50 | 23 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
51 | 3 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
52 | 3 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
53 | 3 | $loader->load('cache_control_listener.xml'); |
|
54 | } |
||
55 | |||
56 | 23 | if (!empty($config['cache_control'])) { |
|
57 | 3 | $this->loadCacheControl($container, $config['cache_control']); |
|
58 | } |
||
59 | |||
60 | 23 | if (isset($config['proxy_client'])) { |
|
61 | 17 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
62 | } |
||
63 | |||
64 | 22 | if (isset($config['test'])) { |
|
65 | 1 | $this->loadTest($container, $loader, $config['test']); |
|
66 | } |
||
67 | |||
68 | 22 | if ($config['cache_manager']['enabled']) { |
|
69 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
70 | // overwrite the previously set alias, if a proxy client was also configured |
||
71 | 1 | $container->setAlias( |
|
72 | 1 | $this->getAlias().'.default_proxy_client', |
|
73 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
74 | ); |
||
75 | } |
||
76 | 17 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
77 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
78 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
79 | } else { |
||
80 | 16 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
81 | 16 | if ($defaultClient !== 'noop' |
|
82 | 16 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
83 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
84 | } else { |
||
85 | 17 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
86 | } |
||
87 | } |
||
88 | } else { |
||
89 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
90 | } |
||
91 | 17 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
92 | 17 | $loader->load('cache_manager.xml'); |
|
93 | } |
||
94 | |||
95 | 22 | if ($config['tags']['enabled']) { |
|
96 | 17 | $this->loadCacheTagging( |
|
97 | $container, |
||
98 | $loader, |
||
99 | 17 | $config['tags'], |
|
100 | 17 | array_key_exists('proxy_client', $config) |
|
101 | 16 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
102 | 17 | : 'custom' |
|
103 | ); |
||
104 | } else { |
||
105 | 5 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
106 | } |
||
107 | |||
108 | 21 | if ($config['invalidation']['enabled']) { |
|
109 | 16 | $loader->load('invalidation_listener.xml'); |
|
110 | |||
111 | 16 | if (!empty($config['invalidation']['expression_language'])) { |
|
112 | $container->setAlias( |
||
113 | $this->getAlias().'.invalidation.expression_language', |
||
114 | $config['invalidation']['expression_language'] |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | 16 | if (!empty($config['invalidation']['rules'])) { |
|
119 | 2 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
120 | } |
||
121 | } |
||
122 | |||
123 | 21 | if ($config['user_context']['enabled']) { |
|
124 | 4 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
125 | } |
||
126 | |||
127 | 21 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
128 | 1 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
129 | |||
130 | 1 | $loader->load('flash_message.xml'); |
|
131 | } |
||
132 | 21 | } |
|
133 | |||
134 | /** |
||
135 | * @param ContainerBuilder $container |
||
136 | * @param array $config |
||
137 | * |
||
138 | * @throws InvalidConfigurationException |
||
139 | */ |
||
140 | 3 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
154 | |||
155 | 5 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
181 | |||
182 | 5 | private function createRuleMatcher(ContainerBuilder $container, Reference $requestMatcher, array $extraCriteria) |
|
198 | |||
199 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
231 | |||
232 | 5 | private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = null, $ips = null, array $attributes = []) |
|
247 | |||
248 | 17 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
249 | { |
||
250 | 17 | if (isset($config['varnish'])) { |
|
251 | 13 | $this->loadVarnish($container, $loader, $config['varnish']); |
|
252 | } |
||
253 | 16 | if (isset($config['nginx'])) { |
|
254 | 2 | $this->loadNginx($container, $loader, $config['nginx']); |
|
255 | } |
||
256 | 16 | if (isset($config['symfony'])) { |
|
257 | 1 | $this->loadSymfony($container, $loader, $config['symfony']); |
|
258 | } |
||
259 | 16 | if (isset($config['noop'])) { |
|
260 | 1 | $loader->load('noop.xml'); |
|
261 | } |
||
262 | |||
263 | 16 | $container->setAlias( |
|
264 | 16 | $this->getAlias().'.default_proxy_client', |
|
265 | 16 | $this->getAlias().'.proxy_client.'.$this->getDefaultProxyClient($config) |
|
266 | ); |
||
267 | 16 | } |
|
268 | |||
269 | /** |
||
270 | * Define the http dispatcher service for the proxy client $name. |
||
271 | * |
||
272 | * @param ContainerBuilder $container |
||
273 | * @param array $config |
||
274 | * @param string $serviceName |
||
275 | */ |
||
276 | 16 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
277 | { |
||
278 | 16 | foreach ($config['servers'] as $url) { |
|
279 | 16 | $this->validateUrl($url, 'Not a valid Varnish server address: "%s"'); |
|
280 | } |
||
281 | 16 | if (!empty($config['base_url'])) { |
|
282 | 16 | $baseUrl = $this->prefixSchema($config['base_url']); |
|
283 | 16 | $this->validateUrl($baseUrl, 'Not a valid base path: "%s"'); |
|
284 | } else { |
||
285 | $baseUrl = null; |
||
286 | } |
||
287 | 15 | $httpClient = null; |
|
288 | 15 | if ($config['http_client']) { |
|
289 | 1 | $httpClient = new Reference($config['http_client']); |
|
290 | } |
||
291 | |||
292 | 15 | $definition = new Definition(HttpDispatcher::class, [ |
|
293 | 15 | $config['servers'], |
|
294 | 15 | $baseUrl, |
|
295 | 15 | $httpClient, |
|
296 | ]); |
||
297 | |||
298 | 15 | $container->setDefinition($serviceName, $definition); |
|
299 | 15 | } |
|
300 | |||
301 | 13 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
302 | { |
||
303 | 13 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.varnish.http_dispatcher'); |
|
304 | 12 | $loader->load('varnish.xml'); |
|
305 | 12 | } |
|
306 | |||
307 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
308 | { |
||
309 | 2 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.nginx.http_dispatcher'); |
|
310 | 2 | $container->setParameter($this->getAlias().'.proxy_client.nginx.options', [ |
|
311 | 2 | 'purge_location' => $config['purge_location'], |
|
312 | ]); |
||
313 | 2 | $loader->load('nginx.xml'); |
|
314 | 2 | } |
|
315 | |||
316 | 1 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
317 | { |
||
318 | 1 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.symfony.http_dispatcher'); |
|
319 | 1 | $loader->load('symfony.xml'); |
|
320 | 1 | } |
|
321 | |||
322 | /** |
||
323 | * @param ContainerBuilder $container |
||
324 | * @param XmlFileLoader $loader |
||
325 | * @param array $config Configuration section for the tags node |
||
326 | * @param string $client Name of the client used with the cache manager, |
||
327 | * "custom" when a custom client is used |
||
328 | */ |
||
329 | 17 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
330 | { |
||
331 | 17 | if ('auto' === $config['enabled'] && 'varnish' !== $client) { |
|
332 | 4 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
333 | |||
334 | 4 | return; |
|
335 | } |
||
336 | 13 | if (!in_array($client, ['varnish', 'custom'])) { |
|
337 | 1 | throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with the %s client', $client)); |
|
338 | } |
||
339 | |||
340 | 12 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', true); |
|
341 | 12 | $container->setParameter($this->getAlias().'.tag_handler.header', $config['header']); |
|
342 | 12 | $loader->load('cache_tagging.xml'); |
|
343 | |||
344 | 12 | if (!empty($config['expression_language'])) { |
|
345 | $container->setAlias( |
||
346 | $this->getAlias().'.tag_handler.expression_language', |
||
347 | $config['expression_language'] |
||
348 | ); |
||
349 | } |
||
350 | |||
351 | 12 | if (!empty($config['rules'])) { |
|
352 | 2 | $this->loadTagRules($container, $config['rules']); |
|
353 | } |
||
354 | 12 | } |
|
355 | |||
356 | 1 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
364 | |||
365 | 1 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
380 | |||
381 | 1 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
391 | |||
392 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
393 | { |
||
394 | $loader->load('nginx_proxy.xml'); |
||
395 | foreach ($config as $key => $value) { |
||
396 | $container->setParameter( |
||
397 | $this->getAlias().'.test.proxy_server.nginx.'.$key, |
||
398 | $value |
||
399 | ); |
||
400 | } |
||
401 | } |
||
402 | |||
403 | 2 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
418 | |||
419 | 2 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
428 | |||
429 | 16 | private function validateUrl($url, $msg) |
|
430 | { |
||
431 | 16 | $prefixed = $this->prefixSchema($url); |
|
432 | |||
433 | 16 | if (!$parts = parse_url($prefixed)) { |
|
434 | 1 | throw new InvalidConfigurationException(sprintf($msg, $url)); |
|
435 | } |
||
436 | 16 | } |
|
437 | |||
438 | 16 | private function prefixSchema($url) |
|
446 | |||
447 | 16 | private function getDefaultProxyClient(array $config) |
|
448 | { |
||
449 | 16 | if (isset($config['default'])) { |
|
450 | return $config['default']; |
||
451 | } |
||
452 | |||
453 | 16 | if (isset($config['varnish'])) { |
|
454 | 12 | return 'varnish'; |
|
471 | } |
||
472 |
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: