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  | 
            ||
| 11 | class Stream implements StreamInterface  | 
            ||
| 12 | { | 
            ||
| 13 | private $stream;  | 
            ||
| 14 | private $size;  | 
            ||
| 15 | private $seekable;  | 
            ||
| 16 | private $readable;  | 
            ||
| 17 | private $writable;  | 
            ||
| 18 | private $uri;  | 
            ||
| 19 | private $customMetadata;  | 
            ||
| 20 | |||
| 21 | /** @var array Hash of readable and writable stream types */  | 
            ||
| 22 | private static $readWriteHash = [  | 
            ||
| 23 | 'read' => [  | 
            ||
| 24 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true,  | 
            ||
| 25 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true,  | 
            ||
| 26 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true,  | 
            ||
| 27 | 'x+t' => true, 'c+t' => true, 'a+' => true  | 
            ||
| 28 | ],  | 
            ||
| 29 | 'write' => [  | 
            ||
| 30 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true,  | 
            ||
| 31 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true,  | 
            ||
| 32 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true,  | 
            ||
| 33 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true  | 
            ||
| 34 | ]  | 
            ||
| 35 | ];  | 
            ||
| 36 | |||
| 37 | /**  | 
            ||
| 38 | * This constructor accepts an associative array of options.  | 
            ||
| 39 | *  | 
            ||
| 40 | * - size: (int) If a read stream would otherwise have an indeterminate  | 
            ||
| 41 | * size, but the size is known due to foreknowledge, then you can  | 
            ||
| 42 | * provide that size, in bytes.  | 
            ||
| 43 | * - metadata: (array) Any additional metadata to return when the metadata  | 
            ||
| 44 | * of the stream is accessed.  | 
            ||
| 45 | *  | 
            ||
| 46 | * @param resource $stream Stream resource to wrap.  | 
            ||
| 47 | * @param array $options Associative array of options.  | 
            ||
| 48 | *  | 
            ||
| 49 | * @throws \InvalidArgumentException if the stream is not a stream resource  | 
            ||
| 50 | */  | 
            ||
| 51 | public function __construct($stream, $options = [])  | 
            ||
| 72 | |||
| 73 | public function __get($name)  | 
            ||
| 81 | |||
| 82 | /**  | 
            ||
| 83 | * Closes the stream when the destructed  | 
            ||
| 84 | */  | 
            ||
| 85 | public function __destruct()  | 
            ||
| 89 | |||
| 90 | public function __toString()  | 
            ||
| 99 | |||
| 100 | public function getContents()  | 
            ||
| 110 | |||
| 111 | public function close()  | 
            ||
| 120 | |||
| 121 | public function detach()  | 
            ||
| 134 | |||
| 135 | public function getSize()  | 
            ||
| 158 | |||
| 159 | public function isReadable()  | 
            ||
| 163 | |||
| 164 | public function isWritable()  | 
            ||
| 168 | |||
| 169 | public function isSeekable()  | 
            ||
| 173 | |||
| 174 | public function eof()  | 
            ||
| 178 | |||
| 179 | public function tell()  | 
            ||
| 189 | |||
| 190 | public function rewind()  | 
            ||
| 194 | |||
| 195 | public function seek($offset, $whence = SEEK_SET)  | 
            ||
| 204 | |||
| 205 | public function read($length)  | 
            ||
| 225 | |||
| 226 | public function write($string)  | 
            ||
| 242 | |||
| 243 | public function getMetadata($key = null)  | 
            ||
| 257 | }  | 
            ||
| 258 | 
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: