File tree Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Expand file tree Collapse file tree 1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change 12
12
13
13
use InvalidArgumentException ;
14
14
use Psr \Http \Message \StreamInterface ;
15
+ use Throwable ;
15
16
use function in_array ;
16
17
use function preg_match ;
17
18
use function str_contains ;
@@ -99,17 +100,27 @@ public static function validateMode(string $mode):string{
99
100
100
101
/**
101
102
* Reads the content from a stream and make sure we rewind
103
+ *
104
+ * Returns the stream content as a string, null if an error occurs, e.g. the StreamInterface throws.
102
105
*/
103
- public static function getContents (StreamInterface $ stream ):string {
106
+ public static function getContents (StreamInterface $ stream ):? string {
104
107
105
108
// rewind before read...
106
109
if ($ stream ->isSeekable ()){
107
110
$ stream ->rewind ();
108
111
}
109
112
110
- $ data = $ stream ->isReadable ()
111
- ? $ stream ->getContents ()
112
- : $ stream ->__toString ();
113
+ try {
114
+ $ data = $ stream ->isReadable ()
115
+ // stream is readable - great!
116
+ ? $ stream ->getContents ()
117
+ // try the __toString() method
118
+ // there's a chance the stream is implemented in such a way (might throw)
119
+ : $ stream ->__toString (); // @codeCoverageIgnore
120
+ }
121
+ catch (Throwable $ e ){
122
+ return null ;
123
+ }
113
124
114
125
// ...and after
115
126
if ($ stream ->isSeekable ()){
You can’t perform that action at this time.
0 commit comments