Complex classes like Stream 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 Stream, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 37 | class Stream implements Streamable |
||
| 38 | { |
||
| 39 | use IsReadable, IsWritable, IsSeekable; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \CSVelte\IO\Resource A stream resource object |
||
| 43 | */ |
||
| 44 | protected $resource; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int The total size (in bytes) of the stream |
||
| 48 | */ |
||
| 49 | protected $size; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Meta data about stream resource. |
||
| 53 | * Just contains the return value of stream_get_meta_data. |
||
| 54 | * @var array The return value of stream_get_meta_data |
||
| 55 | * @todo Not sure if this belongs in this class or in Resource. I'm leaving |
||
| 56 | * it here for now, simply because I'm worried Stream will become superfluous |
||
| 57 | */ |
||
| 58 | protected $meta; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Instantiate a stream. |
||
| 62 | * |
||
| 63 | * Instantiate a new stream object using a stream resource object. |
||
| 64 | * |
||
| 65 | * @param \CSVelte\IO\Resource $resource A stream resource object |
||
| 66 | */ |
||
| 67 | 79 | public function __construct(Resource $resource) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Set stream resource object |
||
| 74 | * |
||
| 75 | * @param Resource $resource A stream resource object |
||
| 76 | */ |
||
| 77 | 79 | protected function setResource(Resource $resource) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Stream Object Destructor. |
||
| 84 | * |
||
| 85 | * Closes stream connection. |
||
| 86 | */ |
||
| 87 | 79 | public function __destruct() |
|
| 91 | |||
| 92 | /** |
||
| 93 | * Reads all data from the stream into a string, from the beginning to end. |
||
| 94 | * |
||
| 95 | * This method MUST attempt to seek to the beginning of the stream before |
||
| 96 | * reading data and read the stream until the end is reached. |
||
| 97 | * |
||
| 98 | * Warning: This could attempt to load a large amount of data into memory. |
||
| 99 | * |
||
| 100 | * This method MUST NOT raise an exception in order to conform with PHP's |
||
| 101 | * string casting operations. |
||
| 102 | * |
||
| 103 | * Returns the internal pointer to the position it was in once it's finished |
||
| 104 | * |
||
| 105 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring |
||
| 106 | * @return string |
||
| 107 | * @todo I'm leaning towards getting rid of the code that places the cursor |
||
| 108 | * back at the position it was in... I'm not sure it's expected behavior |
||
| 109 | */ |
||
| 110 | 7 | public function __toString() |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Stream factory method. |
||
| 126 | * |
||
| 127 | * Pass in a URI and optionally a mode string, context params, and whether or not you want |
||
| 128 | * lazy-opening, and it will give you back a Stream object. |
||
| 129 | * |
||
| 130 | * @param string $uri The stream URI you want to open |
||
| 131 | * @param string $mode The access mode string |
||
| 132 | * @param null|resource $context Stream resource context options/params |
||
| 133 | * @param boolean $lazy Whether or not you want this stream to lazy-open |
||
| 134 | * @return Stream |
||
| 135 | * @see http://php.net/manual/en/function.fopen.php |
||
| 136 | * @see http://php.net/manual/en/function.stream-context-create.php |
||
| 137 | */ |
||
| 138 | 71 | public static function open($uri, $mode = null, $context = null, $lazy = false) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Close stream resource. |
||
| 148 | * |
||
| 149 | * @return boolean True on success or false on failure |
||
| 150 | */ |
||
| 151 | 79 | public function close() |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Get stream metadata (all or certain value). |
||
| 160 | * |
||
| 161 | * Get either the entire stream metadata array or a single value from it by key. |
||
| 162 | * |
||
| 163 | * @param string $key If set, must be one of ``stream_get_meta_data`` array keys |
||
| 164 | * @return string|array Either a single value or whole array returned by ``stream_get_meta_data`` |
||
| 165 | * @see http://php.net/manual/en/function.stream-get-meta-data.php |
||
| 166 | */ |
||
| 167 | 11 | public function getMetaData($key = null) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Accessor for seekability. |
||
| 182 | * |
||
| 183 | * Returns true if possible to seek to a certain position within this stream |
||
| 184 | * |
||
| 185 | * @return boolean True if stream is seekable |
||
| 186 | */ |
||
| 187 | 11 | public function isSeekable() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Accessor for readability. |
||
| 197 | * |
||
| 198 | * Returns true if possible to read from this stream |
||
| 199 | * |
||
| 200 | * @return boolean True if stream is readable |
||
| 201 | */ |
||
| 202 | 53 | public function isReadable() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Accessor for writability. |
||
| 212 | * |
||
| 213 | * Returns true if possible to write to this stream |
||
| 214 | * |
||
| 215 | * @return boolean True if stream is writable |
||
| 216 | */ |
||
| 217 | 20 | public function isWritable() |
|
| 224 | |||
| 225 | /** |
||
| 226 | * Get the Resource object. |
||
| 227 | * |
||
| 228 | * Returns the internal Resource object used as a drop-in replacement for |
||
| 229 | * PHP's native stream resource variables. |
||
| 230 | * |
||
| 231 | * @return \CSVelte\IO\Resource The Resource object |
||
| 232 | */ |
||
| 233 | 32 | public function getResource() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Accessor for stream URI. |
||
| 240 | * |
||
| 241 | * Returns the stream URI |
||
| 242 | * |
||
| 243 | * @return string The URI for the stream |
||
| 244 | */ |
||
| 245 | 4 | public function getUri() |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Accessor for stream name. |
||
| 254 | * |
||
| 255 | * Alias for ``getUri()`` |
||
| 256 | * |
||
| 257 | * @return string The name for this stream |
||
| 258 | */ |
||
| 259 | 1 | public function getName() |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Separates any underlying resources from the stream. |
||
| 266 | * |
||
| 267 | * After the stream has been detached, the stream is in an unusable state. |
||
| 268 | * |
||
| 269 | * @return Resource|null Underlying PHP stream, if any |
||
| 270 | */ |
||
| 271 | 4 | public function detach() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Get the size of the stream if known. |
||
| 282 | * |
||
| 283 | * @return int|null Returns the size in bytes if known, or null if unknown. |
||
| 284 | */ |
||
| 285 | 4 | public function getSize() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Returns the current position of the file read/write pointer |
||
| 300 | * |
||
| 301 | * @return int Position of the file pointer |
||
| 302 | * @throws \RuntimeException on error. |
||
| 303 | */ |
||
| 304 | 8 | public function tell() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Read $length bytes from stream. |
||
| 311 | * |
||
| 312 | * Reads $length bytes (number of characters) from the stream |
||
| 313 | * |
||
| 314 | * @param int $length Number of bytes to read from stream |
||
| 315 | * @return string|false The data read from stream or false if at end of |
||
| 316 | * file or some other problem. |
||
| 317 | * @throws \CSVelte\Exception\IOException if stream not readable |
||
| 318 | */ |
||
| 319 | 51 | public function read($length) |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Returns the remaining contents in a string. |
||
| 328 | * |
||
| 329 | * Read and return the remaining contents of the stream, beginning from |
||
| 330 | * wherever the stream's internal pointer is when this method is called. If |
||
| 331 | * you want the ENTIRE stream's contents, use __toString() instead. |
||
| 332 | * |
||
| 333 | * @return string The remaining contents of the file, beginning at internal |
||
| 334 | * pointer's current location |
||
| 335 | * @throws \CSVelte\Exception\IOException |
||
| 336 | */ |
||
| 337 | 9 | public function getContents() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Is file pointer at the end of the stream? |
||
| 350 | * |
||
| 351 | * Returns true if internal pointer has reached the end of the stream. |
||
| 352 | * |
||
| 353 | * @return boolean True if end of stream has been reached |
||
| 354 | */ |
||
| 355 | 49 | public function eof() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Rewind pointer to beginning of stream. |
||
| 362 | * |
||
| 363 | * Rewinds the stream, meaning it returns the pointer to the beginning of the |
||
| 364 | * stream as if it had just been initialized. |
||
| 365 | */ |
||
| 366 | 29 | public function rewind() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Write to stream |
||
| 375 | * |
||
| 376 | * Writes a string to the stream (if it is writable) |
||
| 377 | * |
||
| 378 | * @param string $str The data to be written to the stream |
||
| 379 | * @return int The number of bytes written to the stream |
||
| 380 | * @throws \CSVelte\Exception\IOException |
||
| 381 | */ |
||
| 382 | 19 | public function write($str) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Seek to position. |
||
| 390 | * |
||
| 391 | * Seek to a specific position within the stream (if seekable). |
||
| 392 | * |
||
| 393 | * @param int $offset The position to seek to |
||
| 394 | * @param int $whence One of three native php ``SEEK_`` constants |
||
| 395 | * @return boolean True on success false on failure |
||
| 396 | * @throws \CSVelte\Exception\IOException |
||
| 397 | * @see http://php.net/manual/en/function.seek.php |
||
| 398 | */ |
||
| 399 | 11 | public function seek($offset, $whence = SEEK_SET) |
|
| 404 | |||
| 405 | } |
||
| 406 |