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 | 1 | public function getTagsHeaderValue(array $tags) |
|
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritdoc} |
||
| 77 | */ |
||
| 78 | 1 | public function getTagsHeaderName() |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | 10 | public function ban(array $headers) |
|
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | 4 | public function banPath($path, $contentType = null, $hosts = null) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | 4 | public function purge($url, array $headers = []) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * {@inheritdoc} |
||
| 136 | */ |
||
| 137 | 3 | View Code Duplication | public function refresh($url, array $headers = []) |
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | */ |
||
| 148 | 22 | protected function configureOptions() |
|
| 169 | } |
||
| 170 |
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: