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 |
||
38 | class Varnish extends HttpProxyClient implements BanCapable, PurgeCapable, RefreshCapable, TagCapable |
||
39 | { |
||
40 | const HTTP_METHOD_BAN = 'BAN'; |
||
41 | |||
42 | const HTTP_METHOD_PURGE = 'PURGE'; |
||
43 | |||
44 | const HTTP_METHOD_PURGEKEYS = 'PURGEKEYS'; |
||
45 | |||
46 | const HTTP_METHOD_REFRESH = 'GET'; |
||
47 | |||
48 | const HTTP_HEADER_HOST = 'X-Host'; |
||
49 | |||
50 | const HTTP_HEADER_URL = 'X-Url'; |
||
51 | |||
52 | const HTTP_HEADER_CONTENT_TYPE = 'X-Content-Type'; |
||
53 | |||
54 | const TAG_BAN = 'ban'; |
||
55 | |||
56 | const TAG_XKEY = 'purgekeys'; |
||
57 | 4 | ||
58 | /** |
||
59 | 4 | * Default name of the header used to invalidate content with specific tags. |
|
60 | * |
||
61 | 4 | * This happens to be the same as TagHeaderFormatter::DEFAULT_HEADER_NAME |
|
62 | * but does not technically need to be the same. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | 1 | const DEFAULT_HTTP_HEADER_CACHE_TAGS = 'X-Cache-Tags'; |
|
67 | 1 | ||
68 | const DEFAULT_HTTP_HEADER_CACHE_XKEY = 'xkey-softpurge'; |
||
69 | 3 | ||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | 4 | */ |
|
73 | 4 | public function invalidateTags(array $tags) |
|
74 | 4 | { |
|
75 | $banMode = self::TAG_BAN === $this->options['tag_mode']; |
||
76 | $escapedTags = array_map('preg_quote', $this->escapeTags($tags)); |
||
77 | 4 | ||
78 | $chunkSize = $this->determineTagsPerHeader($escapedTags, $banMode ? '|' : ' '); |
||
79 | |||
80 | foreach (array_chunk($escapedTags, $chunkSize) as $tagchunk) { |
||
81 | if ($banMode) { |
||
82 | $this->invalidateByBan($tagchunk); |
||
83 | 10 | } else { |
|
84 | $this->invalidateByPurgekeys($tagchunk); |
||
85 | 10 | } |
|
86 | 10 | } |
|
87 | 10 | ||
88 | return $this; |
||
89 | } |
||
90 | 10 | ||
91 | /** |
||
92 | 10 | * {@inheritdoc} |
|
93 | */ |
||
94 | public function ban(array $headers) |
||
95 | { |
||
96 | $headers = array_merge( |
||
97 | $this->options['default_ban_headers'], |
||
98 | 4 | $headers |
|
99 | ); |
||
100 | 4 | ||
101 | 2 | $this->queueRequest(self::HTTP_METHOD_BAN, '/', $headers, false); |
|
102 | 1 | ||
103 | return $this; |
||
104 | 1 | } |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | 3 | */ |
|
109 | public function banPath($path, $contentType = null, $hosts = null) |
||
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | 3 | */ |
|
135 | public function purge($url, array $headers = []) |
||
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 19 | View Code Duplication | public function refresh($url, array $headers = []) |
152 | |||
153 | 19 | /** |
|
154 | 19 | * {@inheritdoc} |
|
155 | */ |
||
156 | 19 | protected function configureOptions() |
|
186 | |||
187 | private function invalidateByBan(array $tagchunk) |
||
192 | |||
193 | private function invalidateByPurgekeys(array $tagchunk) |
||
202 | } |
||
203 |
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: