@@ -41,19 +41,46 @@ final class Decompressor extends TransformStream
41
41
*/
42
42
public function __construct ($ encoding )
43
43
{
44
- $ context = @inflate_init ($ encoding );
44
+ $ errstr = '' ;
45
+ set_error_handler (function ($ _ , $ error ) use (&$ errstr ) {
46
+ // Match errstr from PHP's warning message.
47
+ // inflate_init(): encoding mode must be ZLIB_ENCODING_RAW, ZLIB_ENCODING_GZIP or ZLIB_ENCODING_DEFLATE
48
+ $ errstr = strstr ($ error , ': ' ); // @codeCoverageIgnore
49
+ });
50
+
51
+ try {
52
+ $ context = inflate_init ($ encoding );
53
+ } catch (\ValueError $ e ) { // @codeCoverageIgnoreStart
54
+ // Only works with PHP >= 8.0
55
+ restore_error_handler ();
56
+ throw $ e ;
57
+ } // @codeCoverageIgnoreEnd
58
+
59
+ restore_error_handler ();
60
+
45
61
if ($ context === false ) {
46
- throw new \InvalidArgumentException ('Unable to initialize decompressor ' . strstr (error_get_last ()['message ' ], ': ' ));
62
+ // Only works with PHP < 8.0
63
+ throw new \InvalidArgumentException ('Unable to initialize decompressor ' . $ errstr ); // @codeCoverageIgnore
47
64
}
48
65
49
66
$ this ->context = $ context ;
50
67
}
51
68
52
69
protected function transformData ($ chunk )
53
70
{
54
- $ ret = @inflate_add ($ this ->context , $ chunk );
71
+ $ errstr = '' ;
72
+ set_error_handler (function ($ _ , $ error ) use (&$ errstr ) {
73
+ // Match errstr from PHP's warning message.
74
+ // inflate_add(): data error
75
+ $ errstr = strstr ($ error , ': ' );
76
+ });
77
+
78
+ $ ret = inflate_add ($ this ->context , $ chunk );
79
+
80
+ restore_error_handler ();
81
+
55
82
if ($ ret === false ) {
56
- throw new \RuntimeException ('Unable to decompress ' . strstr ( error_get_last ()[ ' message ' ], ' : ' ) );
83
+ throw new \RuntimeException ('Unable to decompress ' . $ errstr );
57
84
}
58
85
59
86
if ($ ret !== '' ) {
@@ -63,11 +90,20 @@ protected function transformData($chunk)
63
90
64
91
protected function transformEnd ($ chunk )
65
92
{
66
- $ ret = @inflate_add ($ this ->context , $ chunk , ZLIB_FINISH );
93
+ $ errstr = '' ;
94
+ set_error_handler (function ($ _ , $ error ) use (&$ errstr ) {
95
+ // Match errstr from PHP's warning message.
96
+ // inflate_add(): data error
97
+ $ errstr = strstr ($ error , ': ' );
98
+ });
99
+
100
+ $ ret = inflate_add ($ this ->context , $ chunk , ZLIB_FINISH );
67
101
$ this ->context = null ;
68
102
103
+ restore_error_handler ();
104
+
69
105
if ($ ret === false ) {
70
- throw new \RuntimeException ('Unable to decompress ' . strstr ( error_get_last ()[ ' message ' ], ' : ' ) );
106
+ throw new \RuntimeException ('Unable to decompress ' . $ errstr );
71
107
}
72
108
73
109
if ($ ret !== '' ) {
0 commit comments