|
104 | 104 | */ |
105 | 105 | function append($stream, $callback, $read_write = STREAM_FILTER_ALL) |
106 | 106 | { |
107 | | - $ret = @\stream_filter_append($stream, register(), $read_write, $callback); |
| 107 | + $errstr = ''; |
| 108 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 109 | + // Match errstr from PHP's warning message. |
| 110 | + // stream_filter_append() expects parameter 1 to be resource,... |
| 111 | + $errstr = $error; // @codeCoverageIgnore |
| 112 | + }); |
| 113 | + |
| 114 | + try { |
| 115 | + $ret = \stream_filter_append($stream, register(), $read_write, $callback); |
| 116 | + } catch (\TypeError $e) { // @codeCoverageIgnoreStart |
| 117 | + // Throws TypeError on PHP 8.0+ |
| 118 | + \restore_error_handler(); |
| 119 | + throw $e; |
| 120 | + } // @codeCoverageIgnoreEnd |
| 121 | + |
| 122 | + \restore_error_handler(); |
108 | 123 |
|
109 | 124 | // PHP 8 throws above on type errors, older PHP and memory issues can throw here |
110 | 125 | // @codeCoverageIgnoreStart |
111 | 126 | if ($ret === false) { |
112 | | - $error = \error_get_last() + array('message' => ''); |
113 | | - throw new \RuntimeException('Unable to append filter: ' . $error['message']); |
| 127 | + throw new \RuntimeException('Unable to append filter: ' . $errstr); |
114 | 128 | } |
115 | 129 | // @codeCoverageIgnoreEnd |
116 | 130 |
|
@@ -147,13 +161,27 @@ function append($stream, $callback, $read_write = STREAM_FILTER_ALL) |
147 | 161 | */ |
148 | 162 | function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL) |
149 | 163 | { |
150 | | - $ret = @\stream_filter_prepend($stream, register(), $read_write, $callback); |
| 164 | + $errstr = ''; |
| 165 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 166 | + // Match errstr from PHP's warning message. |
| 167 | + // stream_filter_prepend() expects parameter 1 to be resource,... |
| 168 | + $errstr = $error; // @codeCoverageIgnore |
| 169 | + }); |
| 170 | + |
| 171 | + try { |
| 172 | + $ret = \stream_filter_prepend($stream, register(), $read_write, $callback); |
| 173 | + } catch (\TypeError $e) { // @codeCoverageIgnoreStart |
| 174 | + // Throws TypeError on PHP 8.0+ |
| 175 | + \restore_error_handler(); |
| 176 | + throw $e; |
| 177 | + } // @codeCoverageIgnoreEnd |
| 178 | + |
| 179 | + \restore_error_handler(); |
151 | 180 |
|
152 | 181 | // PHP 8 throws above on type errors, older PHP and memory issues can throw here |
153 | 182 | // @codeCoverageIgnoreStart |
154 | 183 | if ($ret === false) { |
155 | | - $error = \error_get_last() + array('message' => ''); |
156 | | - throw new \RuntimeException('Unable to prepend filter: ' . $error['message']); |
| 184 | + throw new \RuntimeException('Unable to prepend filter: ' . $errstr); |
157 | 185 | } |
158 | 186 | // @codeCoverageIgnoreEnd |
159 | 187 |
|
@@ -242,16 +270,25 @@ function prepend($stream, $callback, $read_write = STREAM_FILTER_ALL) |
242 | 270 | function fun($filter, $parameters = null) |
243 | 271 | { |
244 | 272 | $fp = \fopen('php://memory', 'w'); |
| 273 | + |
| 274 | + $errstr = ''; |
| 275 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 276 | + // Match errstr from PHP's warning message. |
| 277 | + // stream_filter_append() expects parameter 1 to be resource,... |
| 278 | + $errstr = $error; |
| 279 | + }); |
| 280 | + |
245 | 281 | if (\func_num_args() === 1) { |
246 | | - $filter = @\stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE); |
| 282 | + $filter = \stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE); |
247 | 283 | } else { |
248 | | - $filter = @\stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE, $parameters); |
| 284 | + $filter = \stream_filter_append($fp, $filter, \STREAM_FILTER_WRITE, $parameters); |
249 | 285 | } |
250 | 286 |
|
| 287 | + \restore_error_handler(); |
| 288 | + |
251 | 289 | if ($filter === false) { |
252 | 290 | \fclose($fp); |
253 | | - $error = \error_get_last() + array('message' => ''); |
254 | | - throw new \RuntimeException('Unable to access built-in filter: ' . $error['message']); |
| 291 | + throw new \RuntimeException('Unable to access built-in filter: ' . $errstr); |
255 | 292 | } |
256 | 293 |
|
257 | 294 | // append filter function which buffers internally |
@@ -301,10 +338,26 @@ function fun($filter, $parameters = null) |
301 | 338 | */ |
302 | 339 | function remove($filter) |
303 | 340 | { |
304 | | - if (@\stream_filter_remove($filter) === false) { |
| 341 | + $errstr = ''; |
| 342 | + \set_error_handler(function ($_, $error) use (&$errstr) { |
| 343 | + // Match errstr from PHP's warning message. |
| 344 | + // stream_filter_remove() expects parameter 1 to be resource,... |
| 345 | + $errstr = $error; |
| 346 | + }); |
| 347 | + |
| 348 | + try { |
| 349 | + $ret = \stream_filter_remove($filter); |
| 350 | + } catch (\TypeError $e) { // @codeCoverageIgnoreStart |
| 351 | + // Throws TypeError on PHP 8.0+ |
| 352 | + \restore_error_handler(); |
| 353 | + throw $e; |
| 354 | + } // @codeCoverageIgnoreEnd |
| 355 | + |
| 356 | + \restore_error_handler(); |
| 357 | + |
| 358 | + if ($ret === false) { |
305 | 359 | // PHP 8 throws above on type errors, older PHP and memory issues can throw here |
306 | | - $error = \error_get_last(); |
307 | | - throw new \RuntimeException('Unable to remove filter: ' . $error['message']); |
| 360 | + throw new \RuntimeException('Unable to remove filter: ' . $errstr); |
308 | 361 | } |
309 | 362 | } |
310 | 363 |
|
|
0 commit comments