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 HttpProxyClient 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 | const DEFAULT_HTTP_HEADER_CACHE_TAGS = 'X-Cache-Tags'; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * {@inheritdoc} |
||
| 43 | */ |
||
| 44 | 4 | public function invalidateTags(array $tags) |
|
| 45 | { |
||
| 46 | 4 | $escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
|
| 47 | |||
| 48 | 4 | if (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
|
| 49 | /* |
||
| 50 | * estimate the amount of tags to invalidate by dividing the max |
||
| 51 | * header length by the largest tag (minus 1 for the implode character) |
||
| 52 | */ |
||
| 53 | 1 | $tagsize = max(array_map('mb_strlen', $escapedTags)); |
|
| 54 | 1 | $elems = floor($this->options['header_length'] / ($tagsize - 1)) ?: 1; |
|
| 55 | 1 | } else { |
|
| 56 | 3 | $elems = count($escapedTags); |
|
| 57 | } |
||
| 58 | |||
| 59 | 4 | foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
|
| 60 | 4 | $tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
|
| 61 | 4 | $this->ban([$this->options['tags_header'] => $tagExpression]); |
|
| 62 | 4 | } |
|
| 63 | |||
| 64 | 4 | return $this; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * {@inheritdoc} |
||
| 69 | */ |
||
| 70 | public function getTagsHeaderValue(array $tags) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Get the HTTP header name that will hold cache tags. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getTagsHeaderName() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | 10 | public function ban(array $headers) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * {@inheritdoc} |
||
| 102 | */ |
||
| 103 | 4 | public function banPath($path, $contentType = null, $hosts = null) |
|
| 104 | { |
||
| 105 | 4 | if (is_array($hosts)) { |
|
| 106 | 2 | if (!count($hosts)) { |
|
| 107 | 1 | throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
|
| 108 | } |
||
| 109 | 1 | $hosts = '^('.implode('|', $hosts).')$'; |
|
| 110 | 1 | } |
|
| 111 | |||
| 112 | $headers = [ |
||
| 113 | 3 | self::HTTP_HEADER_URL => $path, |
|
| 114 | 3 | ]; |
|
| 115 | |||
| 116 | 3 | if ($contentType) { |
|
|
|
|||
| 117 | 2 | $headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
|
| 118 | 2 | } |
|
| 119 | 3 | if ($hosts) { |
|
| 120 | 1 | $headers[self::HTTP_HEADER_HOST] = $hosts; |
|
| 121 | 1 | } |
|
| 122 | |||
| 123 | 3 | return $this->ban($headers); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritdoc} |
||
| 128 | */ |
||
| 129 | 4 | public function purge($url, array $headers = []) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * {@inheritdoc} |
||
| 138 | */ |
||
| 139 | 3 | View Code Duplication | public function refresh($url, array $headers = []) |
| 146 | |||
| 147 | /** |
||
| 148 | * {@inheritdoc} |
||
| 149 | */ |
||
| 150 | 20 | protected function configureOptions() |
|
| 171 | } |
||
| 172 |
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: