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_Transport_StreamBuffer 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_Transport_StreamBuffer, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Swift_Transport_StreamBuffer extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_Transport_IoBuffer |
||
|
|||
17 | { |
||
18 | /** |
||
19 | * A primary socket |
||
20 | * |
||
21 | * @var resource |
||
22 | */ |
||
23 | private $_stream; |
||
24 | |||
25 | /** |
||
26 | * The input stream |
||
27 | */ |
||
28 | private $_in; |
||
29 | |||
30 | /** |
||
31 | * The output stream |
||
32 | */ |
||
33 | private $_out; |
||
34 | |||
35 | /** |
||
36 | * Buffer initialization parameters |
||
37 | */ |
||
38 | private $_params = array(); |
||
39 | |||
40 | /** |
||
41 | * The ReplacementFilterFactory |
||
42 | * |
||
43 | * @var Swift_ReplacementFilterFactory |
||
44 | */ |
||
45 | private $_replacementFactory; |
||
46 | |||
47 | /** |
||
48 | * Translations performed on data being streamed into the buffer |
||
49 | * |
||
50 | * @var array |
||
51 | */ |
||
52 | private $_translations = array(); |
||
53 | |||
54 | /** |
||
55 | * Create a new StreamBuffer using $replacementFactory for transformations. |
||
56 | * |
||
57 | * @param Swift_ReplacementFilterFactory $replacementFactory |
||
58 | */ |
||
59 | 8 | public function __construct(Swift_ReplacementFilterFactory $replacementFactory) |
|
63 | |||
64 | /** |
||
65 | * Perform any initialization needed, using the given $params. |
||
66 | * |
||
67 | * Parameters will vary depending upon the type of IoBuffer used. |
||
68 | * |
||
69 | * @param array $params |
||
70 | */ |
||
71 | 5 | public function initialize(array $params) |
|
84 | |||
85 | /** |
||
86 | * Set an individual param on the buffer (e.g. switching to SSL). |
||
87 | * |
||
88 | * @param string $param |
||
89 | * @param integer $value |
||
90 | */ |
||
91 | public function setParam($param, $value) |
||
110 | |||
111 | /** |
||
112 | * @return mixed <p>Return <strong>true</strong> on success, <strong>false</strong> if negotiation has failed or <strong>0</strong> if there isn't enough data and you should try again.</p> |
||
113 | */ |
||
114 | public function startTLS() |
||
118 | |||
119 | /** |
||
120 | * Perform any shutdown logic needed. |
||
121 | */ |
||
122 | 4 | public function terminate() |
|
141 | |||
142 | /** |
||
143 | * Set an array of string replacements which should be made on data written |
||
144 | * to the buffer. |
||
145 | * |
||
146 | * This could replace LF with CRLF for example. |
||
147 | * |
||
148 | * @param string[] $replacements |
||
149 | */ |
||
150 | 6 | public function setWriteTranslations(array $replacements) |
|
169 | |||
170 | /** |
||
171 | * Get a line of output (including any CRLF). |
||
172 | * |
||
173 | * The $sequence number comes from any writes and may or may not be used |
||
174 | * depending upon the implementation. |
||
175 | * |
||
176 | * @param int $sequence of last write to scan from |
||
177 | * |
||
178 | * @throws Swift_IoException |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | 5 | View Code Duplication | public function readLine($sequence) |
203 | |||
204 | /** |
||
205 | * Reads $length bytes from the stream into a string and moves the pointer |
||
206 | * through the stream by $length. |
||
207 | * |
||
208 | * If less bytes exist than are requested the remaining bytes are given instead. |
||
209 | * If no bytes are remaining at all, boolean false is returned. |
||
210 | * |
||
211 | * @param int $length |
||
212 | * |
||
213 | * @throws Swift_IoException |
||
214 | * |
||
215 | * @return string|null |
||
216 | */ |
||
217 | View Code Duplication | public function read($length) |
|
240 | |||
241 | /** |
||
242 | * WARNING: Not implemented |
||
243 | * |
||
244 | * @param int $byteOffset |
||
245 | */ |
||
246 | public function setReadPointer($byteOffset) |
||
249 | |||
250 | /** Flush the stream contents */ |
||
251 | 4 | protected function _flush() |
|
257 | |||
258 | /** |
||
259 | * Write this bytes to the stream |
||
260 | * |
||
261 | * @param string $bytes |
||
262 | * |
||
263 | * @return int |
||
264 | * |
||
265 | * @throws Swift_IoException |
||
266 | */ |
||
267 | 4 | protected function _commit($bytes) |
|
295 | |||
296 | /** |
||
297 | * Establishes a connection to a remote server. |
||
298 | * |
||
299 | * @throws Swift_TransportException |
||
300 | */ |
||
301 | 5 | private function _establishSocketConnection() |
|
347 | |||
348 | /** |
||
349 | * Opens a process for input/output. |
||
350 | */ |
||
351 | private function _establishProcessConnection() |
||
372 | |||
373 | /** |
||
374 | * @return string |
||
375 | */ |
||
376 | 1 | private function _getReadConnectionDescription() |
|
395 | } |
||
396 |
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.