@@ -41,19 +41,45 @@ final class Decompressor extends TransformStream
4141 */
4242 public function __construct ($ encoding )
4343 {
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+ // Throws ValueError on PHP 8.0+
55+ restore_error_handler ();
56+ throw $ e ;
57+ } // @codeCoverageIgnoreEnd
58+
59+ restore_error_handler ();
60+
4561 if ($ context === false ) {
46- throw new \InvalidArgumentException ('Unable to initialize decompressor ' . strstr ( error_get_last ()[ ' message ' ], ' : ' ));
62+ throw new \InvalidArgumentException ('Unable to initialize decompressor ' . $ errstr ); // @codeCoverageIgnore
4763 }
4864
4965 $ this ->context = $ context ;
5066 }
5167
5268 protected function transformData ($ chunk )
5369 {
54- $ ret = @inflate_add ($ this ->context , $ chunk );
70+ $ errstr = '' ;
71+ set_error_handler (function ($ _ , $ error ) use (&$ errstr ) {
72+ // Match errstr from PHP's warning message.
73+ // inflate_add(): data error
74+ $ errstr = strstr ($ error , ': ' );
75+ });
76+
77+ $ ret = inflate_add ($ this ->context , $ chunk );
78+
79+ restore_error_handler ();
80+
5581 if ($ ret === false ) {
56- throw new \RuntimeException ('Unable to decompress ' . strstr ( error_get_last ()[ ' message ' ], ' : ' ) );
82+ throw new \RuntimeException ('Unable to decompress ' . $ errstr );
5783 }
5884
5985 if ($ ret !== '' ) {
@@ -63,11 +89,20 @@ protected function transformData($chunk)
6389
6490 protected function transformEnd ($ chunk )
6591 {
66- $ ret = @inflate_add ($ this ->context , $ chunk , ZLIB_FINISH );
92+ $ errstr = '' ;
93+ set_error_handler (function ($ _ , $ error ) use (&$ errstr ) {
94+ // Match errstr from PHP's warning message.
95+ // inflate_add(): data error
96+ $ errstr = strstr ($ error , ': ' );
97+ });
98+
99+ $ ret = inflate_add ($ this ->context , $ chunk , ZLIB_FINISH );
67100 $ this ->context = null ;
68101
102+ restore_error_handler ();
103+
69104 if ($ ret === false ) {
70- throw new \RuntimeException ('Unable to decompress ' . strstr ( error_get_last ()[ ' message ' ], ' : ' ) );
105+ throw new \RuntimeException ('Unable to decompress ' . $ errstr );
71106 }
72107
73108 if ($ ret !== '' ) {
0 commit comments