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 |
||
12 | class Swift_CharacterStream_MbCharacterStream implements Swift_CharacterStream |
||
|
|||
13 | { |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $_charset = 'utf-8'; |
||
19 | |||
20 | /** |
||
21 | * @var int |
||
22 | */ |
||
23 | private $_strpos = 0; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $_buffer = ''; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | private $_strlen = 0; |
||
34 | |||
35 | 62 | public function flushContents() |
|
40 | |||
41 | /** |
||
42 | * @param Swift_OutputByteStream $os |
||
43 | */ |
||
44 | 4 | View Code Duplication | public function importByteStream(\Swift_OutputByteStream $os) |
53 | |||
54 | /** |
||
55 | * @param string $string |
||
56 | */ |
||
57 | 58 | public function importString($string) |
|
62 | |||
63 | /** |
||
64 | * @param int $length |
||
65 | * |
||
66 | * @return false|string |
||
67 | */ |
||
68 | 59 | public function read($length) |
|
69 | { |
||
70 | 59 | if ($this->_strpos >= $this->_strlen) { |
|
71 | 56 | return false; |
|
72 | } |
||
73 | |||
74 | 59 | $readChars = min($length, $this->_strlen - $this->_strpos); |
|
75 | |||
76 | 59 | $ret = UTF8::substr($this->_buffer, $this->_strpos, $readChars, $this->_charset); |
|
77 | |||
78 | 59 | $this->_strpos += $readChars; |
|
79 | |||
80 | 59 | return $ret; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param int $length |
||
85 | * |
||
86 | * @return int[]|bool |
||
87 | */ |
||
88 | 53 | public function readBytes($length) |
|
98 | |||
99 | /** |
||
100 | * @param Swift_CharacterReaderFactory $factory |
||
101 | */ |
||
102 | public function setCharacterReaderFactory(\Swift_CharacterReaderFactory $factory) |
||
106 | |||
107 | /** |
||
108 | * @param string $charset |
||
109 | */ |
||
110 | 126 | public function setCharacterSet($charset) |
|
116 | |||
117 | /** |
||
118 | * @param int $charOffset |
||
119 | */ |
||
120 | 1 | public function setPointer($charOffset) |
|
121 | { |
||
122 | 1 | $this->_strpos = $charOffset; |
|
123 | 1 | } |
|
124 | |||
125 | /** |
||
126 | * @param string $chars |
||
127 | */ |
||
128 | 62 | public function write($chars) |
|
133 | } |
||
134 |
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.