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