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 |
||
| 13 | final class Stream implements StreamInterface |
||
| 14 | { |
||
| 15 | /** @var resource A resource reference */ |
||
| 16 | private $stream; |
||
| 17 | |||
| 18 | /** @var bool */ |
||
| 19 | private $seekable; |
||
| 20 | |||
| 21 | /** @var bool */ |
||
| 22 | private $readable; |
||
| 23 | |||
| 24 | /** @var bool */ |
||
| 25 | private $writable; |
||
| 26 | |||
| 27 | /** @var array|mixed|null|void */ |
||
| 28 | private $uri; |
||
| 29 | |||
| 30 | /** @var int */ |
||
| 31 | private $size; |
||
| 32 | |||
| 33 | /** @var array Hash of readable and writable stream types */ |
||
| 34 | private static $readWriteHash = [ |
||
| 35 | 'read' => [ |
||
| 36 | 'r' => true, 'w+' => true, 'r+' => true, 'x+' => true, 'c+' => true, |
||
| 37 | 'rb' => true, 'w+b' => true, 'r+b' => true, 'x+b' => true, |
||
| 38 | 'c+b' => true, 'rt' => true, 'w+t' => true, 'r+t' => true, |
||
| 39 | 'x+t' => true, 'c+t' => true, 'a+' => true, |
||
| 40 | ], |
||
| 41 | 'write' => [ |
||
| 42 | 'w' => true, 'w+' => true, 'rw' => true, 'r+' => true, 'x+' => true, |
||
| 43 | 'c+' => true, 'wb' => true, 'w+b' => true, 'r+b' => true, |
||
| 44 | 'x+b' => true, 'c+b' => true, 'w+t' => true, 'r+t' => true, |
||
| 45 | 'x+t' => true, 'c+t' => true, 'a' => true, 'a+' => true, |
||
| 46 | ], |
||
| 47 | ]; |
||
| 48 | |||
| 49 | 43 | private function __construct() |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Creates a new PSR-7 stream. |
||
| 55 | * |
||
| 56 | * @param string|resource|StreamInterface $body |
||
| 57 | * |
||
| 58 | * @return StreamInterface |
||
| 59 | * |
||
| 60 | * @throws \InvalidArgumentException |
||
| 61 | */ |
||
| 62 | 43 | public static function create($body = ''): StreamInterface |
|
| 63 | { |
||
| 64 | 43 | if ($body instanceof StreamInterface) { |
|
| 65 | return $body; |
||
| 66 | } |
||
| 67 | |||
| 68 | 43 | if (is_string($body)) { |
|
| 69 | 15 | $resource = fopen('php://temp', 'rw+'); |
|
| 70 | 15 | fwrite($resource, $body); |
|
| 71 | 15 | $body = $resource; |
|
| 72 | } |
||
| 73 | |||
| 74 | 43 | if ('resource' === gettype($body)) { |
|
| 75 | 43 | $obj = new self(); |
|
| 76 | 43 | $obj->stream = $body; |
|
| 77 | 43 | $meta = stream_get_meta_data($obj->stream); |
|
| 78 | 43 | $obj->seekable = $meta['seekable']; |
|
| 79 | 43 | $obj->readable = isset(self::$readWriteHash['read'][$meta['mode']]); |
|
| 80 | 43 | $obj->writable = isset(self::$readWriteHash['write'][$meta['mode']]); |
|
| 81 | 43 | $obj->uri = $obj->getMetadata('uri'); |
|
| 82 | |||
| 83 | 43 | return $obj; |
|
| 84 | } |
||
| 85 | |||
| 86 | throw new \InvalidArgumentException('First argument to Stream::create() must be a string, resource or StreamInterface.'); |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Closes the stream when the destructed. |
||
| 91 | */ |
||
| 92 | 43 | public function __destruct() |
|
| 96 | |||
| 97 | 9 | public function __toString(): string |
|
| 109 | |||
| 110 | 43 | public function close(): void |
|
| 119 | |||
| 120 | 43 | public function detach() |
|
| 133 | |||
| 134 | 18 | public function getSize(): ?int |
|
| 158 | |||
| 159 | 3 | public function tell(): int |
|
| 167 | |||
| 168 | 8 | public function eof(): bool |
|
| 172 | |||
| 173 | 16 | public function isSeekable(): bool |
|
| 177 | |||
| 178 | 22 | public function seek($offset, $whence = SEEK_SET): void |
|
| 186 | |||
| 187 | 8 | public function rewind(): void |
|
| 191 | |||
| 192 | 4 | public function isWritable(): bool |
|
| 196 | |||
| 197 | 8 | public function write($string): int |
|
| 212 | |||
| 213 | 4 | public function isReadable(): bool |
|
| 217 | |||
| 218 | 8 | public function read($length): string |
|
| 226 | |||
| 227 | 11 | public function getContents(): string |
|
| 239 | |||
| 240 | 43 | public function getMetadata($key = null) |
|
| 252 | } |
||
| 253 |