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_CharacterStream_NgCharacterStream implements Swift_CharacterStream |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The char reader (lazy-loaded) for the current charset. |
||
| 20 | * |
||
| 21 | * @var Swift_CharacterReader |
||
| 22 | */ |
||
| 23 | private $_charReader; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * A factory for creating CharacterReader instances. |
||
| 27 | * |
||
| 28 | * @var Swift_CharacterReaderFactory |
||
| 29 | */ |
||
| 30 | private $_charReaderFactory; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The character set this stream is using. |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $_charset; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The data's stored as-is. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | private $_datas = ''; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Number of bytes in the stream. |
||
| 48 | * |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | private $_datasSize = 0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Map. |
||
| 55 | * |
||
| 56 | * @var mixed |
||
| 57 | */ |
||
| 58 | private $_map; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Map Type. |
||
| 62 | * |
||
| 63 | * @var int |
||
| 64 | */ |
||
| 65 | private $_mapType = 0; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Number of characters in the stream. |
||
| 69 | * |
||
| 70 | * @var int |
||
| 71 | */ |
||
| 72 | private $_charCount = 0; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Position in the stream. |
||
| 76 | * |
||
| 77 | * @var int |
||
| 78 | */ |
||
| 79 | private $_currentPos = 0; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Constructor. |
||
| 83 | * |
||
| 84 | * @param Swift_CharacterReaderFactory $factory |
||
| 85 | * @param string $charset |
||
| 86 | */ |
||
| 87 | 3 | public function __construct(Swift_CharacterReaderFactory $factory, $charset) |
|
| 92 | |||
| 93 | /* -- Changing parameters of the stream -- */ |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Set the character set used in this CharacterStream. |
||
| 97 | * |
||
| 98 | * @param string $charset |
||
| 99 | */ |
||
| 100 | 3 | public function setCharacterSet($charset) |
|
| 110 | |||
| 111 | /** |
||
| 112 | 3 | * Set the CharacterReaderFactory for multi charset support. |
|
| 113 | * |
||
| 114 | 3 | * @param Swift_CharacterReaderFactory $factory |
|
| 115 | 3 | */ |
|
| 116 | public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory) |
||
| 120 | 3 | ||
| 121 | /** |
||
| 122 | 3 | * @see Swift_CharacterStream::flushContents() |
|
| 123 | 3 | */ |
|
| 124 | 3 | public function flushContents() |
|
| 132 | |||
| 133 | /** |
||
| 134 | 2 | * @see Swift_CharacterStream::importByteStream() |
|
| 135 | * |
||
| 136 | 2 | * @param Swift_OutputByteStream $os |
|
| 137 | 2 | */ |
|
| 138 | 2 | View Code Duplication | public function importByteStream(Swift_OutputByteStream $os) |
| 148 | |||
| 149 | 1 | /** |
|
| 150 | * @see Swift_CharacterStream::importString() |
||
| 151 | 1 | * |
|
| 152 | 1 | * @param string $string |
|
| 153 | 1 | */ |
|
| 154 | public function importString($string) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @see Swift_CharacterStream::read() |
||
| 162 | 3 | * |
|
| 163 | * @param int $length |
||
| 164 | 3 | * |
|
| 165 | 3 | * @return string|false false on error |
|
| 166 | */ |
||
| 167 | public function read($length) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @see Swift_CharacterStream::readBytes() |
||
| 222 | 3 | * |
|
| 223 | * @param int $length |
||
| 224 | 3 | * |
|
| 225 | 3 | * @return int[]|false false on error |
|
| 226 | 3 | */ |
|
| 227 | public function readBytes($length) |
||
| 236 | |||
| 237 | /** |
||
| 238 | * @see Swift_CharacterStream::setPointer() |
||
| 239 | * |
||
| 240 | * @param int $charOffset |
||
| 241 | */ |
||
| 242 | public function setPointer($charOffset) |
||
| 249 | |||
| 250 | 3 | /** |
|
| 251 | * Set the char-reader for the current charset. |
||
| 252 | 3 | */ |
|
| 253 | 3 | private function setCharReaderForCurrentCharset() |
|
| 261 | 3 | ||
| 262 | 3 | /** |
|
| 263 | 3 | * @see Swift_CharacterStream::write() |
|
| 264 | * |
||
| 265 | * @param string $chars |
||
| 266 | */ |
||
| 267 | 3 | public function write($chars) |
|
| 286 | } |
||
| 287 |
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.