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 |
||
| 25 | class Stream implements StreamInterface |
||
| 26 | { |
||
| 27 | /** @var resource */ |
||
| 28 | protected $stream; |
||
| 29 | /** @var array|mixed|null */ |
||
| 30 | protected $seekable; |
||
| 31 | /** @var bool */ |
||
| 32 | protected $readable; |
||
| 33 | /** @var bool */ |
||
| 34 | protected $writable; |
||
| 35 | /** @var int */ |
||
| 36 | protected $size; |
||
| 37 | /** @var array */ |
||
| 38 | protected $meta; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Create new instance of Stream from string, array or callable. |
||
| 42 | * |
||
| 43 | * Stream constructor. |
||
| 44 | * @param $resource array|string|callable |
||
| 45 | */ |
||
| 46 | 21 | public function __construct($resource) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Set stream resource. |
||
| 78 | * |
||
| 79 | * @param $stream resource |
||
| 80 | */ |
||
| 81 | 5 | public function setStream($stream) |
|
| 82 | { |
||
| 83 | 5 | $this->stream = $stream; |
|
| 84 | 5 | } |
|
| 85 | |||
| 86 | /** |
||
| 87 | * Closes the stream when the destructed |
||
| 88 | */ |
||
| 89 | 20 | public function __destruct() |
|
| 93 | |||
| 94 | /** @inheritdoc */ |
||
| 95 | 6 | public function __toString() |
|
| 104 | |||
| 105 | /** @inheritdoc */ |
||
| 106 | 9 | public function getContents() |
|
| 107 | { |
||
| 108 | 9 | if (!is_resource($this->stream)) { |
|
| 109 | 1 | throw new \RuntimeException('Unable to read stream contents'); |
|
| 110 | } |
||
| 111 | |||
| 112 | 8 | $contents = stream_get_contents($this->stream); |
|
| 113 | |||
| 114 | 8 | return $contents; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** @inheritdoc */ |
||
| 118 | 20 | public function close() |
|
| 127 | |||
| 128 | /** @inheritdoc */ |
||
| 129 | 20 | public function detach() |
|
| 130 | { |
||
| 131 | 20 | if (!isset($this->stream)) { |
|
| 132 | 1 | return null; |
|
| 133 | } |
||
| 134 | |||
| 135 | 20 | $result = $this->stream; |
|
| 136 | 20 | unset($this->stream); |
|
| 137 | 20 | $this->readable = $this->writable = $this->seekable = false; |
|
| 138 | |||
| 139 | 20 | return $result; |
|
| 140 | } |
||
| 141 | |||
| 142 | /** @inheritdoc */ |
||
| 143 | 2 | public function getSize() |
|
| 144 | { |
||
| 145 | 2 | if (!isset($this->stream)) { |
|
| 146 | 1 | return null; |
|
| 147 | } |
||
| 148 | |||
| 149 | 1 | if ($this->size !== null) { |
|
| 150 | 1 | return $this->size; |
|
| 151 | } |
||
| 152 | |||
| 153 | 1 | $stats = fstat($this->stream); |
|
| 154 | 1 | if (isset($stats['size'])) { |
|
| 155 | 1 | $this->size = $stats['size']; |
|
| 156 | 1 | return $this->size; |
|
| 157 | } |
||
| 158 | |||
| 159 | 1 | return null; |
|
| 160 | } |
||
| 161 | |||
| 162 | /** @inheritdoc */ |
||
| 163 | 2 | public function isReadable() |
|
| 167 | |||
| 168 | /** @inheritdoc */ |
||
| 169 | 2 | public function isWritable() |
|
| 173 | |||
| 174 | /** @inheritdoc */ |
||
| 175 | 2 | public function isSeekable() |
|
| 179 | |||
| 180 | /** @inheritdoc */ |
||
| 181 | 1 | public function eof() |
|
| 185 | |||
| 186 | /** @inheritdoc */ |
||
| 187 | 2 | public function tell() |
|
| 188 | { |
||
| 189 | 2 | if (!is_resource($this->stream)) { |
|
| 190 | 1 | throw new \RuntimeException('Unable to read stream contents'); |
|
| 191 | } |
||
| 192 | |||
| 193 | 2 | $result = ftell($this->stream); |
|
| 194 | |||
| 195 | 2 | return $result; |
|
| 196 | } |
||
| 197 | |||
| 198 | /** @inheritdoc */ |
||
| 199 | 1 | public function rewind() |
|
| 203 | |||
| 204 | /** @inheritdoc */ |
||
| 205 | 8 | public function seek($offset, $whence = SEEK_SET) |
|
| 206 | { |
||
| 207 | 8 | if (!$this->seekable) { |
|
| 208 | 1 | throw new \RuntimeException('Stream is not seekable'); |
|
| 209 | 7 | } elseif (fseek($this->stream, $offset, $whence) === -1) { |
|
| 210 | 1 | throw new \RuntimeException( |
|
| 211 | 1 | sprintf('Unable to seek to stream position %s with whence %s', $offset, var_export($whence, true)) |
|
| 212 | 1 | ); |
|
| 213 | } |
||
| 214 | 6 | } |
|
| 215 | |||
| 216 | /** @inheritdoc */ |
||
| 217 | 1 | public function read($length) |
|
| 225 | |||
| 226 | /** @inheritdoc */ |
||
| 227 | 2 | public function write($string) |
|
| 238 | |||
| 239 | /** @inheritdoc */ |
||
| 240 | 20 | public function getMetadata($key = null) |
|
| 241 | { |
||
| 242 | 20 | if (!isset($this->stream)) { |
|
| 243 | 1 | return $this->meta = $key ? null : array(); |
|
| 244 | 20 | } elseif (isset($this->meta[$key])) { |
|
| 251 | } |
||
| 252 |