@@ -185,16 +185,20 @@ public function getSize(): ?int
185
185
186
186
public function tell (): int
187
187
{
188
- if (false === $ result = \ftell ($ this ->stream )) {
189
- throw new \RuntimeException ('Unable to determine stream position ' );
188
+ if (!isset ($ this ->stream )) {
189
+ throw new \RuntimeException ('Stream is detached ' );
190
+ }
191
+
192
+ if (false === $ result = @\ftell ($ this ->stream )) {
193
+ throw new \RuntimeException ('Unable to determine stream position: ' . (\error_get_last ()['message ' ] ?? '' ));
190
194
}
191
195
192
196
return $ result ;
193
197
}
194
198
195
199
public function eof (): bool
196
200
{
197
- return !$ this ->stream || \feof ($ this ->stream );
201
+ return !isset ( $ this ->stream ) || \feof ($ this ->stream );
198
202
}
199
203
200
204
public function isSeekable (): bool
@@ -204,6 +208,10 @@ public function isSeekable(): bool
204
208
205
209
public function seek ($ offset , $ whence = \SEEK_SET ): void
206
210
{
211
+ if (!isset ($ this ->stream )) {
212
+ throw new \RuntimeException ('Stream is detached ' );
213
+ }
214
+
207
215
if (!$ this ->seekable ) {
208
216
throw new \RuntimeException ('Stream is not seekable ' );
209
217
}
@@ -225,15 +233,19 @@ public function isWritable(): bool
225
233
226
234
public function write ($ string ): int
227
235
{
236
+ if (!isset ($ this ->stream )) {
237
+ throw new \RuntimeException ('Stream is detached ' );
238
+ }
239
+
228
240
if (!$ this ->writable ) {
229
241
throw new \RuntimeException ('Cannot write to a non-writable stream ' );
230
242
}
231
243
232
244
// We can't know the size after writing anything
233
245
$ this ->size = null ;
234
246
235
- if (false === $ result = \fwrite ($ this ->stream , $ string )) {
236
- throw new \RuntimeException ('Unable to write to stream ' );
247
+ if (false === $ result = @ \fwrite ($ this ->stream , $ string )) {
248
+ throw new \RuntimeException ('Unable to write to stream: ' . ( \error_get_last ()[ ' message ' ] ?? '' ) );
237
249
}
238
250
239
251
return $ result ;
@@ -246,12 +258,16 @@ public function isReadable(): bool
246
258
247
259
public function read ($ length ): string
248
260
{
261
+ if (!isset ($ this ->stream )) {
262
+ throw new \RuntimeException ('Stream is detached ' );
263
+ }
264
+
249
265
if (!$ this ->readable ) {
250
266
throw new \RuntimeException ('Cannot read from non-readable stream ' );
251
267
}
252
268
253
- if (false === $ result = \fread ($ this ->stream , $ length )) {
254
- throw new \RuntimeException ('Unable to read from stream ' );
269
+ if (false === $ result = @ \fread ($ this ->stream , $ length )) {
270
+ throw new \RuntimeException ('Unable to read from stream: ' . ( \error_get_last ()[ ' message ' ] ?? '' ) );
255
271
}
256
272
257
273
return $ result ;
@@ -260,11 +276,11 @@ public function read($length): string
260
276
public function getContents (): string
261
277
{
262
278
if (!isset ($ this ->stream )) {
263
- throw new \RuntimeException ('Unable to read stream contents ' );
279
+ throw new \RuntimeException ('Stream is detached ' );
264
280
}
265
281
266
- if (false === $ contents = \stream_get_contents ($ this ->stream )) {
267
- throw new \RuntimeException ('Unable to read stream contents ' );
282
+ if (false === $ contents = @ \stream_get_contents ($ this ->stream )) {
283
+ throw new \RuntimeException ('Unable to read stream contents: ' . ( \error_get_last ()[ ' message ' ] ?? '' ) );
268
284
}
269
285
270
286
return $ contents ;
0 commit comments