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 |
||
31 | class Varnish extends AbstractProxyClient implements BanInterface, PurgeInterface, RefreshInterface, TagsInterface |
||
32 | { |
||
33 | const HTTP_METHOD_BAN = 'BAN'; |
||
34 | const HTTP_METHOD_PURGE = 'PURGE'; |
||
35 | const HTTP_METHOD_REFRESH = 'GET'; |
||
36 | const HTTP_HEADER_HOST = 'X-Host'; |
||
37 | const HTTP_HEADER_URL = 'X-Url'; |
||
38 | const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
||
39 | |||
40 | /** |
||
41 | * Map of default headers for ban requests with their default values. |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $defaultBanHeaders = [ |
||
46 | self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
||
47 | self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
||
48 | self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * Set the default headers that get merged with the provided headers in self::ban(). |
||
53 | * |
||
54 | * @param array $headers Hashmap with keys being the header names, values |
||
55 | * the header values. |
||
56 | */ |
||
57 | 2 | public function setDefaultBanHeaders(array $headers) |
|
61 | |||
62 | /** |
||
63 | * Add or overwrite a default ban header. |
||
64 | * |
||
65 | * @param string $name The name of that header |
||
66 | * @param string $value The content of that header |
||
67 | */ |
||
68 | 2 | public function setDefaultBanHeader($name, $value) |
|
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | 4 | public function invalidateTags(array $tags) |
|
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function getTagsHeaderValue(array $tags) |
||
106 | |||
107 | /** |
||
108 | * Get the HTTP header name that will hold cache tags. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | 1 | public function getTagsHeaderName() |
|
116 | |||
117 | /** |
||
118 | * Get the maximum HTTP header length. |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | 1 | public function getHeaderLength() |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 12 | public function ban(array $headers) |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 4 | public function banPath($path, $contentType = null, $hosts = null) |
|
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | 11 | public function purge($url, array $headers = []) |
|
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | 3 | View Code Duplication | public function refresh($url, array $headers = []) |
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 35 | protected function getDefaultOptions() |
|
200 | |||
201 | /** |
||
202 | * Build the invalidation request and validate it. |
||
203 | * |
||
204 | * {@inheritdoc} |
||
205 | * |
||
206 | * @throws MissingHostException If a relative path is queued for purge/ |
||
207 | * refresh and no base URL is set |
||
208 | * |
||
209 | */ |
||
210 | 26 | protected function queueRequest($method, $url, array $headers = []) |
|
223 | } |
||
224 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: