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 |
||
| 35 | class Varnish extends HttpProxyClient implements BanCapable, PurgeCapable, RefreshCapable, TagCapable |
||
| 36 | { |
||
| 37 | const HTTP_METHOD_BAN = 'BAN'; |
||
| 38 | const HTTP_METHOD_PURGE = 'PURGE'; |
||
| 39 | const HTTP_METHOD_REFRESH = 'GET'; |
||
| 40 | const HTTP_HEADER_HOST = 'X-Host'; |
||
| 41 | const HTTP_HEADER_URL = 'X-Url'; |
||
| 42 | const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Default name of the header used to invalidate content with specific tags. |
||
| 46 | * |
||
| 47 | * This happens to be the same as TagHeaderFormatter::DEFAULT_HEADER_NAME |
||
| 48 | * but does not technically need to be the same. |
||
| 49 | * |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | const DEFAULT_HTTP_HEADER_CACHE_TAGS = 'X-Cache-Tags'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | 4 | public function invalidateTags(array $tags) |
|
| 58 | { |
||
| 59 | 4 | $tagMode = $this->options['tag_mode']; |
|
| 60 | 4 | $escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
|
| 61 | |||
| 62 | 4 | if ($tagMode === 'purge_single') { |
|
| 63 | $elems = 1; |
||
| 64 | 4 | } elseif (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
|
| 65 | /* |
||
| 66 | * estimate the amount of tags to invalidate by dividing the max |
||
| 67 | * header length by the largest tag (minus 1 for the implode character) |
||
| 68 | */ |
||
| 69 | 1 | $tagsize = max(array_map('mb_strlen', $escapedTags)); |
|
| 70 | 1 | $elems = floor($this->options['header_length'] / ($tagsize - 1)) ?: 1; |
|
| 71 | } else { |
||
| 72 | 3 | $elems = count($escapedTags); |
|
| 73 | } |
||
| 74 | |||
| 75 | 4 | foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
|
| 76 | 4 | if ($tagMode === 'ban') { |
|
| 77 | 4 | $tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
|
| 78 | 4 | $this->ban([$this->options['tags_header'] => $tagExpression]); |
|
| 79 | } else { |
||
| 80 | $this->queueRequest( |
||
| 81 | self::HTTP_METHOD_PURGE, |
||
| 82 | '/', |
||
| 83 | [$this->options['tags_header'] => implode(' ', $tagchunk)], |
||
| 84 | 4 | false |
|
| 85 | ); |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | 4 | return $this; |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * {@inheritdoc} |
||
| 94 | */ |
||
| 95 | 10 | public function ban(array $headers) |
|
| 96 | { |
||
| 97 | 10 | $headers = array_merge( |
|
| 98 | 10 | $this->options['default_ban_headers'], |
|
| 99 | 10 | $headers |
|
| 100 | ); |
||
| 101 | |||
| 102 | 10 | $this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers, false); |
|
| 103 | |||
| 104 | 10 | return $this; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 4 | public function banPath($path, $contentType = null, $hosts = null) |
|
| 111 | { |
||
| 112 | 4 | if (is_array($hosts)) { |
|
| 113 | 2 | if (!count($hosts)) { |
|
| 114 | 1 | throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
|
| 115 | } |
||
| 116 | 1 | $hosts = '^('.implode('|', $hosts).')$'; |
|
| 117 | } |
||
| 118 | |||
| 119 | $headers = [ |
||
| 120 | 3 | self::HTTP_HEADER_URL => $path, |
|
| 121 | ]; |
||
| 122 | |||
| 123 | 3 | if ($contentType) { |
|
|
|
|||
| 124 | 2 | $headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
|
| 125 | } |
||
| 126 | 3 | if ($hosts) { |
|
| 127 | 1 | $headers[self::HTTP_HEADER_HOST] = $hosts; |
|
| 128 | } |
||
| 129 | |||
| 130 | 3 | return $this->ban($headers); |
|
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * {@inheritdoc} |
||
| 135 | */ |
||
| 136 | 4 | public function purge($url, array $headers = []) |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | 3 | View Code Duplication | public function refresh($url, array $headers = []) |
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | 19 | protected function configureOptions() |
|
| 181 | } |
||
| 182 |
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: