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) |
||
| 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) |
||
| 72 | |||
| 73 | 1 | /** |
|
| 74 | * {@inheritdoc} |
||
| 75 | */ |
||
| 76 | 32 | public function invalidateTags(array $tags) |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | public function getTagsHeaderValue(array $tags) |
||
| 90 | 2 | ||
| 91 | /** |
||
| 92 | * Get the HTTP header name that will hold cache tags. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | public function getTagsHeaderName() |
||
| 100 | 2 | ||
| 101 | 2 | /** |
|
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function ban(array $headers) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * {@inheritdoc} |
||
| 118 | */ |
||
| 119 | public function banPath($path, $contentType = null, $hosts = null) |
||
| 141 | 11 | ||
| 142 | /** |
||
| 143 | 11 | * {@inheritdoc} |
|
| 144 | */ |
||
| 145 | public function purge($url, array $headers = []) |
||
| 151 | 4 | ||
| 152 | 2 | /** |
|
| 153 | 1 | * {@inheritdoc} |
|
| 154 | */ |
||
| 155 | 1 | View Code Duplication | public function refresh($url, array $headers = []) |
| 162 | 3 | ||
| 163 | 2 | /** |
|
| 164 | 2 | * {@inheritdoc} |
|
| 165 | 3 | */ |
|
| 166 | 1 | protected function getDefaultOptions() |
|
| 173 | |||
| 174 | /** |
||
| 175 | 11 | * Build the invalidation request and validate it. |
|
| 176 | * |
||
| 177 | 11 | * {@inheritdoc} |
|
| 178 | * |
||
| 179 | 10 | * @throws MissingHostException If a relative path is queued for purge/ |
|
| 180 | * refresh and no base URL is set |
||
| 181 | * |
||
| 182 | */ |
||
| 183 | protected function queueRequest($method, $url, array $headers = []) |
||
| 196 | } |
||
| 197 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: