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 | public function setDefaultBanHeaders(array $headers) |
||
58 | { |
||
59 | $this->defaultBanHeaders = $headers; |
||
60 | } |
||
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 | public function setDefaultBanHeader($name, $value) |
||
69 | { |
||
70 | $this->defaultBanHeaders[$name] = $value; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | public function invalidateTags(array $tags) |
||
77 | { |
||
78 | $escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
||
79 | |||
80 | if (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
||
81 | /* |
||
82 | * estimate the amount of tags to invalidate by dividing the max |
||
83 | * header length by the largest tag (minus 1 for the implode character) |
||
84 | */ |
||
85 | $tagsize = max(array_map('mb_strlen', $escapedTags)); |
||
86 | $elems = floor($this->options['header_length'] / ($tagsize - 1)) ? : 1; |
||
87 | } else { |
||
88 | $elems = count($escapedTags); |
||
89 | } |
||
90 | |||
91 | foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
||
92 | $tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
||
93 | $this->ban([$this->options['tags_header'] => $tagExpression]); |
||
94 | } |
||
95 | |||
96 | return $this; |
||
97 | } |
||
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 | public function getTagsHeaderName() |
||
116 | |||
117 | /** |
||
118 | * Get the maximum HTTP header length. |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | public function getHeaderLength() |
||
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function ban(array $headers) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | public function banPath($path, $contentType = null, $hosts = null) |
||
167 | |||
168 | /** |
||
169 | * {@inheritdoc} |
||
170 | */ |
||
171 | public function purge($url, array $headers = []) |
||
177 | |||
178 | /** |
||
179 | * {@inheritdoc} |
||
180 | */ |
||
181 | View Code Duplication | public function refresh($url, array $headers = []) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 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 | 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: