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, 'rb+' => 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, 'rb+' => 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 | /** |
||
| 74 | * Closes the stream when the destructed |
||
| 75 | */ |
||
| 76 | public function __destruct() |
||
| 80 | |||
| 81 | public function __toString() |
||
| 90 | |||
| 91 | public function getContents() |
||
| 105 | |||
| 106 | public function close() |
||
| 115 | |||
| 116 | public function detach() |
||
| 129 | |||
| 130 | public function getSize() |
||
| 153 | |||
| 154 | public function isReadable() |
||
| 158 | |||
| 159 | public function isWritable() |
||
| 163 | |||
| 164 | public function isSeekable() |
||
| 168 | |||
| 169 | public function eof() |
||
| 177 | |||
| 178 | public function tell() |
||
| 192 | |||
| 193 | public function rewind() |
||
| 197 | |||
| 198 | public function seek($offset, $whence = SEEK_SET) |
||
| 211 | |||
| 212 | public function read($length) |
||
| 235 | |||
| 236 | public function write($string) |
||
| 255 | |||
| 256 | public function getMetadata($key = null) |
||
| 270 | } |
||
| 271 |
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: