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) |
|
56 | { |
||
57 | 28 | $this->_prepareCache($nsKey); |
|
58 | switch ($mode) { |
||
59 | 28 | case self::MODE_WRITE: |
|
60 | 25 | $this->_contents[$nsKey][$itemKey] = $string; |
|
61 | 25 | break; |
|
62 | 5 | case self::MODE_APPEND: |
|
63 | 5 | if (!$this->hasKey($nsKey, $itemKey)) { |
|
64 | 3 | $this->_contents[$nsKey][$itemKey] = ''; |
|
65 | 3 | } |
|
66 | 5 | $this->_contents[$nsKey][$itemKey] .= $string; |
|
67 | 5 | break; |
|
68 | default: |
||
69 | throw new Swift_SwiftException( |
||
70 | 'Invalid mode [' . $mode . '] used to set nsKey=' . |
||
71 | $nsKey . ', itemKey=' . $itemKey |
||
72 | ); |
||
73 | } |
||
74 | 28 | } |
|
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) |
|
89 | { |
||
90 | 6 | $this->_prepareCache($nsKey); |
|
91 | switch ($mode) { |
||
92 | 6 | case self::MODE_WRITE: |
|
93 | 2 | $this->clearKey($nsKey, $itemKey); |
|
94 | 6 | case self::MODE_APPEND: |
|
95 | 6 | if (!$this->hasKey($nsKey, $itemKey)) { |
|
96 | 4 | $this->_contents[$nsKey][$itemKey] = ''; |
|
97 | 4 | } |
|
98 | 6 | while (false !== $bytes = $os->read(8192)) { |
|
99 | 6 | $this->_contents[$nsKey][$itemKey] .= $bytes; |
|
100 | 6 | } |
|
101 | 6 | break; |
|
102 | default: |
||
103 | throw new Swift_SwiftException( |
||
104 | 'Invalid mode [' . $mode . '] used to set nsKey=' . $nsKey . ', itemKey=' . $itemKey |
||
105 | ); |
||
106 | } |
||
107 | 6 | } |
|
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) |
121 | { |
||
122 | 1 | $is = clone $this->_stream; |
|
123 | 1 | $is->setKeyCache($this); |
|
124 | 1 | $is->setNsKey($nsKey); |
|
125 | 1 | $is->setItemKey($itemKey); |
|
126 | 1 | if (isset($writeThrough)) { |
|
127 | $is->setWriteThroughStream($writeThrough); |
||
128 | } |
||
129 | |||
130 | 1 | return $is; |
|
131 | } |
||
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) |
|
142 | { |
||
143 | 19 | $this->_prepareCache($nsKey); |
|
144 | 19 | if ($this->hasKey($nsKey, $itemKey)) { |
|
145 | 19 | return $this->_contents[$nsKey][$itemKey]; |
|
146 | } |
||
147 | } |
||
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) |
|
157 | { |
||
158 | 2 | $this->_prepareCache($nsKey); |
|
159 | 2 | $is->write($this->getString($nsKey, $itemKey)); |
|
160 | 2 | } |
|
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) |
|
171 | { |
||
172 | 32 | $this->_prepareCache($nsKey); |
|
173 | |||
174 | 32 | return isset($this->_contents[$nsKey][$itemKey]); |
|
175 | } |
||
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.