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 |
||
27 | class FOSHttpCacheExtension extends Extension |
||
28 | { |
||
29 | /** |
||
30 | * {@inheritDoc} |
||
31 | */ |
||
32 | 21 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | 21 | public function load(array $configs, ContainerBuilder $container) |
|
41 | { |
||
42 | 21 | $configuration = $this->getConfiguration($configs, $container); |
|
43 | 21 | $config = $this->processConfiguration($configuration, $configs); |
|
44 | |||
45 | 21 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
46 | 21 | $loader->load('matcher.xml'); |
|
47 | |||
48 | 21 | if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { |
|
49 | 3 | $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; |
|
50 | 3 | $container->setParameter($this->getAlias().'.debug_header', $debugHeader); |
|
51 | 3 | $loader->load('cache_control_listener.xml'); |
|
52 | 3 | } |
|
53 | |||
54 | 21 | if (!empty($config['cache_control'])) { |
|
55 | 3 | $this->loadCacheControl($container, $config['cache_control']); |
|
56 | 3 | } |
|
57 | |||
58 | 21 | if (isset($config['proxy_client'])) { |
|
59 | 16 | $this->loadProxyClient($container, $loader, $config['proxy_client']); |
|
60 | 15 | } |
|
61 | |||
62 | 20 | if (isset($config['test'])) { |
|
63 | 1 | $this->loadTest($container, $loader, $config['test']); |
|
64 | 1 | } |
|
65 | |||
66 | 20 | if ($config['cache_manager']['enabled']) { |
|
67 | 15 | if (!empty($config['cache_manager']['custom_proxy_client'])) { |
|
68 | // overwrite the previously set alias, if a proxy client was also configured |
||
69 | $container->setAlias( |
||
70 | $this->getAlias().'.default_proxy_client', |
||
71 | $config['cache_manager']['custom_proxy_client'] |
||
72 | ); |
||
73 | } |
||
74 | 15 | if ('auto' === $config['cache_manager']['generate_url_type']) { |
|
75 | 15 | $defaultClient = $this->getDefaultProxyClient($config['proxy_client']); |
|
76 | 15 | $generateUrlType = empty($config['cache_manager']['custom_proxy_client']) && isset($config['proxy_client'][$defaultClient]['base_url']) |
|
77 | 15 | ? UrlGeneratorInterface::ABSOLUTE_PATH |
|
78 | 15 | : UrlGeneratorInterface::ABSOLUTE_URL |
|
79 | 15 | ; |
|
80 | 15 | } else { |
|
81 | $generateUrlType = $config['cache_manager']['generate_url_type']; |
||
82 | } |
||
83 | 15 | $container->setParameter($this->getAlias().'.cache_manager.generate_url_type', $generateUrlType); |
|
84 | 15 | $loader->load('cache_manager.xml'); |
|
85 | 15 | } |
|
86 | |||
87 | 20 | if ($config['tags']['enabled']) { |
|
88 | 15 | $this->loadCacheTagging( |
|
89 | 15 | $container, |
|
90 | 15 | $loader, |
|
91 | 15 | $config['tags'], |
|
92 | 15 | $this->getDefaultProxyClient($config['proxy_client']) |
|
93 | 15 | ); |
|
94 | 14 | } else { |
|
95 | 5 | $container->setParameter($this->getAlias().'.compiler_pass.tag_annotations', false); |
|
96 | } |
||
97 | |||
98 | 19 | if ($config['invalidation']['enabled']) { |
|
99 | 14 | $loader->load('invalidation_listener.xml'); |
|
100 | |||
101 | 14 | if (!empty($config['invalidation']['expression_language'])) { |
|
102 | $container->setAlias( |
||
103 | $this->getAlias().'.invalidation.expression_language', |
||
104 | $config['invalidation']['expression_language'] |
||
105 | ); |
||
106 | } |
||
107 | |||
108 | 14 | if (!empty($config['invalidation']['rules'])) { |
|
109 | 2 | $this->loadInvalidatorRules($container, $config['invalidation']['rules']); |
|
110 | 2 | } |
|
111 | 14 | } |
|
112 | |||
113 | 19 | if ($config['user_context']['enabled']) { |
|
114 | 4 | $this->loadUserContext($container, $loader, $config['user_context']); |
|
115 | 4 | } |
|
116 | |||
117 | 19 | if (!empty($config['flash_message']) && $config['flash_message']['enabled']) { |
|
118 | 1 | $container->setParameter($this->getAlias().'.event_listener.flash_message.options', $config['flash_message']); |
|
119 | |||
120 | 1 | $loader->load('flash_message.xml'); |
|
121 | 1 | } |
|
122 | 19 | } |
|
123 | |||
124 | /** |
||
125 | * @param ContainerBuilder $container |
||
126 | * @param array $config |
||
127 | * |
||
128 | * @throws InvalidConfigurationException |
||
129 | */ |
||
130 | 3 | private function loadCacheControl(ContainerBuilder $container, array $config) |
|
131 | { |
||
132 | 3 | $controlDefinition = $container->getDefinition($this->getAlias().'.event_listener.cache_control'); |
|
133 | |||
134 | 3 | foreach ($config['rules'] as $rule) { |
|
135 | 3 | $ruleMatcher = $this->parseRuleMatcher($container, $rule['match']); |
|
136 | |||
137 | 3 | if ('default' === $rule['headers']['overwrite']) { |
|
138 | 3 | $rule['headers']['overwrite'] = $config['defaults']['overwrite']; |
|
139 | 3 | } |
|
140 | |||
141 | 3 | $controlDefinition->addMethodCall('addRule', array($ruleMatcher, $rule['headers'])); |
|
142 | 3 | } |
|
143 | 3 | } |
|
144 | |||
145 | 5 | private function parseRuleMatcher(ContainerBuilder $container, array $match) |
|
146 | { |
||
147 | 5 | $match['ips'] = (empty($match['ips'])) ? null : $match['ips']; |
|
148 | |||
149 | 5 | $requestMatcher = $this->createRequestMatcher( |
|
150 | 5 | $container, |
|
151 | 5 | $match['path'], |
|
152 | 5 | $match['host'], |
|
153 | 5 | $match['methods'], |
|
154 | 5 | $match['ips'], |
|
155 | 5 | $match['attributes'] |
|
156 | 5 | ); |
|
157 | |||
158 | 5 | $extraCriteria = array(); |
|
159 | 5 | foreach (array('additional_cacheable_status', 'match_response') as $extra) { |
|
160 | 5 | if (isset($match[$extra])) { |
|
161 | 5 | $extraCriteria[$extra] = $match[$extra]; |
|
162 | 5 | } |
|
163 | 5 | } |
|
164 | |||
165 | 5 | return $this->createRuleMatcher( |
|
166 | 5 | $container, |
|
167 | 5 | $requestMatcher, |
|
168 | $extraCriteria |
||
169 | 5 | ); |
|
170 | } |
||
171 | |||
172 | 5 | private function createRuleMatcher(ContainerBuilder $container, Reference $requestMatcher, array $extraCriteria) |
|
173 | { |
||
174 | 5 | $arguments = array((string) $requestMatcher, $extraCriteria); |
|
175 | 5 | $serialized = serialize($arguments); |
|
176 | 5 | $id = $this->getAlias().'.rule_matcher.'.md5($serialized).sha1($serialized); |
|
177 | |||
178 | 5 | View Code Duplication | if (!$container->hasDefinition($id)) { |
|
|||
179 | $container |
||
180 | 5 | ->setDefinition($id, new DefinitionDecorator($this->getAlias().'.rule_matcher')) |
|
181 | 5 | ->replaceArgument(0, $requestMatcher) |
|
182 | 5 | ->replaceArgument(1, $extraCriteria) |
|
183 | ; |
||
184 | 5 | } |
|
185 | |||
186 | 5 | return new Reference($id); |
|
187 | } |
||
188 | |||
189 | 4 | private function loadUserContext(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
190 | { |
||
191 | 4 | $loader->load('user_context.xml'); |
|
192 | |||
193 | 4 | $container->getDefinition($this->getAlias().'.user_context.request_matcher') |
|
194 | 4 | ->replaceArgument(0, $config['match']['accept']) |
|
195 | 4 | ->replaceArgument(1, $config['match']['method']); |
|
196 | |||
197 | 4 | $container->getDefinition($this->getAlias().'.event_listener.user_context') |
|
198 | 4 | ->replaceArgument(0, new Reference($config['match']['matcher_service'])) |
|
199 | 4 | ->replaceArgument(2, $config['user_identifier_headers']) |
|
200 | 4 | ->replaceArgument(3, $config['user_hash_header']) |
|
201 | 4 | ->replaceArgument(4, $config['hash_cache_ttl']); |
|
202 | |||
203 | 4 | if ($config['logout_handler']['enabled']) { |
|
204 | 4 | $container->getDefinition($this->getAlias().'.user_context.logout_handler') |
|
205 | 4 | ->replaceArgument(1, $config['user_identifier_headers']) |
|
206 | 4 | ->replaceArgument(2, $config['match']['accept']); |
|
207 | 4 | } else { |
|
208 | $container->removeDefinition($this->getAlias().'.user_context.logout_handler'); |
||
209 | } |
||
210 | |||
211 | 4 | if ($config['role_provider']) { |
|
212 | 2 | $container->getDefinition($this->getAlias().'.user_context.role_provider') |
|
213 | 2 | ->addTag(HashGeneratorPass::TAG_NAME) |
|
214 | 2 | ->setAbstract(false); |
|
215 | 2 | } |
|
216 | 4 | } |
|
217 | |||
218 | 5 | private function createRequestMatcher(ContainerBuilder $container, $path = null, $host = null, $methods = null, $ips = null, array $attributes = array()) |
|
219 | { |
||
220 | 5 | $arguments = array($path, $host, $methods, $ips, $attributes); |
|
221 | 5 | $serialized = serialize($arguments); |
|
222 | 5 | $id = $this->getAlias().'.request_matcher.'.md5($serialized).sha1($serialized); |
|
223 | |||
224 | 5 | View Code Duplication | if (!$container->hasDefinition($id)) { |
225 | $container |
||
226 | 5 | ->setDefinition($id, new DefinitionDecorator($this->getAlias().'.request_matcher')) |
|
227 | 5 | ->setArguments($arguments) |
|
228 | ; |
||
229 | 5 | } |
|
230 | |||
231 | 5 | return new Reference($id); |
|
232 | } |
||
233 | |||
234 | 16 | private function loadProxyClient(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
251 | |||
252 | 13 | View Code Duplication | private function loadVarnish(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
253 | { |
||
254 | 13 | $loader->load('varnish.xml'); |
|
255 | 13 | foreach ($config['servers'] as $url) { |
|
256 | 13 | $this->validateUrl($url, 'Not a valid Varnish server address: "%s"'); |
|
257 | 13 | } |
|
274 | |||
275 | 2 | View Code Duplication | private function loadNginx(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
297 | |||
298 | 1 | View Code Duplication | private function loadSymfony(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
320 | |||
321 | 15 | private function loadCacheTagging(ContainerBuilder $container, XmlFileLoader $loader, array $config, $client) |
|
352 | |||
353 | 1 | private function loadTest(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
379 | |||
380 | 1 | private function loadProxyServer(ContainerBuilder $container, XmlFileLoader $loader, array $config) |
|
395 | |||
396 | 1 | private function loadVarnishProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
|
406 | |||
407 | private function loadNginxProxyServer(ContainerBuilder $container, XmlFileLoader $loader, $config) |
||
417 | |||
418 | 2 | private function loadTagRules(ContainerBuilder $container, array $config) |
|
433 | |||
434 | 2 | private function loadInvalidatorRules(ContainerBuilder $container, array $config) |
|
443 | |||
444 | 16 | private function validateUrl($url, $msg) |
|
452 | |||
453 | 16 | private function prefixSchema($url) |
|
461 | |||
462 | 15 | private function getDefaultProxyClient(array $config) |
|
482 | } |
||
483 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.