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 |
||
41 | class LocalCacheAdapter implements CacheAdapterInterface |
||
42 | { |
||
43 | |||
44 | /** |
||
45 | * Trait that provides custom cache adapter functionality. |
||
46 | * |
||
47 | * @var TechDivision\Import\Cache\CacheAdapterTrait |
||
48 | */ |
||
49 | use CacheAdapterTrait; |
||
50 | |||
51 | /** |
||
52 | * The array with the tags. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | protected $tags = array(); |
||
57 | |||
58 | /** |
||
59 | * The cache for the query results. |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | protected $cache = array(); |
||
64 | |||
65 | /** |
||
66 | * References that links to another cache entry. |
||
67 | * |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $references = array(); |
||
71 | |||
72 | /** |
||
73 | * The cache key utility instance. |
||
74 | * |
||
75 | * @var \TechDivision\Import\Utils\CacheKeyUtilInterface |
||
76 | */ |
||
77 | protected $cacheKeyUtil; |
||
78 | |||
79 | /** |
||
80 | * Initialize the cache handler with the passed cache and configuration instances. |
||
81 | * |
||
82 | * @param \TechDivision\Import\Utils\CacheKeyUtilInterface $cacheKeyUtil The cache key utility instance |
||
83 | */ |
||
84 | public function __construct(CacheKeyUtilInterface $cacheKeyUtil) |
||
88 | |||
89 | /** |
||
90 | * Creates a unique cache key from the passed data. |
||
91 | * |
||
92 | * @param mixed $data The date to create the cache key from |
||
93 | * |
||
94 | * @return string The generated cache key |
||
95 | */ |
||
96 | public function cacheKey($data) |
||
100 | |||
101 | /** |
||
102 | * Query whether or not a cache value for the passed cache key is available. |
||
103 | * |
||
104 | * @param string $key The cache key to query for |
||
105 | * |
||
106 | * @return boolean TRUE if the a value is available, else FALSE |
||
107 | */ |
||
108 | public function isCached($key) |
||
119 | |||
120 | /** |
||
121 | * Inversion of the isCached() method. |
||
122 | * |
||
123 | * @param string $key The cache key to query for |
||
124 | * |
||
125 | * @return boolean TRUE if the value is not available, else FALSE |
||
126 | */ |
||
127 | public function notCached($key) |
||
131 | |||
132 | /** |
||
133 | * Add's a cache reference from one key to another. |
||
134 | * |
||
135 | * @param string $from The key to reference from |
||
136 | * @param string $to The key to reference to |
||
137 | * |
||
138 | * @return void |
||
139 | */ |
||
140 | public function addReference($from, $to) |
||
144 | |||
145 | /** |
||
146 | * Resolve's the cache key. |
||
147 | * |
||
148 | * @param string $from The cache key to resolve |
||
149 | * |
||
150 | * @return string The resolved reference |
||
151 | */ |
||
152 | protected function resolveReference($from) |
||
163 | |||
164 | /** |
||
165 | * Add the passed item to the cache. |
||
166 | * |
||
167 | * @param string $key The cache key to use |
||
168 | * @param mixed $value The value that has to be cached |
||
169 | * @param array $references An array with references to add |
||
170 | * @param array $tags An array with tags to add |
||
171 | * @param boolean $override Flag that allows to override an exising cache entry |
||
172 | * @param integer $time The TTL in seconds for the passed item |
||
173 | * |
||
174 | * @return void |
||
175 | */ |
||
176 | public function toCache($key, $value, array $references = array(), array $tags = array(), $override = false, $time = null) |
||
207 | |||
208 | /** |
||
209 | * Returns a new cache item for the passed key |
||
210 | * |
||
211 | * @param string $key The cache key to return the item for |
||
212 | * |
||
213 | * @return mixed The value for the passed key |
||
214 | */ |
||
215 | public function fromCache($key) |
||
221 | |||
222 | /** |
||
223 | * Flush the cache and remove the references. |
||
224 | * |
||
225 | * @return void |
||
226 | */ |
||
227 | public function flushCache() |
||
233 | |||
234 | /** |
||
235 | * Invalidate the cache entries for the passed tags. |
||
236 | * |
||
237 | * @param array $tags The tags to invalidate the cache for |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | public function invalidateTags(array $tags) |
||
260 | |||
261 | /** |
||
262 | * Remove the item with the passed key and all its references from the cache. |
||
263 | * |
||
264 | * @param string $key The key of the cache item to Remove |
||
265 | * |
||
266 | * @return void |
||
267 | */ |
||
268 | public function removeCache($key) |
||
279 | } |
||
280 |
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.