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  | 
            ||
| 12 | class Stream implements StreamInterface  | 
            ||
| 13 | { | 
            ||
| 14 | |||
| 15 | /**  | 
            ||
| 16 | * @var stream body / the resource  | 
            ||
| 17 | */  | 
            ||
| 18 | protected $body;  | 
            ||
| 19 | |||
| 20 | /**  | 
            ||
| 21 | * @var The stream uri, if any  | 
            ||
| 22 | */  | 
            ||
| 23 | protected $uri;  | 
            ||
| 24 | |||
| 25 | /**  | 
            ||
| 26 | * @var Is the stream seekable?  | 
            ||
| 27 | */  | 
            ||
| 28 | protected $seekable;  | 
            ||
| 29 | |||
| 30 | /**  | 
            ||
| 31 | * @var Is the stream readable?  | 
            ||
| 32 | */  | 
            ||
| 33 | protected $readable;  | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * @var Is the stream writable?  | 
            ||
| 37 | */  | 
            ||
| 38 | protected $writable;  | 
            ||
| 39 | |||
| 40 | /**  | 
            ||
| 41 | * @var The stream metadata, if any  | 
            ||
| 42 | */  | 
            ||
| 43 | protected $metaData = [];  | 
            ||
| 44 | |||
| 45 | /**  | 
            ||
| 46 | * @var The overriding options (size, uri, etc.)  | 
            ||
| 47 | */  | 
            ||
| 48 | protected $options = [];  | 
            ||
| 49 | |||
| 50 | /**  | 
            ||
| 51 | * @var Default output format  | 
            ||
| 52 | */  | 
            ||
| 53 | protected $defaultFormat; // 'JSON'  | 
            ||
| 54 | |||
| 55 | /**  | 
            ||
| 56 | * @var The accepted overriding options  | 
            ||
| 57 | */  | 
            ||
| 58 | protected $overridingOptions = [  | 
            ||
| 59 | 'size',  | 
            ||
| 60 | 'uri',  | 
            ||
| 61 | ];  | 
            ||
| 62 | |||
| 63 | /** @var array Hash of readable and writable stream types */  | 
            ||
| 64 | private static $readWriteHash = [  | 
            ||
| 65 | 'read' => [  | 
            ||
| 66 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,  | 
            ||
| 67 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,  | 
            ||
| 68 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,  | 
            ||
| 69 | 'x+t' => true, 'c+t' => true, 'a+' => true  | 
            ||
| 70 | ],  | 
            ||
| 71 | 'write' => [  | 
            ||
| 72 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,  | 
            ||
| 73 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,  | 
            ||
| 74 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,  | 
            ||
| 75 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true  | 
            ||
| 76 | ]  | 
            ||
| 77 | ];  | 
            ||
| 78 | |||
| 79 | /**  | 
            ||
| 80 | * Sets up the resource.  | 
            ||
| 81 | *  | 
            ||
| 82 | * @param resource $stream  | 
            ||
| 83 | * @param array $options  | 
            ||
| 84 | * @return void  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 85 | */  | 
            ||
| 86 | public function __construct($stream, $metaData = [], $options = [])  | 
            ||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * Sets the stream metadata.  | 
            ||
| 100 | *  | 
            ||
| 101 | * @param resource $stream The stream  | 
            ||
| 102 | * @param array $userData The overriding user metadata  | 
            ||
| 103 | * @return void  | 
            ||
| 104 | */  | 
            ||
| 105 | protected function setMetadata($stream, array $userData = [])  | 
            ||
| 110 | |||
| 111 | /**  | 
            ||
| 112 | * Sets the options such as the size, effectively overriding those from the stream.  | 
            ||
| 113 | *  | 
            ||
| 114 | * @param array $options The overriding options  | 
            ||
| 115 | * @return void  | 
            ||
| 116 | */  | 
            ||
| 117 | protected function setOptions(array $options)  | 
            ||
| 125 | |||
| 126 | /**  | 
            ||
| 127 | * Reads all data from the stream into a string, from the beginning to end.  | 
            ||
| 128 | *  | 
            ||
| 129 | * This method MUST attempt to seek to the beginning of the stream before  | 
            ||
| 130 | * reading data and read the stream until the end is reached.  | 
            ||
| 131 | *  | 
            ||
| 132 | * Warning: This could attempt to load a large amount of data into memory.  | 
            ||
| 133 | *  | 
            ||
| 134 | * This method MUST NOT raise an exception in order to conform with PHP's  | 
            ||
| 135 | * string casting operations.  | 
            ||
| 136 | *  | 
            ||
| 137 | * @see http://php.net/manual/en/language.oop5.magic.php#object.tostring  | 
            ||
| 138 | * @return string  | 
            ||
| 139 | */  | 
            ||
| 140 | public function __toString()  | 
            ||
| 156 | |||
| 157 | /**  | 
            ||
| 158 | * Closes the stream and any underlying resources.  | 
            ||
| 159 | *  | 
            ||
| 160 | * @return void  | 
            ||
| 161 | */  | 
            ||
| 162 | public function close()  | 
            ||
| 171 | |||
| 172 | /**  | 
            ||
| 173 | * Separates any underlying resources from the stream.  | 
            ||
| 174 | *  | 
            ||
| 175 | * After the stream has been detached, the stream is in an unusable state.  | 
            ||
| 176 | *  | 
            ||
| 177 | * @return resource|null Underlying PHP stream, if any  | 
            ||
| 178 | */  | 
            ||
| 179 | public function detach()  | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * Get the size of the stream if known.  | 
            ||
| 197 | *  | 
            ||
| 198 | * @return int|null Returns the size in bytes if known, or null if unknown.  | 
            ||
| 199 | */  | 
            ||
| 200 | public function getSize()  | 
            ||
| 217 | |||
| 218 | /**  | 
            ||
| 219 | * Returns the current position of the file read/write pointer  | 
            ||
| 220 | *  | 
            ||
| 221 | * @return int Position of the file pointer  | 
            ||
| 222 | * @throws \RuntimeException on error.  | 
            ||
| 223 | */  | 
            ||
| 224 | public function tell()  | 
            ||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | * Returns true if the stream is at the end of the stream.  | 
            ||
| 235 | *  | 
            ||
| 236 | * @return bool  | 
            ||
| 237 | */  | 
            ||
| 238 | public function eof()  | 
            ||
| 243 | |||
| 244 | /**  | 
            ||
| 245 | * Returns whether or not the stream is seekable.  | 
            ||
| 246 | *  | 
            ||
| 247 | * @return bool  | 
            ||
| 248 | */  | 
            ||
| 249 | public function isSeekable()  | 
            ||
| 253 | |||
| 254 | /**  | 
            ||
| 255 | * Seek to a position in the stream.  | 
            ||
| 256 | *  | 
            ||
| 257 | * @link http://www.php.net/manual/en/function.fseek.php  | 
            ||
| 258 | * @param int $offset Stream offset  | 
            ||
| 259 | * @param int $whence Specifies how the cursor position will be calculated  | 
            ||
| 260 | * based on the seek offset. Valid values are identical to the built-in  | 
            ||
| 261 | * PHP $whence values for `fseek()`. SEEK_SET: Set position equal to  | 
            ||
| 262 | * offset bytes SEEK_CUR: Set position to current location plus offset  | 
            ||
| 263 | * SEEK_END: Set position to end-of-stream plus offset.  | 
            ||
| 264 | * @throws \RuntimeException on failure.  | 
            ||
| 265 | */  | 
            ||
| 266 | public function seek($offset, $whence = SEEK_SET)  | 
            ||
| 276 | |||
| 277 | /**  | 
            ||
| 278 | * Seek to the beginning of the stream.  | 
            ||
| 279 | *  | 
            ||
| 280 | * If the stream is not seekable, this method will raise an exception;  | 
            ||
| 281 | * otherwise, it will perform a seek(0).  | 
            ||
| 282 | *  | 
            ||
| 283 | * @see seek()  | 
            ||
| 284 | * @link http://www.php.net/manual/en/function.fseek.php  | 
            ||
| 285 | * @throws \RuntimeException on failure.  | 
            ||
| 286 | */  | 
            ||
| 287 | public function rewind()  | 
            ||
| 291 | |||
| 292 | /**  | 
            ||
| 293 | * Returns whether or not the stream is writable.  | 
            ||
| 294 | *  | 
            ||
| 295 | * @return bool  | 
            ||
| 296 | */  | 
            ||
| 297 | public function isWritable()  | 
            ||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * Write data to the stream.  | 
            ||
| 304 | *  | 
            ||
| 305 | * @param string $string The string that is to be written.  | 
            ||
| 306 | * @return int Returns the number of bytes written to the stream.  | 
            ||
| 307 | * @throws \RuntimeException on failure.  | 
            ||
| 308 | */  | 
            ||
| 309 | public function write($string)  | 
            ||
| 323 | |||
| 324 | /**  | 
            ||
| 325 | * Returns whether or not the stream is readable.  | 
            ||
| 326 | *  | 
            ||
| 327 | * @return bool  | 
            ||
| 328 | */  | 
            ||
| 329 | public function isReadable()  | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * Read data from the stream.  | 
            ||
| 336 | *  | 
            ||
| 337 | * @param int $length Read up to $length bytes from the object and return  | 
            ||
| 338 | * them. Fewer than $length bytes may be returned if underlying stream  | 
            ||
| 339 | * call returns fewer bytes.  | 
            ||
| 340 | * @return string Returns the data read from the stream, or an empty string  | 
            ||
| 341 | * if no bytes are available.  | 
            ||
| 342 | * @throws \RuntimeException if an error occurs.  | 
            ||
| 343 | */  | 
            ||
| 344 | public function read($length)  | 
            ||
| 352 | |||
| 353 | /**  | 
            ||
| 354 | * Returns the remaining contents in a string  | 
            ||
| 355 | *  | 
            ||
| 356 | * @return string  | 
            ||
| 357 | * @throws \RuntimeException if unable to read or an error occurs while  | 
            ||
| 358 | * reading.  | 
            ||
| 359 | */  | 
            ||
| 360 | public function getContents()  | 
            ||
| 368 | |||
| 369 | /**  | 
            ||
| 370 | * Get stream metadata as an associative array or retrieve a specific key.  | 
            ||
| 371 | *  | 
            ||
| 372 | * The keys returned are identical to the keys returned from PHP's  | 
            ||
| 373 | * stream_get_meta_data() function.  | 
            ||
| 374 | *  | 
            ||
| 375 | * @link http://php.net/manual/en/function.stream-get-meta-data.php  | 
            ||
| 376 | * @param string $key Specific metadata to retrieve.  | 
            ||
| 377 | * @return array|mixed|null Returns an associative array if no key is  | 
            ||
| 378 | * provided. Returns a specific key value if a key is provided and the  | 
            ||
| 379 | * value is found, or null if the key is not found.  | 
            ||
| 380 | */  | 
            ||
| 381 | public function getMetadata($key = null)  | 
            ||
| 395 | |||
| 396 | /**  | 
            ||
| 397 | * Gets the value of body.  | 
            ||
| 398 | *  | 
            ||
| 399 | * @return mixed  | 
            ||
| 400 | */  | 
            ||
| 401 | public function getBody()  | 
            ||
| 409 | |||
| 410 | /**  | 
            ||
| 411 | * Sets the value of body.  | 
            ||
| 412 | *  | 
            ||
| 413 | * @param mixed $body the body  | 
            ||
| 414 | *  | 
            ||
| 415 | * @return self  | 
            ||
| 416 | */  | 
            ||
| 417 | protected function setBody($body)  | 
            ||
| 423 | |||
| 424 | /**  | 
            ||
| 425 | * Is the stream JSONable?  | 
            ||
| 426 | *  | 
            ||
| 427 | * @return boolean  | 
            ||
| 428 | */  | 
            ||
| 429 | protected function isJsonable()  | 
            ||
| 444 | |||
| 445 | protected function getOption($name)  | 
            ||
| 462 | |||
| 463 | protected function setOperations()  | 
            ||
| 469 | |||
| 470 | protected function unsetOperations()  | 
            ||
| 476 | |||
| 477 | /**  | 
            ||
| 478 | * Free resources  | 
            ||
| 479 | *  | 
            ||
| 480 | * @return void  | 
            ||
| 481 | */  | 
            ||
| 482 | public function __destruct()  | 
            ||
| 486 | }  | 
            ||
| 487 | 
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.