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 | const DEFAULT_HTTP_PURGE_HEADER_CACHE_TAGS = 'key'; |
||
41 | |||
42 | /** |
||
43 | * {@inheritdoc} |
||
44 | 4 | */ |
|
45 | public function invalidateTags(array $tags) |
||
46 | 4 | { |
|
47 | $tagMode = $this->options['tag_mode']; |
||
48 | 4 | $escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
|
49 | |||
50 | if ($tagMode === 'purge_single') { |
||
51 | $elems = 1; |
||
52 | } elseif (mb_strlen(implode('|', $escapedTags)) >= $this->options['header_length']) { |
||
53 | 1 | /* |
|
54 | 1 | * estimate the amount of tags to invalidate by dividing the max |
|
55 | 1 | * header length by the largest tag (minus 1 for the implode character) |
|
56 | 3 | */ |
|
57 | $tagsize = max(array_map('mb_strlen', $escapedTags)); |
||
58 | $elems = floor($this->options['header_length'] / ($tagsize - 1)) ?: 1; |
||
59 | 4 | } else { |
|
60 | 4 | $elems = count($escapedTags); |
|
61 | 4 | } |
|
62 | 4 | ||
63 | foreach (array_chunk($escapedTags, $elems) as $tagchunk) { |
||
64 | 4 | if ($tagMode === 'ban') { |
|
65 | $tagExpression = sprintf('(%s)(,.+)?$', implode('|', $tagchunk)); |
||
66 | $this->ban([$this->options['tags_header'] => $tagExpression]); |
||
67 | } else { |
||
68 | $this->purge('/', [$this->options['tags_purge_header'] => implode(' ', $tagchunk)]); |
||
69 | } |
||
70 | 1 | } |
|
71 | |||
72 | 1 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 1 | public function getTagsHeaderValue(array $tags) |
|
79 | { |
||
80 | 1 | return implode(($this->options['tag_mode'] === 'ban' ? ',' : ' '), array_unique($this->escapeTags($tags))); |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | 10 | public function getTagsHeaderName() |
|
90 | |||
91 | 10 | /** |
|
92 | * {@inheritdoc} |
||
93 | 10 | */ |
|
94 | public function ban(array $headers) |
||
105 | 1 | ||
106 | /** |
||
107 | 1 | * {@inheritdoc} |
|
108 | 1 | */ |
|
109 | public function banPath($path, $contentType = null, $hosts = null) |
||
110 | { |
||
111 | 3 | if (is_array($hosts)) { |
|
112 | 3 | if (!count($hosts)) { |
|
113 | throw new InvalidArgumentException('Either supply a list of hosts or null, but not an empty array.'); |
||
114 | 3 | } |
|
115 | 2 | $hosts = '^('.implode('|', $hosts).')$'; |
|
116 | 2 | } |
|
117 | 3 | ||
118 | 1 | $headers = [ |
|
119 | 1 | self::HTTP_HEADER_URL => $path, |
|
120 | ]; |
||
121 | 3 | ||
122 | if ($contentType) { |
||
|
|||
123 | $headers[self::HTTP_HEADER_CONTENT_TYPE] = $contentType; |
||
124 | } |
||
125 | if ($hosts) { |
||
126 | $headers[self::HTTP_HEADER_HOST] = $hosts; |
||
127 | 4 | } |
|
128 | |||
129 | 4 | return $this->ban($headers); |
|
130 | } |
||
131 | 4 | ||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function purge($url, array $headers = []) |
||
141 | |||
142 | 3 | /** |
|
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | View Code Duplication | public function refresh($url, array $headers = []) |
|
152 | 22 | ||
153 | 22 | /** |
|
154 | 22 | * {@inheritdoc} |
|
155 | 22 | */ |
|
156 | 22 | protected function configureOptions() |
|
181 | } |
||
182 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: