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( |
|
| 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) |
|
| 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) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * {@inheritdoc} |
||
| 148 | */ |
||
| 149 | 4 | public function banPath($path, $contentType = null, $hosts = null) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | */ |
||
| 175 | 11 | public function purge($url, array $headers = []) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | 3 | View Code Duplication | public function refresh($url, array $headers = []) |
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | 32 | protected function getDefaultOptions() |
|
| 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.