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:
1 | <?php |
||
19 | class ConfigurationTest extends AbstractExtensionConfigurationTestCase |
||
20 | { |
||
21 | protected function getContainerExtension() |
||
25 | |||
26 | protected function getConfiguration() |
||
30 | |||
31 | public function testEmptyConfiguration() |
||
47 | |||
48 | public function testSupportsAllConfigFormats() |
||
49 | { |
||
50 | $expectedConfiguration = array( |
||
51 | 'cache_control' => array( |
||
52 | 'defaults' => array( |
||
53 | 'overwrite' => true, |
||
54 | ), |
||
55 | 'rules' => array( |
||
56 | array( |
||
57 | 'match' => array( |
||
58 | 'path' => '/abc', |
||
59 | 'host' => 'fos', |
||
60 | 'methods' => array('GET', 'POST'), |
||
61 | 'ips' => array('1.2.3.4', '1.1.1.1'), |
||
62 | 'attributes' => array('_controller' => 'fos.user_bundle.*'), |
||
63 | 'additional_cacheable_status' => array(100, 500), |
||
64 | 'match_response' => '', |
||
65 | // TODO 'match_response' => '', |
||
|
|||
66 | ), |
||
67 | 'headers' => array( |
||
68 | 'overwrite' => false, |
||
69 | 'cache_control' => array( |
||
70 | 'max_age' => 1, |
||
71 | 's_maxage' => 2, |
||
72 | 'public' => true, |
||
73 | 'must_revalidate' => true, |
||
74 | 'proxy_revalidate' => false, |
||
75 | 'no_transform' => true, |
||
76 | 'no_cache' => false, |
||
77 | 'stale_if_error' => 3, |
||
78 | 'stale_while_revalidate' => 4, |
||
79 | ), |
||
80 | 'etag' => true, |
||
81 | 'last_modified' => '-1 hour', |
||
82 | 'reverse_proxy_ttl' => 42, |
||
83 | 'vary' => array('Cookie', 'Authorization'), |
||
84 | ), |
||
85 | ), |
||
86 | ), |
||
87 | ), |
||
88 | 'proxy_client' => array( |
||
89 | 'varnish' => array( |
||
90 | 'servers' => array('22.22.22.22'), |
||
91 | 'base_url' => '/test', |
||
92 | 'guzzle_client' => 'acme.guzzle.varnish', |
||
93 | ), |
||
94 | ), |
||
95 | 'cache_manager' => array( |
||
96 | 'enabled' => true, |
||
97 | 'custom_proxy_client' => 'acme.proxy_client', |
||
98 | 'generate_url_type' => 'auto', |
||
99 | ), |
||
100 | 'tags' => array( |
||
101 | 'enabled' => 'auto', |
||
102 | 'header' => 'FOS-Tags', |
||
103 | 'expression_language' => 'acme.expression_language', |
||
104 | 'rules' => array( |
||
105 | array( |
||
106 | 'match' => array( |
||
107 | 'path' => '/def', |
||
108 | 'host' => 'friends', |
||
109 | 'methods' => array('PUT', 'DELETE'), |
||
110 | 'ips' => array('99.99.99.99'), |
||
111 | 'attributes' => array( |
||
112 | '_foo' => 'bar', |
||
113 | ), |
||
114 | 'additional_cacheable_status' => array(501, 502), |
||
115 | 'match_response' => '', |
||
116 | // TODO match_response |
||
117 | ), |
||
118 | 'tags' => array('a', 'b'), |
||
119 | 'tag_expressions' => array('"a"', '"b"'), |
||
120 | ), |
||
121 | ), |
||
122 | ), |
||
123 | 'invalidation' => array( |
||
124 | 'enabled' => 'auto', |
||
125 | 'expression_language' => 'acme.expression_language', |
||
126 | 'rules' => array( |
||
127 | array( |
||
128 | 'match' => array( |
||
129 | 'path' => '/hij', |
||
130 | 'host' => 'symfony', |
||
131 | 'methods' => array('PATCH'), |
||
132 | 'ips' => array('42.42.42.42'), |
||
133 | 'attributes' => array( |
||
134 | '_format' => 'json', |
||
135 | ), |
||
136 | 'additional_cacheable_status' => array(404, 403), |
||
137 | 'match_response' => '', |
||
138 | // TODO match_response |
||
139 | ), |
||
140 | 'routes' => array( |
||
141 | 'invalidate_route1' => array( |
||
142 | 'ignore_extra_params' => false, |
||
143 | ), |
||
144 | ), |
||
145 | ), |
||
146 | ), |
||
147 | ), |
||
148 | 'user_context' => array( |
||
149 | 'enabled' => true, |
||
150 | 'match' => array( |
||
151 | 'matcher_service' => 'fos_http_cache.user_context.request_matcher', |
||
152 | 'accept' => 'application/vnd.fos.user-context-hash', |
||
153 | 'method' => 'GET', |
||
154 | ), |
||
155 | 'hash_cache_ttl' => 300, |
||
156 | 'user_identifier_headers' => array('Cookie', 'Authorization'), |
||
157 | 'user_hash_header' => 'FOS-User-Context-Hash', |
||
158 | 'role_provider' => true, |
||
159 | 'logout_handler' => array( |
||
160 | 'enabled' => 'auto', |
||
161 | ), |
||
162 | ), |
||
163 | 'flash_message' => array( |
||
164 | 'enabled' => true, |
||
165 | 'name' => 'flashtest', |
||
166 | 'path' => '/x', |
||
167 | 'host' => 'y', |
||
168 | 'secure' => true, |
||
169 | ), |
||
170 | 'debug' => array( |
||
171 | 'enabled' => true, |
||
172 | 'header' => 'FOS-Cache-Debug', |
||
173 | ), |
||
174 | ); |
||
175 | |||
176 | $formats = array_map(function ($path) { |
||
177 | return __DIR__.'/../../Resources/Fixtures/'.$path; |
||
178 | }, array( |
||
179 | 'config/full.yml', |
||
180 | 'config/full.xml', |
||
181 | 'config/full.php', |
||
182 | )); |
||
183 | |||
184 | foreach ($formats as $format) { |
||
185 | $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format)); |
||
186 | } |
||
187 | } |
||
188 | |||
189 | public function testCustomProxyClient() |
||
212 | |||
213 | View Code Duplication | public function testSupportsNginx() |
|
242 | |||
243 | View Code Duplication | public function testSupportsSymfony() |
|
271 | |||
272 | public function testSplitOptions() |
||
273 | { |
||
274 | $expectedConfiguration = $this->getEmptyConfig(); |
||
275 | $expectedConfiguration['cache_control'] = array( |
||
276 | 'rules' => array( |
||
277 | array( |
||
278 | 'match' => array( |
||
279 | 'path' => null, |
||
280 | 'host' => null, |
||
281 | 'attributes' => array(), |
||
282 | 'additional_cacheable_status' => array(), |
||
283 | 'match_response' => null, |
||
284 | 'methods' => array('GET', 'POST'), |
||
285 | 'ips' => array('1.2.3.4', '1.1.1.1'), |
||
286 | ), |
||
287 | 'headers' => array( |
||
288 | 'reverse_proxy_ttl' => null, |
||
289 | 'vary' => array('Cookie', 'Authorization'), |
||
290 | 'overwrite' => 'default', |
||
291 | 'etag' => false, |
||
292 | ), |
||
293 | ), |
||
294 | ), |
||
295 | 'defaults' => array( |
||
296 | 'overwrite' => false, |
||
297 | ), |
||
298 | ); |
||
299 | $expectedConfiguration['proxy_client'] = array( |
||
300 | 'varnish' => array( |
||
301 | 'base_url' => null, |
||
302 | 'guzzle_client' => null, |
||
303 | 'servers' => array('1.1.1.1:80', '2.2.2.2:80'), |
||
304 | ), |
||
305 | 'nginx' => array( |
||
306 | 'base_url' => null, |
||
307 | 'guzzle_client' => null, |
||
308 | 'purge_location' => '', |
||
309 | 'servers' => array('1.1.1.1:81', '2.2.2.2:81'), |
||
310 | ), |
||
311 | ); |
||
312 | $expectedConfiguration['cache_manager']['enabled'] = 'auto'; |
||
313 | $expectedConfiguration['cache_manager']['generate_url_type'] = 'auto'; |
||
314 | $expectedConfiguration['tags']['enabled'] = 'auto'; |
||
315 | $expectedConfiguration['invalidation']['enabled'] = 'auto'; |
||
316 | $expectedConfiguration['user_context']['logout_handler']['enabled'] = 'auto'; |
||
317 | |||
318 | $formats = array_map(function ($path) { |
||
319 | return __DIR__.'/../../Resources/Fixtures/'.$path; |
||
320 | }, array( |
||
321 | 'config/split.yml', |
||
322 | 'config/split.xml', |
||
323 | 'config/split.php', |
||
324 | )); |
||
325 | |||
326 | foreach ($formats as $format) { |
||
327 | $this->assertProcessedConfigurationEquals($expectedConfiguration, array($format)); |
||
328 | } |
||
329 | } |
||
330 | |||
331 | View Code Duplication | public function testCacheManagerNoClient() |
|
350 | |||
351 | View Code Duplication | public function testTagsNoCacheManager() |
|
370 | |||
371 | View Code Duplication | public function testInvalidationNoCacheManager() |
|
390 | |||
391 | View Code Duplication | public function testInvalidDate() |
|
410 | |||
411 | /** |
||
412 | * The configuration is reused, we only need to test this once. |
||
413 | */ |
||
414 | View Code Duplication | public function testRulesBothStatusAndExpression() |
|
433 | |||
434 | /** |
||
435 | * @return array The configuration when nothing is specified. |
||
436 | */ |
||
437 | private function getEmptyConfig() |
||
483 | } |
||
484 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.