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 |
||
32 | class FOSHttpCacheExtension extends Extension |
||
33 | { |
||
34 | /** |
||
35 | * {@inheritdoc} |
||
36 | */ |
||
37 | 27 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | */ |
||
45 | 27 | public function load(array $configs, ContainerBuilder $container) |
|
46 | { |
||
47 | 27 | $configuration = $this->getConfiguration($configs, $container); |
|
48 | 27 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
49 | |||
50 | 27 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
51 | 27 | $loader->load('matcher.xml'); |
|
52 | |||
53 | 27 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
54 | 7 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
55 | 7 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
56 | 7 | $loader->load('cache_control_listener.xml'); |
|
57 | } |
||
58 | |||
59 | 27 | $this->loadCacheable($container, $config['cacheable']); |
|
60 | |||
61 | 27 | if (!empty($config['cache_control'])) { |
|
62 | 7 | $this->loadCacheControl($container, $config['cache_control']); |
|
63 | } |
||
64 | |||
65 | 26 | if (isset($config['proxy_client'])) { |
|
66 | 18 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
67 | } |
||
68 | |||
69 | 25 | if (isset($config['test'])) { |
|
70 | 2 | $this->loadTest($container, $loader, $config['test']); |
|
71 | } |
||
72 | |||
73 | 25 | if ($config['cache_manager']['enabled']) { |
|
74 | 18 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
75 | // overwrite the previously set alias, if a proxy client was also configured |
||
76 | 1 | $container->setAlias( |
|
77 | 1 | $this->getAlias().'.default_proxy_client', |
|
78 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
79 | ); |
||
80 | } |
||
81 | 18 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
82 | 18 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
83 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
84 | } else { |
||
85 | 17 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
86 | 17 | if ('noop' !== $defaultClient |
|
87 | 17 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
88 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
89 | } else { |
||
90 | 18 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
91 | } |
||
92 | } |
||
93 | } else { |
||
94 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
95 | } |
||
96 | 18 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
97 | 18 | $loader->load('cache_manager.xml'); |
|
98 | 18 | if (class_exists(Application::class)) { |
|
99 | 18 | $loader->load('cache_manager_commands.xml'); |
|
100 | } |
||
101 | } |
||
102 | |||
103 | 25 | if ($config['tags']['enabled']) { |
|
104 | 18 | $this->loadCacheTagging( |
|
105 | 18 | $container, |
|
106 | 18 | $loader, |
|
107 | 18 | $config['tags'], |
|
108 | 18 | array_key_exists('proxy_client', $config) |
|
109 | 17 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
110 | 18 | : 'custom' |
|
111 | ); |
||
112 | } else { |
||
113 | 7 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
114 | } |
||
115 | |||
116 | 24 | if ($config['invalidation']['enabled']) { |
|
117 | 17 | $loader->load('invalidation_listener.xml'); |
|
118 | |||
119 | 17 | if (!empty($config['invalidation']['expression_language'])) { |
|
120 | $container->setAlias( |
||
121 | $this->getAlias().'.invalidation.expression_language', |
||
122 | $config['invalidation']['expression_language'] |
||
123 | ); |
||
124 | } |
||
125 | |||
126 | 17 | if (!empty($config['invalidation']['rules'])) { |
|
127 | 3 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
128 | } |
||
129 | } |
||
130 | |||
131 | 24 | if ($config['user_context']['enabled']) { |
|
132 | 5 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
133 | } |
||
134 | |||
135 | 24 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
136 | 3 | unset($config['flash_message']['enabled']); |
|
137 | 3 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
138 | |||
139 | 3 | $loader->load('flash_message.xml'); |
|
140 | } |
||
141 | 24 | } |
|
142 | |||
143 | 27 | private function loadCacheable(ContainerBuilder $container, array $config) |
|
144 | { |
||
145 | 27 | $definition = $container->getDefinition($this->getAlias().'.response_matcher.cacheable'); |
|
146 | |||
147 | // Change CacheableResponseMatcher to ExpressionResponseMatcher |
||
148 | 27 | if ($config['response']['expression']) { |
|
149 | $definition->setClass(ExpressionResponseMatcher::class) |
||
150 | ->setArguments([$config['response']['expression']]); |
||
151 | } else { |
||
152 | 27 | $container->setParameter( |
|
153 | 27 | $this->getAlias().'.cacheable.response.additional_status', |
|
154 | 27 | $config['response']['additional_status'] |
|
155 | ); |
||
156 | } |
||
157 | 27 | } |
|
158 | |||
159 | /** |
||
160 | * @param ContainerBuilder $container |
||
161 | * @param array $config |
||
162 | * |
||
163 | * @throws InvalidConfigurationException |
||
164 | */ |
||
165 | 7 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
166 | { |
||
167 | 7 | $controlDefinition = $container->getDefinition($this->getAlias().'.event_listener.cache_control'); |
|
168 | |||
169 | 7 | foreach ($config['rules'] as $rule) { |
|
170 | 7 | $ruleMatcher = $this->parseRuleMatcher($container, $rule['match']); |
|
171 | |||
172 | 7 | if ('default' === $rule['headers']['overwrite']) { |
|
173 | 7 | $rule['headers']['overwrite'] = $config['defaults']['overwrite']; |
|
174 | } |
||
175 | |||
176 | 7 | $controlDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['headers']]); |
|
177 | } |
||
178 | 6 | } |
|
179 | |||
180 | /** |
||
181 | * Parse one cache control rule match configuration. |
||
182 | * |
||
183 | * @param ContainerBuilder $container |
||
184 | * @param array $match Request and response match criteria |
||
185 | * |
||
186 | * @return Reference pointing to a rule matcher service |
||
187 | */ |
||
188 | 7 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
189 | { |
||
190 | 7 | $requestMatcher = $this->parseRequestMatcher($container, $match); |
|
191 | 7 | $responseMatcher = $this->parseResponseMatcher($container, $match); |
|
192 | |||
193 | 7 | $signature = serialize([(string) $requestMatcher, (string) $responseMatcher]); |
|
194 | 7 | $id = $this->getAlias().'.cache_control.rule_matcher.'.md5($signature); |
|
195 | |||
196 | 7 | if ($container->hasDefinition($id)) { |
|
197 | 1 | throw new InvalidConfigurationException('Duplicate match criteria. Would be hidden by a previous rule. match: '.json_encode($match)); |
|
198 | } |
||
199 | |||
200 | $container |
||
201 | 7 | ->setDefinition($id, $this->createChildDefinition($this->getAlias().'.rule_matcher')) |
|
202 | 7 | ->replaceArgument(0, $requestMatcher) |
|
203 | 7 | ->replaceArgument(1, $responseMatcher) |
|
204 | ; |
||
205 | |||
206 | 7 | return new Reference($id); |
|
207 | } |
||
208 | |||
209 | /** |
||
210 | * Used for cache control, tag and invalidation rules. |
||
211 | * |
||
212 | * @param ContainerBuilder $container |
||
213 | * @param array $match |
||
214 | * |
||
215 | * @return Reference to the request matcher |
||
216 | */ |
||
217 | 9 | private function parseRequestMatcher(ContainerBuilder $container, array $match) |
|
218 | { |
||
219 | 9 | $match['ips'] = (empty($match['ips'])) ? null : $match['ips']; |
|
220 | |||
221 | $arguments = [ |
||
222 | 9 | $match['path'], |
|
223 | 9 | $match['host'], |
|
224 | 9 | $match['methods'], |
|
225 | 9 | $match['ips'], |
|
226 | 9 | $match['attributes'], |
|
227 | ]; |
||
228 | 9 | $serialized = serialize($arguments); |
|
229 | 9 | $id = $this->getAlias().'.request_matcher.'.md5($serialized).sha1($serialized); |
|
230 | |||
231 | 9 | if (!$container->hasDefinition($id)) { |
|
232 | $container |
||
233 | 9 | ->setDefinition($id, $this->createChildDefinition($this->getAlias().'.request_matcher')) |
|
234 | 9 | ->setArguments($arguments) |
|
235 | ; |
||
236 | |||
237 | 9 | if (!empty($match['query_string'])) { |
|
238 | $container->getDefinition($id)->addMethodCall('setQueryString', [$match['query_string']]); |
||
239 | } |
||
240 | } |
||
241 | |||
242 | 9 | return new Reference($id); |
|
243 | } |
||
244 | |||
245 | /** |
||
246 | * Used only for cache control rules. |
||
247 | * |
||
248 | * @param ContainerBuilder $container |
||
249 | * @param array $config |
||
250 | * |
||
251 | * @return Reference to the correct response matcher service |
||
252 | */ |
||
253 | 7 | private function parseResponseMatcher(ContainerBuilder $container, array $config) |
|
254 | { |
||
255 | 7 | if (!empty($config['additional_response_status'])) { |
|
256 | 1 | $id = $this->getAlias().'cache_control.expression.'.md5(serialize($config['additional_response_status'])); |
|
257 | 1 | View Code Duplication | if (!$container->hasDefinition($id)) { |
258 | $container |
||
259 | 1 | ->setDefinition($id, $this->createChildDefinition($this->getAlias().'.response_matcher.cache_control.cacheable_response')) |
|
260 | 1 | ->setArguments([$config['additional_response_status']]) |
|
261 | ; |
||
262 | } |
||
263 | 6 | } elseif (!empty($config['match_response'])) { |
|
264 | 2 | $id = $this->getAlias().'cache_control.match_response.'.md5($config['match_response']); |
|
265 | 2 | View Code Duplication | if (!$container->hasDefinition($id)) { |
266 | $container |
||
267 | 2 | ->setDefinition($id, $this->createChildDefinition($this->getAlias().'.response_matcher.cache_control.expression')) |
|
268 | 2 | ->replaceArgument(0, $config['match_response']) |
|
269 | ; |
||
270 | } |
||
271 | } else { |
||
272 | 4 | $id = $this->getAlias().'.response_matcher.cacheable'; |
|
273 | } |
||
274 | |||
275 | 7 | return new Reference($id); |
|
276 | } |
||
277 | |||
278 | 5 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
279 | { |
||
280 | 5 | $loader->load('user_context.xml'); |
|
281 | |||
282 | 5 | $container->getDefinition($this->getAlias().'.user_context.request_matcher') |
|
283 | 5 | ->replaceArgument(0, $config['match']['accept']) |
|
284 | 5 | ->replaceArgument(1, $config['match']['method']); |
|
285 | |||
286 | 5 | $container->setParameter($this->getAlias().'.event_listener.user_context.options', [ |
|
287 | 5 | 'user_identifier_headers' => $config['user_identifier_headers'], |
|
288 | 5 | 'user_hash_header' => $config['user_hash_header'], |
|
289 | 5 | 'ttl' => $config['hash_cache_ttl'], |
|
290 | 5 | 'add_vary_on_hash' => $config['always_vary_on_context_hash'], |
|
291 | ]); |
||
292 | 5 | $container->getDefinition($this->getAlias().'.event_listener.user_context') |
|
293 | 5 | ->replaceArgument(0, new Reference($config['match']['matcher_service'])); |
|
294 | |||
295 | 5 | $container->getDefinition($this->getAlias().'.user_context.anonymous_request_matcher') |
|
296 | 5 | ->replaceArgument(0, $config['user_identifier_headers']); |
|
297 | |||
298 | 5 | if ($config['logout_handler']['enabled']) { |
|
299 | 5 | $container->getDefinition($this->getAlias().'.user_context_invalidator') |
|
300 | 5 | ->replaceArgument(1, $config['user_identifier_headers']) |
|
301 | 5 | ->replaceArgument(2, $config['match']['accept']); |
|
302 | |||
303 | 5 | $container->setAlias('security.logout.handler.session', $this->getAlias().'.user_context.session_logout_handler'); |
|
304 | } else { |
||
305 | $container->removeDefinition($this->getAlias().'.user_context.logout_handler'); |
||
306 | $container->removeDefinition($this->getAlias().'.user_context.session_logout_handler'); |
||
307 | $container->removeDefinition($this->getAlias().'.user_context_invalidator'); |
||
308 | } |
||
309 | |||
310 | 5 | if ($config['role_provider']) { |
|
311 | 3 | $container->getDefinition($this->getAlias().'.user_context.role_provider') |
|
312 | 3 | ->addTag(HashGeneratorPass::TAG_NAME) |
|
313 | 3 | ->setAbstract(false); |
|
314 | } |
||
315 | 5 | } |
|
316 | |||
317 | 18 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
337 | |||
338 | /** |
||
339 | * Define the http dispatcher service for the proxy client $name. |
||
340 | * |
||
341 | * @param ContainerBuilder $container |
||
342 | * @param array $config |
||
343 | * @param string $serviceName |
||
344 | */ |
||
345 | 17 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
369 | |||
370 | 14 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
386 | |||
387 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
395 | |||
396 | 1 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
397 | { |
||
398 | 1 | $this->createHttpDispatcherDefinition($container, $config['http'], $this->getAlias().'.proxy_client.symfony.http_dispatcher'); |
|
399 | $options = [ |
||
400 | 1 | 'tags_header' => $config['tags_header'], |
|
401 | 1 | 'tags_method' => $config['tags_method'], |
|
402 | 1 | 'purge_method' => $config['purge_method'], |
|
403 | ]; |
||
404 | 1 | if (!empty($config['header_length'])) { |
|
405 | $options['header_length'] = $config['header_length']; |
||
406 | } |
||
407 | 1 | $container->setParameter($this->getAlias().'.proxy_client.symfony.options', $options); |
|
408 | |||
409 | 1 | $loader->load('symfony.xml'); |
|
410 | 1 | } |
|
411 | |||
412 | /** |
||
413 | * @param ContainerBuilder $container |
||
414 | * @param XmlFileLoader $loader |
||
415 | * @param array $config Configuration section for the tags node |
||
416 | * @param string $client Name of the client used with the cache manager, |
||
417 | * "custom" when a custom client is used |
||
418 | */ |
||
419 | 18 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
420 | { |
||
421 | 18 | if ('auto' === $config['enabled'] && !in_array($client, ['varnish', 'symfony'])) { |
|
422 | 3 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
423 | |||
424 | 3 | return; |
|
425 | } |
||
426 | 15 | if (!in_array($client, ['varnish', 'symfony', 'custom', 'noop'])) { |
|
427 | 1 | throw new InvalidConfigurationException(sprintf('You can not enable cache tagging with the %s client', $client)); |
|
428 | } |
||
429 | |||
430 | 14 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', true); |
|
431 | 14 | $container->setParameter($this->getAlias().'.tag_handler.response_header', $config['response_header']); |
|
432 | 14 | $container->setParameter($this->getAlias().'.tag_handler.strict', $config['strict']); |
|
433 | 14 | $loader->load('cache_tagging.xml'); |
|
434 | 14 | if (class_exists(Application::class)) { |
|
435 | 14 | $loader->load('cache_tagging_commands.xml'); |
|
436 | } |
||
437 | |||
438 | 14 | if (!empty($config['expression_language'])) { |
|
439 | $container->setAlias( |
||
440 | $this->getAlias().'.tag_handler.expression_language', |
||
441 | $config['expression_language'] |
||
442 | ); |
||
443 | } |
||
444 | |||
445 | 14 | if (!empty($config['rules'])) { |
|
446 | 3 | $this->loadTagRules($container, $config['rules']); |
|
447 | } |
||
448 | 14 | } |
|
449 | |||
450 | 2 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
451 | { |
||
452 | 2 | $container->setParameter($this->getAlias().'.test.cache_header', $config['cache_header']); |
|
453 | |||
454 | 2 | if ($config['proxy_server']) { |
|
455 | 2 | $this->loadProxyServer($container, $loader, $config['proxy_server']); |
|
456 | } |
||
457 | 2 | } |
|
458 | |||
459 | 2 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
460 | { |
||
461 | 2 | if (isset($config['varnish'])) { |
|
462 | 2 | $this->loadVarnishProxyServer($container, $loader, $config['varnish']); |
|
463 | } |
||
464 | |||
465 | 2 | if (isset($config['nginx'])) { |
|
466 | $this->loadNginxProxyServer($container, $loader, $config['nginx']); |
||
467 | } |
||
468 | |||
469 | 2 | $container->setAlias( |
|
470 | 2 | $this->getAlias().'.test.default_proxy_server', |
|
471 | 2 | $this->getAlias().'.test.proxy_server.'.$this->getDefaultProxyClient($config) |
|
472 | ); |
||
473 | 2 | } |
|
474 | |||
475 | 2 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
476 | { |
||
477 | 2 | $loader->load('varnish_proxy.xml'); |
|
478 | 2 | foreach ($config as $key => $value) { |
|
479 | 2 | $container->setParameter( |
|
480 | 2 | $this->getAlias().'.test.proxy_server.varnish.'.$key, |
|
481 | 2 | $value |
|
482 | ); |
||
483 | } |
||
484 | 2 | } |
|
485 | |||
486 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
487 | { |
||
488 | $loader->load('nginx_proxy.xml'); |
||
489 | foreach ($config as $key => $value) { |
||
490 | $container->setParameter( |
||
491 | $this->getAlias().'.test.proxy_server.nginx.'.$key, |
||
492 | $value |
||
493 | ); |
||
494 | } |
||
495 | } |
||
496 | |||
497 | 3 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
512 | |||
513 | 3 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
514 | { |
||
515 | 3 | $tagDefinition = $container->getDefinition($this->getAlias().'.event_listener.invalidation'); |
|
516 | |||
517 | 3 | foreach ($config as $rule) { |
|
518 | 3 | $ruleMatcher = $this->parseRequestMatcher($container, $rule['match']); |
|
519 | 3 | $tagDefinition->addMethodCall('addRule', [$ruleMatcher, $rule['routes']]); |
|
520 | } |
||
521 | 3 | } |
|
522 | |||
523 | 17 | private function validateUrl($url, $msg) |
|
524 | { |
||
525 | 17 | $prefixed = $this->prefixSchema($url); |
|
531 | |||
532 | 17 | private function prefixSchema($url) |
|
540 | |||
541 | 17 | private function getDefaultProxyClient(array $config) |
|
565 | |||
566 | /** |
||
567 | * Build the child definition with fallback for Symfony versions < 3.3. |
||
568 | * |
||
569 | * @param string $id Id of the service to extend |
||
570 | * |
||
571 | * @return ChildDefinition|DefinitionDecorator |
||
572 | */ |
||
573 | 9 | private function createChildDefinition($id) |
|
581 | } |
||
582 |
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: