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 |
||
| 29 | class Varnish extends AbstractProxyClient implements BanInterface, PurgeInterface, RefreshInterface, TagsInterface |
||
| 30 | { |
||
| 31 | const HTTP_METHOD_BAN = 'BAN'; |
||
| 32 | const HTTP_METHOD_PURGE = 'PURGE'; |
||
| 33 | const HTTP_METHOD_REFRESH = 'GET'; |
||
| 34 | const HTTP_HEADER_HOST = 'X-Host'; |
||
| 35 | const HTTP_HEADER_URL = 'X-Url'; |
||
| 36 | const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Map of default headers for ban requests with their default values. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | private $defaultBanHeaders = [ |
||
| 44 | self::HTTP_HEADER_HOST => self::REGEX_MATCH_ALL, |
||
| 45 | self::HTTP_HEADER_URL => self::REGEX_MATCH_ALL, |
||
| 46 | self::HTTP_HEADER_CONTENT_TYPE => self::REGEX_MATCH_ALL |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Has a base URI been set? |
||
| 51 | * |
||
| 52 | * @var bool |
||
| 53 | */ |
||
| 54 | private $baseUriSet; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $tagsHeader; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * {@inheritdoc} |
||
| 63 | * |
||
| 64 | * Additional options: |
||
| 65 | * |
||
| 66 | * - tags_header Header for tagging responses, defaults to X-Cache-Tags |
||
| 67 | * |
||
| 68 | * @param string $tagsHeader |
||
|
|
|||
| 69 | */ |
||
| 70 | 32 | public function __construct( |
|
| 71 | array $servers, |
||
| 72 | array $options = [], |
||
| 73 | 1 | HttpAsyncClient $httpClient = null, |
|
| 74 | MessageFactory $messageFactory = null |
||
| 75 | ) { |
||
| 76 | 32 | parent::__construct($servers, $options, $httpClient, $messageFactory); |
|
| 77 | 29 | $this->baseUriSet = $this->options['base_uri'] !== null; |
|
| 78 | 29 | $this->tagsHeader = $this->options['tags_header']; |
|
| 79 | 29 | } |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Set the default headers that get merged with the provided headers in self::ban(). |
||
| 83 | * |
||
| 84 | * @param array $headers Hashmap with keys being the header names, values |
||
| 85 | * the header values. |
||
| 86 | */ |
||
| 87 | 2 | public function setDefaultBanHeaders(array $headers) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Add or overwrite a default ban header. |
||
| 94 | * |
||
| 95 | * @param string $name The name of that header |
||
| 96 | * @param string $value The content of that header |
||
| 97 | */ |
||
| 98 | 2 | public function setDefaultBanHeader($name, $value) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | 3 | public function invalidateTags(array $tags) |
|
| 107 | { |
||
| 108 | 3 | $tagExpression = sprintf('(%s)(,.+)?$', implode('|', array_map('preg_quote', $this->escapeTags($tags)))); |
|
| 109 | |||
| 110 | 3 | return $this->ban([$this->tagsHeader => $tagExpression]); |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * {@inheritdoc} |
||
| 115 | */ |
||
| 116 | public function getTagsHeaderValue(array $tags) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Get the HTTP header name that will hold cache tags. |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getTagsHeaderName() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * {@inheritdoc} |
||
| 133 | */ |
||
| 134 | 11 | public function ban(array $headers) |
|
| 135 | { |
||
| 136 | 11 | $headers = array_merge( |
|
| 137 | 11 | $this->defaultBanHeaders, |
|
| 138 | $headers |
||
| 139 | 11 | ); |
|
| 140 | |||
| 141 | 11 | $this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers); |
|
| 142 | |||
| 143 | 11 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 4 | public function banPath($path, $contentType = null, $hosts = null) |
|
| 150 | { |
||
| 151 | 4 | if (is_array($hosts)) { |
|
| 152 | 2 | if (!count($hosts)) { |
|
| 153 | 1 | throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
|
| 154 | } |
||
| 155 | 1 | $hosts = '^('.join('|', $hosts).')$'; |
|
| 156 | 1 | } |
|
| 157 | |||
| 158 | $headers = [ |
||
| 159 | 3 | self::HTTP_HEADER_URL => $path, |
|
| 160 | 3 | ]; |
|
| 161 | |||
| 162 | 3 | if ($contentType) { |
|
| 163 | 2 | $headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
|
| 164 | 2 | } |
|
| 165 | 3 | if ($hosts) { |
|
| 166 | 1 | $headers[self::HTTP_HEADER_HOST] = $hosts; |
|
| 167 | 1 | } |
|
| 168 | |||
| 169 | 3 | return $this->ban($headers); |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 11 | public function purge($url, array $headers = []) |
|
| 176 | { |
||
| 177 | 11 | $this->queueRequest(self::HTTP_METHOD_PURGE, $url, $headers); |
|
| 178 | |||
| 179 | 10 | return $this; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | 3 | View Code Duplication | public function refresh($url, array $headers = []) |
| 186 | { |
||
| 187 | 3 | $headers = array_merge($headers, ['Cache-Control' => 'no-cache']); |
|
| 188 | 3 | $this->queueRequest(self::HTTP_METHOD_REFRESH, $url, $headers); |
|
| 189 | |||
| 190 | 3 | return $this; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | 32 | protected function getDefaultOptions() |
|
| 197 | { |
||
| 198 | 32 | $resolver = parent::getDefaultOptions(); |
|
| 199 | 32 | $resolver->setDefaults(['tags_header' => 'X-Cache-Tags']); |
|
| 200 | |||
| 201 | 32 | return $resolver; |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Build the invalidation request and validate it. |
||
| 206 | * |
||
| 207 | * {@inheritdoc} |
||
| 208 | * |
||
| 209 | * @throws MissingHostException If a relative path is queued for purge/ |
||
| 210 | * refresh and no base URL is set |
||
| 211 | * |
||
| 212 | */ |
||
| 213 | 25 | protected function queueRequest($method, $url, array $headers = []) |
|
| 226 | } |
||
| 227 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.