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 |
||
16 | class Swift_KeyCache_ArrayKeyCache implements Swift_KeyCache |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * Cache contents. |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private $_contents = array(); |
||
24 | |||
25 | /** |
||
26 | * An InputStream for cloning. |
||
27 | * |
||
28 | * @var Swift_KeyCache_KeyCacheInputStream |
||
29 | */ |
||
30 | private $_stream; |
||
31 | |||
32 | /** |
||
33 | * Create a new ArrayKeyCache with the given $stream for cloning to make |
||
34 | * InputByteStreams. |
||
35 | * |
||
36 | * @param Swift_KeyCache_KeyCacheInputStream $stream |
||
37 | */ |
||
38 | 66 | public function __construct(Swift_KeyCache_KeyCacheInputStream $stream) |
|
42 | |||
43 | /** |
||
44 | * Set a string into the cache under $itemKey for the namespace $nsKey. |
||
45 | * |
||
46 | * @see MODE_WRITE, MODE_APPEND |
||
47 | * |
||
48 | * @param string $nsKey |
||
49 | * @param string $itemKey |
||
50 | * @param string $string |
||
51 | * @param int $mode |
||
52 | * |
||
53 | * @throws Swift_SwiftException |
||
54 | */ |
||
55 | 28 | public function setString($nsKey, $itemKey, $string, $mode) |
|
75 | |||
76 | /** |
||
77 | * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
||
78 | * |
||
79 | * @see MODE_WRITE, MODE_APPEND |
||
80 | * |
||
81 | * @param string $nsKey |
||
82 | * @param string $itemKey |
||
83 | * @param Swift_OutputByteStream $os |
||
84 | * @param int $mode |
||
85 | * |
||
86 | * @throws Swift_SwiftException |
||
87 | */ |
||
88 | 6 | public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode) |
|
108 | |||
109 | /** |
||
110 | * Provides a ByteStream which when written to, writes data to $itemKey. |
||
111 | * |
||
112 | * NOTE: The stream will always write in append mode. |
||
113 | * |
||
114 | * @param string $nsKey |
||
115 | * @param string $itemKey |
||
116 | * @param Swift_InputByteStream $writeThrough |
||
117 | * |
||
118 | * @return Swift_InputByteStream |
||
119 | */ |
||
120 | 1 | View Code Duplication | public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null) |
132 | |||
133 | /** |
||
134 | * Get data back out of the cache as a string. |
||
135 | * |
||
136 | * @param string $nsKey |
||
137 | * @param string $itemKey |
||
138 | * |
||
139 | * @return string |
||
140 | */ |
||
141 | 19 | public function getString($nsKey, $itemKey) |
|
148 | |||
149 | /** |
||
150 | * Get data back out of the cache as a ByteStream. |
||
151 | * |
||
152 | * @param string $nsKey |
||
153 | * @param string $itemKey |
||
154 | * @param Swift_InputByteStream $is to write the data to |
||
155 | */ |
||
156 | 2 | public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is) |
|
161 | |||
162 | /** |
||
163 | * Check if the given $itemKey exists in the namespace $nsKey. |
||
164 | * |
||
165 | * @param string $nsKey |
||
166 | * @param string $itemKey |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | 32 | public function hasKey($nsKey, $itemKey) |
|
176 | |||
177 | /** |
||
178 | * Clear data for $itemKey in the namespace $nsKey if it exists. |
||
179 | * |
||
180 | * @param string $nsKey |
||
181 | * @param string $itemKey |
||
182 | */ |
||
183 | 21 | public function clearKey($nsKey, $itemKey) |
|
187 | |||
188 | /** |
||
189 | * Clear all data in the namespace $nsKey if it exists. |
||
190 | * |
||
191 | * @param string $nsKey |
||
192 | */ |
||
193 | 19 | public function clearAll($nsKey) |
|
197 | |||
198 | /** |
||
199 | * Initialize the namespace of $nsKey if needed. |
||
200 | * |
||
201 | * @param string $nsKey |
||
202 | */ |
||
203 | 32 | private function _prepareCache($nsKey) |
|
209 | } |
||
210 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.