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 |
||
46 | class BrowscapCache implements BrowscapCacheInterface |
||
47 | { |
||
48 | /** |
||
49 | * Path to the cache directory |
||
50 | * |
||
51 | * @var \WurflCache\Adapter\AdapterInterface |
||
52 | */ |
||
53 | private $cache = null; |
||
54 | |||
55 | /** |
||
56 | * Detected browscap version (read from INI file) |
||
57 | * |
||
58 | * @var int |
||
59 | */ |
||
60 | private $version = null; |
||
61 | |||
62 | /** |
||
63 | * Release date of the Browscap data (read from INI file) |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | private $releaseDate; |
||
68 | |||
69 | /** |
||
70 | * Type of the Browscap data (read from INI file) |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | private $type; |
||
75 | |||
76 | /** |
||
77 | * Constructor class, checks for the existence of (and loads) the cache and |
||
78 | * if needed updated the definitions |
||
79 | * |
||
80 | * @param \WurflCache\Adapter\AdapterInterface $adapter |
||
81 | * @param int $updateInterval |
||
82 | */ |
||
83 | 17 | public function __construct(AdapterInterface $adapter, $updateInterval = BrowscapCacheInterface::CACHE_LIVETIME) |
|
88 | |||
89 | /** |
||
90 | * Gets the version of the Browscap data |
||
91 | * |
||
92 | * @return int |
||
93 | */ |
||
94 | 6 | View Code Duplication | public function getVersion() |
108 | |||
109 | /** |
||
110 | * Gets the release date of the Browscap data |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | 1 | public function getReleaseDate() |
|
128 | |||
129 | /** |
||
130 | * Gets the type of the Browscap data |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 1 | View Code Duplication | public function getType() |
148 | |||
149 | /** |
||
150 | * Get an item. |
||
151 | * |
||
152 | * @param string $cacheId |
||
153 | * @param bool $withVersion |
||
154 | * @param bool $success |
||
155 | * |
||
156 | * @return mixed Data on success, null on failure |
||
157 | */ |
||
158 | 7 | public function getItem($cacheId, $withVersion = true, & $success = null) |
|
189 | |||
190 | /** |
||
191 | * save the content into an php file |
||
192 | * |
||
193 | * @param string $cacheId The cache id |
||
194 | * @param mixed $content The content to store |
||
195 | * @param bool $withVersion |
||
196 | * |
||
197 | * @return boolean whether the file was correctly written to the disk |
||
198 | */ |
||
199 | 6 | public function setItem($cacheId, $content, $withVersion = true) |
|
213 | |||
214 | /** |
||
215 | * Test if an item exists. |
||
216 | * |
||
217 | * @param string $cacheId |
||
218 | * @param bool $withVersion |
||
219 | * |
||
220 | * @return bool |
||
221 | */ |
||
222 | public function hasItem($cacheId, $withVersion = true) |
||
223 | { |
||
224 | if ($withVersion) { |
||
225 | $cacheId .= '.'.$this->getVersion(); |
||
226 | } |
||
227 | |||
228 | return $this->cache->hasItem($cacheId); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Remove an item. |
||
233 | * |
||
234 | * @param string $cacheId |
||
235 | * @param bool $withVersion |
||
236 | * |
||
237 | * @return bool |
||
238 | */ |
||
239 | public function removeItem($cacheId, $withVersion = true) |
||
247 | |||
248 | /** |
||
249 | * Flush the whole storage |
||
250 | * |
||
251 | * @return bool |
||
252 | */ |
||
253 | public function flush() |
||
257 | } |
||
258 |
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.