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:
Complex classes like Swift_KeyCache_DiskKeyCache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Swift_KeyCache_DiskKeyCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_KeyCache_DiskKeyCache implements Swift_KeyCache |
||
|
|
|||
| 17 | { |
||
| 18 | /** Signal to place pointer at start of file */ |
||
| 19 | const POSITION_START = 0; |
||
| 20 | |||
| 21 | /** Signal to place pointer at end of file */ |
||
| 22 | const POSITION_END = 1; |
||
| 23 | |||
| 24 | /** Signal to leave pointer in whatever position it currently is */ |
||
| 25 | const POSITION_CURRENT = 2; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * An InputStream for cloning. |
||
| 29 | * |
||
| 30 | * @var Swift_KeyCache_KeyCacheInputStream |
||
| 31 | */ |
||
| 32 | private $_stream; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * A path to write to. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $_path; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Stored keys. |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | private $_keys = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Will be true if magic_quotes_runtime is turned on. |
||
| 50 | * |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | private $_quotes = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Create a new DiskKeyCache with the given $stream for cloning to make |
||
| 57 | * InputByteStreams, and the given $path to save to. |
||
| 58 | * |
||
| 59 | * @param Swift_KeyCache_KeyCacheInputStream $stream |
||
| 60 | * @param string $path to save to |
||
| 61 | */ |
||
| 62 | 14 | public function __construct(Swift_KeyCache_KeyCacheInputStream $stream, $path) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Set a string into the cache under $itemKey for the namespace $nsKey. |
||
| 74 | * |
||
| 75 | * @see MODE_WRITE, MODE_APPEND |
||
| 76 | * |
||
| 77 | * @param string $nsKey |
||
| 78 | * @param string $itemKey |
||
| 79 | * @param string $string |
||
| 80 | * @param int $mode |
||
| 81 | * |
||
| 82 | * @throws Swift_IoException |
||
| 83 | * @throws Swift_SwiftException |
||
| 84 | */ |
||
| 85 | 78 | public function setString($nsKey, $itemKey, $string, $mode) |
|
| 107 | |||
| 108 | /** |
||
| 109 | * Set a ByteStream into the cache under $itemKey for the namespace $nsKey. |
||
| 110 | * |
||
| 111 | * @see MODE_WRITE, MODE_APPEND |
||
| 112 | * |
||
| 113 | * @param string $nsKey |
||
| 114 | * @param string $itemKey |
||
| 115 | * @param Swift_OutputByteStream $os |
||
| 116 | * @param int $mode |
||
| 117 | * |
||
| 118 | * @throws Swift_IoException |
||
| 119 | */ |
||
| 120 | 3 | public function importFromByteStream($nsKey, $itemKey, Swift_OutputByteStream $os, $mode) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Provides a ByteStream which when written to, writes data to $itemKey. |
||
| 147 | * |
||
| 148 | * NOTE: The stream will always write in append mode. |
||
| 149 | * |
||
| 150 | * @param string $nsKey |
||
| 151 | * @param string $itemKey |
||
| 152 | * @param Swift_InputByteStream $writeThrough |
||
| 153 | * |
||
| 154 | * @return Swift_InputByteStream |
||
| 155 | */ |
||
| 156 | 18 | View Code Duplication | public function getInputByteStream($nsKey, $itemKey, Swift_InputByteStream $writeThrough = null) |
| 170 | |||
| 171 | /** |
||
| 172 | * Get data back out of the cache as a string. |
||
| 173 | * |
||
| 174 | * @param string $nsKey |
||
| 175 | * @param string $itemKey |
||
| 176 | * |
||
| 177 | * @throws Swift_IoException |
||
| 178 | * |
||
| 179 | * @return string |
||
| 180 | */ |
||
| 181 | 11 | public function getString($nsKey, $itemKey) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Get data back out of the cache as a ByteStream. |
||
| 207 | * |
||
| 208 | * @param string $nsKey |
||
| 209 | * @param string $itemKey |
||
| 210 | * @param Swift_InputByteStream $is to write the data to |
||
| 211 | */ |
||
| 212 | 4 | public function exportToByteStream($nsKey, $itemKey, Swift_InputByteStream $is) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Check if the given $itemKey exists in the namespace $nsKey. |
||
| 235 | * |
||
| 236 | * @param string $nsKey |
||
| 237 | * @param string $itemKey |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | 175 | public function hasKey($nsKey, $itemKey) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Clear data for $itemKey in the namespace $nsKey if it exists. |
||
| 248 | * |
||
| 249 | * @param string $nsKey |
||
| 250 | * @param string $itemKey |
||
| 251 | */ |
||
| 252 | 164 | public function clearKey($nsKey, $itemKey) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Clear all data in the namespace $nsKey if it exists. |
||
| 262 | * |
||
| 263 | * @param string $nsKey |
||
| 264 | */ |
||
| 265 | 161 | public function clearAll($nsKey) |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Initialize the namespace of $nsKey if needed. |
||
| 282 | * |
||
| 283 | * @param string $nsKey |
||
| 284 | */ |
||
| 285 | 80 | private function _prepareCache($nsKey) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Get a file handle on the cache item. |
||
| 298 | * |
||
| 299 | * @param string $nsKey |
||
| 300 | * @param string $itemKey |
||
| 301 | * @param int $position |
||
| 302 | * |
||
| 303 | * @return resource |
||
| 304 | */ |
||
| 305 | 80 | private function _getHandle($nsKey, $itemKey, $position) |
|
| 306 | { |
||
| 307 | 80 | $position = (int)$position; |
|
| 308 | |||
| 309 | 80 | if (!isset($this->_keys[$nsKey][$itemKey])) { |
|
| 310 | 80 | $openMode = $this->hasKey($nsKey, $itemKey) ? 'r+b' : 'w+b'; |
|
| 311 | 80 | $fp = fopen($this->_path . '/' . $nsKey . '/' . $itemKey, $openMode); |
|
| 312 | 80 | $this->_keys[$nsKey][$itemKey] = $fp; |
|
| 313 | 80 | } |
|
| 314 | |||
| 315 | 80 | if (self::POSITION_START === $position) { |
|
| 316 | 69 | fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_SET); |
|
| 317 | 80 | } elseif (self::POSITION_END === $position) { |
|
| 318 | 21 | fseek($this->_keys[$nsKey][$itemKey], 0, SEEK_END); |
|
| 319 | 21 | } |
|
| 320 | |||
| 321 | 80 | return $this->_keys[$nsKey][$itemKey]; |
|
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * @param string $nsKey |
||
| 326 | * @param string $itemKey |
||
| 327 | */ |
||
| 328 | 80 | private function _freeHandle($nsKey, $itemKey) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Destructor. |
||
| 337 | */ |
||
| 338 | public function __destruct() |
||
| 344 | } |
||
| 345 |
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.