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 CacheInvalidator |
||
39 | { |
||
40 | /** |
||
41 | * Value to check support of invalidatePath operation. |
||
42 | */ |
||
43 | const PATH = 'path'; |
||
44 | |||
45 | /** |
||
46 | * Value to check support of refreshPath operation. |
||
47 | */ |
||
48 | const REFRESH = 'refresh'; |
||
49 | |||
50 | /** |
||
51 | * Value to check support of invalidate and invalidateRegex operations. |
||
52 | */ |
||
53 | const INVALIDATE = 'invalidate'; |
||
54 | |||
55 | /** |
||
56 | * Value to check support of invalidateTags operation. |
||
57 | */ |
||
58 | const TAGS = 'tags'; |
||
59 | |||
60 | /** |
||
61 | * Value to check support of clearCache operation. |
||
62 | */ |
||
63 | const CLEAR = 'clear'; |
||
64 | |||
65 | /** |
||
66 | * @var ProxyClient |
||
67 | */ |
||
68 | private $cache; |
||
69 | |||
70 | /** |
||
71 | 12 | * @var EventDispatcherInterface |
|
72 | */ |
||
73 | 12 | private $eventDispatcher; |
|
74 | 12 | ||
75 | /** |
||
76 | * Constructor. |
||
77 | * |
||
78 | * @param ProxyClient $cache HTTP cache |
||
79 | */ |
||
80 | public function __construct(ProxyClient $cache) |
||
84 | |||
85 | /** |
||
86 | * Check whether this invalidator instance supports the specified |
||
87 | * operation. |
||
88 | * |
||
89 | * Support for PATH means invalidatePath will work, REFRESH means |
||
90 | 3 | * refreshPath works, TAGS means that invalidateTags works and |
|
91 | * INVALIDATE is for the invalidate and invalidateRegex methods. |
||
92 | * |
||
93 | 3 | * @param string $operation one of the class constants |
|
94 | 2 | * |
|
95 | 3 | * @return bool |
|
96 | 2 | * |
|
97 | 3 | * @throws InvalidArgumentException |
|
98 | 2 | */ |
|
99 | 3 | public function supports($operation) |
|
121 | |||
122 | 2 | /** |
|
123 | 2 | * Set event dispatcher - may only be called once. |
|
124 | * |
||
125 | * @param EventDispatcherInterface $eventDispatcher |
||
126 | * |
||
127 | * @throws \Exception when trying to override the event dispatcher |
||
128 | */ |
||
129 | public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) |
||
138 | |||
139 | /** |
||
140 | * Get the event dispatcher used by the cache invalidator. |
||
141 | * |
||
142 | * @return EventDispatcherInterface |
||
143 | */ |
||
144 | View Code Duplication | public function getEventDispatcher() |
|
156 | |||
157 | 1 | /** |
|
158 | * Invalidate a path or URL. |
||
159 | * |
||
160 | * @param string $path Path or URL |
||
161 | * @param array $headers HTTP headers (optional) |
||
162 | * |
||
163 | * @throws UnsupportedProxyOperationException |
||
164 | * |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function invalidatePath($path, array $headers = []) |
||
177 | |||
178 | 1 | /** |
|
179 | * Refresh a path or URL. |
||
180 | 1 | * |
|
181 | * @param string $path Path or URL |
||
182 | * @param array $headers HTTP headers (optional) |
||
183 | * |
||
184 | * @see RefreshCapable::refresh() |
||
185 | * |
||
186 | * @throws UnsupportedProxyOperationException |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function refreshPath($path, array $headers = []) |
||
200 | |||
201 | /** |
||
202 | * Invalidate all cached objects matching the provided HTTP headers. |
||
203 | 1 | * |
|
204 | * Each header is a a POSIX regular expression, for example |
||
205 | 1 | * ['X-Host' => '^(www\.)?(this|that)\.com$'] |
|
206 | * |
||
207 | * @see BanCapable::ban() |
||
208 | * |
||
209 | * @param array $headers HTTP headers that path must match to be banned |
||
210 | * |
||
211 | * @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests |
||
212 | * |
||
213 | * @return $this |
||
214 | */ |
||
215 | public function invalidate(array $headers) |
||
225 | |||
226 | 2 | /** |
|
227 | * Remove/Expire cache objects based on cache tags. |
||
228 | * |
||
229 | * @see TagCapable::tags() |
||
230 | * |
||
231 | * @param array $tags Tags that should be removed/expired from the cache |
||
232 | * |
||
233 | * @throws UnsupportedProxyOperationException If HTTP cache does not support Tags invalidation |
||
234 | * |
||
235 | * @return $this |
||
236 | */ |
||
237 | public function invalidateTags(array $tags) |
||
246 | |||
247 | /** |
||
248 | * Invalidate URLs based on a regular expression for the URI, an optional |
||
249 | * content type and optional limit to certain hosts. |
||
250 | * |
||
251 | 1 | * The hosts parameter can either be a regular expression, e.g. |
|
252 | * '^(www\.)?(this|that)\.com$' or an array of exact host names, e.g. |
||
253 | 1 | * ['example.com', 'other.net']. If the parameter is empty, all hosts |
|
254 | * are matched. |
||
255 | * |
||
256 | * @see BanCapable::banPath() |
||
257 | 1 | * |
|
258 | * @param string $path Regular expression pattern for URI to |
||
259 | 1 | * invalidate |
|
260 | * @param string $contentType Regular expression pattern for the content |
||
261 | * type to limit banning, for instance 'text' |
||
262 | * @param array|string $hosts Regular expression of a host name or list of |
||
263 | * exact host names to limit banning |
||
264 | * |
||
265 | * @throws UnsupportedProxyOperationException If HTTP cache does not support BAN requests |
||
266 | * |
||
267 | * @return $this |
||
268 | */ |
||
269 | 3 | public function invalidateRegex($path, $contentType = null, $hosts = null) |
|
279 | 1 | ||
280 | 1 | /** |
|
281 | * Clear the cache completely. |
||
282 | * |
||
283 | * @throws UnsupportedProxyOperationException if HTTP cache does not support clearing the cache completely |
||
284 | 1 | * |
|
285 | * @return $this |
||
286 | */ |
||
287 | public function clearCache() |
||
297 | |||
298 | /** |
||
299 | * Send all pending invalidation requests. |
||
300 | * |
||
301 | * @return int the number of cache invalidations performed per caching server |
||
302 | * |
||
303 | * @throws ExceptionCollection if any errors occurred during flush |
||
304 | */ |
||
305 | public function flush() |
||
323 | |||
324 | private function dispatch(Event $event, $eventName) |
||
335 | } |
||
336 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.