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_CharacterStream_ArrayCharacterStream 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_CharacterStream_ArrayCharacterStream, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_CharacterStream_ArrayCharacterStream implements Swift_CharacterStream |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * A map of byte values and their respective characters |
||
20 | * |
||
21 | * @var array |
||
22 | */ |
||
23 | private static $_charMap; |
||
24 | |||
25 | /** |
||
26 | * A map of characters and their derivative byte values |
||
27 | * |
||
28 | * @var array |
||
29 | */ |
||
30 | private static $_byteMap; |
||
31 | |||
32 | /** |
||
33 | * The char reader (lazy-loaded) for the current charset |
||
34 | * |
||
35 | * @var Swift_CharacterReader |
||
36 | */ |
||
37 | private $_charReader; |
||
38 | |||
39 | /** |
||
40 | * A factory for creating CharacterReader instance |
||
41 | * |
||
42 | * @var Swift_CharacterReaderFactory |
||
43 | */ |
||
44 | private $_charReaderFactory; |
||
45 | |||
46 | /** |
||
47 | * The character set this stream is using |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | private $_charset; |
||
52 | |||
53 | /** |
||
54 | * Array of characters |
||
55 | * |
||
56 | * @var array |
||
57 | */ |
||
58 | private $_array = array(); |
||
59 | |||
60 | /** |
||
61 | * Size of the array of character |
||
62 | * |
||
63 | * @var int |
||
64 | */ |
||
65 | private $_array_size = 0; |
||
66 | |||
67 | /** |
||
68 | * The current character offset in the stream |
||
69 | * |
||
70 | * @var int |
||
71 | */ |
||
72 | private $_offset = 0; |
||
73 | |||
74 | /** |
||
75 | * Create a new CharacterStream with the given $chars, if set. |
||
76 | * |
||
77 | * @param Swift_CharacterReaderFactory $factory for loading validators |
||
78 | * @param string $charset used in the stream |
||
79 | */ |
||
80 | 54 | public function __construct(Swift_CharacterReaderFactory $factory, $charset) |
|
81 | { |
||
82 | 54 | self::_initializeMaps(); |
|
83 | 54 | $this->setCharacterReaderFactory($factory); |
|
84 | 54 | $this->setCharacterSet($charset); |
|
85 | 54 | } |
|
86 | |||
87 | /** |
||
88 | * Set the character set used in this CharacterStream. |
||
89 | * |
||
90 | * @param string $charset |
||
91 | */ |
||
92 | 54 | public function setCharacterSet($charset) |
|
93 | { |
||
94 | 54 | $this->_charset = $charset; |
|
95 | 54 | $this->_charReader = null; |
|
96 | 54 | } |
|
97 | |||
98 | /** |
||
99 | * Set the CharacterReaderFactory for multi charset support. |
||
100 | * |
||
101 | * @param Swift_CharacterReaderFactory $factory |
||
102 | */ |
||
103 | 54 | public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory) |
|
107 | |||
108 | /** |
||
109 | * Overwrite this character stream using the byte sequence in the byte stream. |
||
110 | * |
||
111 | * @param Swift_OutputByteStream $os output stream to read from |
||
112 | */ |
||
113 | 2 | public function importByteStream(Swift_OutputByteStream $os) |
|
144 | |||
145 | /** |
||
146 | * Import a string a bytes into this CharacterStream, overwriting any existing |
||
147 | * data in the stream. |
||
148 | * |
||
149 | * @param string $string |
||
150 | */ |
||
151 | 17 | public function importString($string) |
|
156 | |||
157 | /** |
||
158 | * Read $length characters from the stream and move the internal pointer |
||
159 | * $length further into the stream. |
||
160 | * |
||
161 | * @param int $length |
||
162 | * |
||
163 | * @return false|string false on error |
||
164 | */ |
||
165 | 7 | public function read($length) |
|
190 | |||
191 | /** |
||
192 | * Read $length characters from the stream and return a 1-dimensional array |
||
193 | * containing there octet values. |
||
194 | * |
||
195 | * @param int $length |
||
196 | * |
||
197 | * @return integer[] |
||
198 | */ |
||
199 | 9 | public function readBytes($length) |
|
218 | |||
219 | /** |
||
220 | * Write $chars to the end of the stream. |
||
221 | * |
||
222 | * @param string $chars |
||
223 | */ |
||
224 | 17 | public function write($chars) |
|
284 | |||
285 | /** |
||
286 | * Move the internal pointer to $charOffset in the stream. |
||
287 | * |
||
288 | * @param int $charOffset |
||
289 | */ |
||
290 | 1 | public function setPointer($charOffset) |
|
299 | |||
300 | /** |
||
301 | * Empty the stream and reset the internal pointer. |
||
302 | */ |
||
303 | 17 | public function flushContents() |
|
309 | |||
310 | /** |
||
311 | * @param resource $fp |
||
312 | * @param int $len |
||
313 | * |
||
314 | * @return array|false false on error |
||
315 | */ |
||
316 | 17 | private function _reloadBuffer($fp, $len) |
|
333 | |||
334 | 54 | private static function _initializeMaps() |
|
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.