Complex classes like Swift_ByteStream_FileByteStream 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_ByteStream_FileByteStream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Swift_ByteStream_FileByteStream extends Swift_ByteStream_AbstractFilterableInputStream implements Swift_FileStream |
||
|
|
|||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The internal pointer offset |
||
| 20 | * |
||
| 21 | * @var int |
||
| 22 | */ |
||
| 23 | private $_offset = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * The path to the file |
||
| 27 | * |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $_path; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The mode this file is opened in for writing |
||
| 34 | * |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | private $_mode; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * A lazy-loaded resource handle for reading the file |
||
| 41 | * |
||
| 42 | * @var resource |
||
| 43 | */ |
||
| 44 | private $_reader; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * A lazy-loaded resource handle for writing the file |
||
| 48 | * |
||
| 49 | * @var resource |
||
| 50 | */ |
||
| 51 | private $_writer; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * If magic_quotes_runtime is on, this will be true |
||
| 55 | * |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | private $_quotes = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * If stream is seekable true/false, or null if not known |
||
| 62 | * |
||
| 63 | * @var null|boolean |
||
| 64 | */ |
||
| 65 | private $_seekable = null; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Create a new FileByteStream for $path. |
||
| 69 | * |
||
| 70 | * @param string $path |
||
| 71 | * @param bool|false $writable |
||
| 72 | * |
||
| 73 | * @throws Swift_IoException |
||
| 74 | */ |
||
| 75 | 29 | public function __construct($path, $writable = false) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Get the complete path to the file. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 18 | public function getPath() |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Reads $length bytes from the stream into a string and moves the pointer |
||
| 101 | * through the stream by $length. |
||
| 102 | * |
||
| 103 | * If less bytes exist than are requested the |
||
| 104 | * remaining bytes are given instead. If no bytes are remaining at all, boolean |
||
| 105 | * false is returned. |
||
| 106 | * |
||
| 107 | * @param int $length |
||
| 108 | * |
||
| 109 | * @throws Swift_IoException |
||
| 110 | * |
||
| 111 | * @return string|bool |
||
| 112 | */ |
||
| 113 | 21 | public function read($length) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Move the internal read pointer to $byteOffset in the stream. |
||
| 162 | * |
||
| 163 | * @param int $byteOffset |
||
| 164 | * |
||
| 165 | * @return bool |
||
| 166 | */ |
||
| 167 | 16 | public function setReadPointer($byteOffset) |
|
| 174 | |||
| 175 | /** Just write the bytes to the file */ |
||
| 176 | 14 | protected function _commit($bytes) |
|
| 181 | |||
| 182 | /** Not used */ |
||
| 183 | 10 | protected function _flush() |
|
| 186 | |||
| 187 | /** Get the resource for reading */ |
||
| 188 | 21 | private function _getReadHandle() |
|
| 209 | |||
| 210 | /** Get the resource for writing */ |
||
| 211 | 14 | private function _getWriteHandle() |
|
| 227 | |||
| 228 | /** Force a reload of the resource for reading */ |
||
| 229 | 24 | private function _resetReadHandle() |
|
| 236 | |||
| 237 | /** Check if ReadOnly Stream is seekable */ |
||
| 238 | private function _getReadStreamSeekableStatus() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Streams in a readOnly stream ensuring copy if needed |
||
| 246 | * |
||
| 247 | * @param $offset |
||
| 248 | * |
||
| 249 | * @throws Swift_IoException |
||
| 250 | */ |
||
| 251 | private function _seekReadStreamToPosition($offset) |
||
| 268 | |||
| 269 | /** Copy a readOnly Stream to ensure seekability */ |
||
| 270 | private function _copyReadStream() |
||
| 299 | } |
||
| 300 |
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.