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 |
||
31 | class FOSHttpCacheExtension extends Extension |
||
32 | { |
||
33 | /** |
||
34 | * {@inheritdoc} |
||
35 | */ |
||
36 | 26 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
37 | { |
||
38 | 26 | return new Configuration($container->getParameter('kernel.debug')); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * {@inheritdoc} |
||
43 | */ |
||
44 | 26 | public function load(array $configs, ContainerBuilder $container) |
|
45 | { |
||
46 | 26 | $configuration = $this->getConfiguration($configs, $container); |
|
47 | 26 | $config = $this->processConfiguration($configuration, $configs); |
|
|
|||
48 | |||
49 | 26 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
50 | 26 | $loader->load('matcher.xml'); |
|
51 | |||
52 | 26 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
53 | 6 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
54 | 6 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
55 | 6 | $loader->load('cache_control_listener.xml'); |
|
56 | } |
||
57 | |||
58 | 26 | $this->loadCacheable($container, $config['cacheable']); |
|
59 | |||
60 | 26 | if (!empty($config['cache_control'])) { |
|
61 | 6 | $this->loadCacheControl($container, $config['cache_control']); |
|
62 | } |
||
63 | |||
64 | 25 | if (isset($config['proxy_client'])) { |
|
65 | 17 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
66 | } |
||
67 | |||
68 | 24 | if (isset($config['test'])) { |
|
69 | 1 | $this->loadTest($container, $loader, $config['test']); |
|
70 | } |
||
71 | |||
72 | 24 | if ($config['cache_manager']['enabled']) { |
|
73 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
74 | // overwrite the previously set alias, if a proxy client was also configured |
||
75 | 1 | $container->setAlias( |
|
76 | 1 | $this->getAlias().'.default_proxy_client', |
|
77 | 1 | $config['cache_manager']['custom_proxy_client'] |
|
78 | ); |
||
79 | } |
||
80 | 17 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
81 | 17 | if (array_key_exists('custom_proxy_client', $config['cache_manager'])) { |
|
82 | 1 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
83 | } else { |
||
84 | 16 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
85 | 16 | if ($defaultClient !== 'noop' |
|
86 | 15 | && array_key_exists('base_url', $config['proxy_client'][$defaultClient])) { |
|
87 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH; |
||
88 | } else { |
||
89 | 16 | $generateUrlType = UrlGeneratorInterface::ABSOLUTE_URL; |
|
90 | } |
||
91 | } |
||
92 | } else { |
||
93 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
94 | } |
||
95 | 17 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
96 | 17 | $loader->load('cache_manager.xml'); |
|
97 | } |
||
98 | |||
99 | 24 | if ($config['tags']['enabled']) { |
|
100 | 17 | $this->loadCacheTagging( |
|
101 | 17 | $container, |
|
102 | 17 | $loader, |
|
103 | 17 | $config['tags'], |
|
104 | 17 | array_key_exists('proxy_client', $config) |
|
105 | 16 | ? $this->getDefaultProxyClient($config['proxy_client']) |
|
106 | 17 | : 'custom' |
|
107 | ); |
||
108 | } else { |
||
109 | 7 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
110 | } |
||
111 | |||
112 | 23 | if ($config['invalidation']['enabled']) { |
|
113 | 16 | $loader->load('invalidation_listener.xml'); |
|
114 | |||
115 | 16 | if (!empty($config['invalidation']['expression_language'])) { |
|
116 | $container->setAlias( |
||
117 | $this->getAlias().'.invalidation.expression_language', |
||
118 | $config['invalidation']['expression_language'] |
||
119 | ); |
||
120 | } |
||
121 | |||
122 | 16 | if (!empty($config['invalidation']['rules'])) { |
|
123 | 2 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
124 | } |
||
125 | } |
||
126 | |||
127 | 23 | if ($config['user_context']['enabled']) { |
|
128 | 4 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
129 | } |
||
130 | |||
131 | 23 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
132 | 1 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
133 | |||
134 | 1 | $loader->load('flash_message.xml'); |
|
135 | } |
||
136 | 23 | } |
|
137 | |||
138 | 26 | private function loadCacheable(ContainerBuilder $container, array $config) |
|
139 | { |
||
140 | 26 | $definition = $container->getDefinition($this->getAlias().'.response_matcher.cacheable'); |
|
141 | |||
142 | // Change CacheableResponseMatcher to ExpressionResponseMatcher |
||
143 | 26 | if ($config['response']['expression']) { |
|
144 | $definition->setClass(ExpressionResponseMatcher::class) |
||
145 | ->setArguments([$config['response']['expression']]); |
||
146 | } else { |
||
147 | 26 | $container->setParameter( |
|
148 | 26 | $this->getAlias().'.cacheable.response.additional_status', |
|
149 | 26 | $config['response']['additional_status'] |
|
150 | ); |
||
151 | } |
||
152 | 26 | } |
|
153 | |||
154 | /** |
||
155 | * @param ContainerBuilder $container |
||
156 | * @param array $config |
||
157 | * |
||
158 | * @throws InvalidConfigurationException |
||
159 | */ |
||
160 | 6 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
174 | |||
175 | /** |
||
176 | * Parse one cache control rule match configuration. |
||
177 | * |
||
178 | * @param ContainerBuilder $container |
||
179 | * @param array $match Request and response match criteria |
||
180 | * |
||
181 | * @return Reference pointing to a rule matcher service |
||
182 | */ |
||
183 | 6 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
203 | |||
204 | /** |
||
205 | * Used for cache control, tag and invalidation rules. |
||
206 | * |
||
207 | * @param ContainerBuilder $container |
||
208 | * @param array $match |
||
209 | * |
||
210 | * @return Reference to the request matcher |
||
211 | */ |
||
212 | 8 | private function parseRequestMatcher(ContainerBuilder $container, array $match) |
|
235 | |||
236 | /** |
||
237 | * Used only for cache control rules. |
||
238 | * |
||
239 | * @param ContainerBuilder $container |
||
240 | * @param array $config |
||
241 | * |
||
242 | * @return Reference to the correct response matcher service |
||
243 | */ |
||
244 | 6 | private function parseResponseMatcher(ContainerBuilder $container, array $config) |
|
268 | |||
269 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
303 | |||
304 | 17 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
324 | |||
325 | /** |
||
326 | * Define the http dispatcher service for the proxy client $name. |
||
327 | * |
||
328 | * @param ContainerBuilder $container |
||
329 | * @param array $config |
||
330 | * @param string $serviceName |
||
331 | */ |
||
332 | 16 | private function createHttpDispatcherDefinition(ContainerBuilder $container, array $config, $serviceName) |
|
356 | |||
357 | 13 | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
373 | |||
374 | 2 | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
382 | |||
383 | 1 | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
388 | |||
389 | /** |
||
390 | * @param ContainerBuilder $container |
||
391 | * @param XmlFileLoader $loader |
||
392 | * @param array $config Configuration section for the tags node |
||
393 | * @param string $client Name of the client used with the cache manager, |
||
394 | * "custom" when a custom client is used |
||
395 | */ |
||
396 | 17 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
423 | |||
424 | 1 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
432 | |||
433 | 1 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
448 | |||
449 | 1 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
459 | |||
460 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
470 | |||
471 | 2 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
486 | |||
487 | 2 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
496 | |||
497 | 16 | private function validateUrl($url, $msg) |
|
505 | |||
506 | 16 | private function prefixSchema($url) |
|
514 | |||
515 | 16 | private function getDefaultProxyClient(array $config) |
|
539 | |||
540 | /** |
||
541 | * Build the child definition with fallback for Symfony versions < 3.3. |
||
542 | * |
||
543 | * @param string $id Id of the service to extend |
||
544 | * |
||
545 | * @return ChildDefinition|DefinitionDecorator |
||
546 | */ |
||
547 | 8 | private function createChildDefinition($id) |
|
555 | } |
||
556 |
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: