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 |
||
30 | class Varnish extends AbstractProxyClient implements BanInterface, PurgeInterface, RefreshInterface, TagsInterface |
||
31 | { |
||
32 | const HTTP_METHOD_BAN = 'BAN'; |
||
33 | const HTTP_METHOD_PURGE = 'PURGE'; |
||
34 | const HTTP_METHOD_REFRESH = 'GET'; |
||
35 | const HTTP_HEADER_HOST = 'X-Host'; |
||
36 | const HTTP_HEADER_URL = 'X-Url'; |
||
37 | const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
||
38 | |||
39 | /** |
||
40 | * Map of default headers for ban requests with their default values. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | private $defaultBanHeaders = [ |
||
45 | self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
||
46 | self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
||
47 | self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL |
||
48 | ]; |
||
49 | |||
50 | /** |
||
51 | * Set the default headers that get merged with the provided headers in self::ban(). |
||
52 | * |
||
53 | * @param array $headers Hashmap with keys being the header names, values |
||
54 | * the header values. |
||
55 | */ |
||
56 | public function setDefaultBanHeaders(array $headers) |
||
60 | |||
61 | /** |
||
62 | * Add or overwrite a default ban header. |
||
63 | * |
||
64 | * @param string $name The name of that header |
||
65 | * @param string $value The content of that header |
||
66 | */ |
||
67 | public function setDefaultBanHeader($name, $value) |
||
71 | |||
72 | /** |
||
73 | 1 | * {@inheritdoc} |
|
74 | */ |
||
75 | public function invalidateTags(array $tags) |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function getTagsHeaderValue(array $tags) |
||
89 | 2 | ||
90 | 2 | /** |
|
91 | * Get the HTTP header name that will hold cache tags. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getTagsHeaderName() |
||
99 | |||
100 | 2 | /** |
|
101 | 2 | * {@inheritdoc} |
|
102 | */ |
||
103 | public function ban(array $headers) |
||
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | */ |
||
118 | public function banPath($path, $contentType = null, $hosts = null) |
||
140 | |||
141 | 11 | /** |
|
142 | * {@inheritdoc} |
||
143 | 11 | */ |
|
144 | public function purge($url, array $headers = []) |
||
150 | |||
151 | 4 | /** |
|
152 | 2 | * {@inheritdoc} |
|
153 | 1 | */ |
|
154 | View Code Duplication | public function refresh($url, array $headers = []) |
|
161 | |||
162 | 3 | /** |
|
163 | 2 | * {@inheritdoc} |
|
164 | 2 | */ |
|
165 | 3 | protected function getDefaultOptions() |
|
172 | |||
173 | /** |
||
174 | * Build the invalidation request and validate it. |
||
175 | 11 | * |
|
176 | * {@inheritdoc} |
||
177 | 11 | * |
|
178 | * @throws MissingHostException If a relative path is queued for purge/ |
||
179 | 10 | * refresh and no base URL is set |
|
180 | * |
||
181 | */ |
||
182 | protected function queueRequest($method, $url, array $headers = []) |
||
195 | } |
||
196 |
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: