44 *
55 * @license http://opensource.org/licenses/bsd-license.php BSD
66 */
7+
78namespace ZBateson \StreamDecorators ;
89
9- use Psr \Http \Message \StreamInterface ;
10- use GuzzleHttp \Psr7 \StreamDecoratorTrait ;
1110use GuzzleHttp \Psr7 \BufferStream ;
11+ use GuzzleHttp \Psr7 \StreamDecoratorTrait ;
12+ use Psr \Http \Message \StreamInterface ;
1213use RuntimeException ;
1314
1415/**
@@ -64,9 +65,6 @@ class Base64Stream implements StreamInterface
6465 */
6566 private $ stream ;
6667
67- /**
68- * @param StreamInterface $stream
69- */
7068 public function __construct (StreamInterface $ stream )
7169 {
7270 $ this ->stream = $ stream ;
@@ -75,10 +73,8 @@ public function __construct(StreamInterface $stream)
7573
7674 /**
7775 * Returns the current position of the file read/write pointer
78- *
79- * @return int
8076 */
81- public function tell ()
77+ public function tell () : int
8278 {
8379 return $ this ->position ;
8480 }
@@ -88,7 +84,7 @@ public function tell()
8884 *
8985 * @return null
9086 */
91- public function getSize ()
87+ public function getSize () : ? int
9288 {
9389 return null ;
9490 }
@@ -109,20 +105,16 @@ public function seek($offset, $whence = SEEK_SET)
109105
110106 /**
111107 * Overridden to return false
112- *
113- * @return boolean
114108 */
115- public function isSeekable ()
109+ public function isSeekable () : bool
116110 {
117111 return false ;
118112 }
119113
120114 /**
121115 * Returns true if the end of stream has been reached.
122- *
123- * @return boolean
124116 */
125- public function eof ()
117+ public function eof () : bool
126118 {
127119 return ($ this ->buffer ->eof () && $ this ->stream ->eof ());
128120 }
@@ -134,18 +126,16 @@ public function eof()
134126 * Note that it's expected the underlying stream will only contain valid
135127 * base64 characters (normally the stream should be wrapped in a
136128 * PregReplaceFilterStream to filter out non-base64 characters).
137- *
138- * @param int $length
139129 */
140- private function fillBuffer ($ length )
130+ private function fillBuffer (int $ length ) : void
141131 {
142132 $ fill = 8192 ;
143133 while ($ this ->buffer ->getSize () < $ length ) {
144134 $ read = $ this ->stream ->read ($ fill );
145- if ($ read === false || $ read === '' ) {
135+ if ($ read === '' ) {
146136 break ;
147137 }
148- $ this ->buffer ->write (base64_decode ($ read ));
138+ $ this ->buffer ->write (\ base64_decode ($ read ));
149139 }
150140 }
151141
@@ -166,7 +156,7 @@ public function read($length)
166156 }
167157 $ this ->fillBuffer ($ length );
168158 $ ret = $ this ->buffer ->read ($ length );
169- $ this ->position += strlen ($ ret );
159+ $ this ->position += \ strlen ($ ret );
170160 return $ ret ;
171161 }
172162
@@ -184,52 +174,50 @@ public function read($length)
184174 * @param string $string
185175 * @return int the number of bytes written
186176 */
187- public function write ($ string )
177+ public function write ($ string ) : int
188178 {
189179 $ bytes = $ this ->remainder . $ string ;
190- $ len = strlen ($ bytes );
180+ $ len = \ strlen ($ bytes );
191181 if (($ len % 3 ) !== 0 ) {
192- $ this ->remainder = substr ($ bytes , -($ len % 3 ));
193- $ bytes = substr ($ bytes , 0 , $ len - ($ len % 3 ));
182+ $ this ->remainder = \ substr ($ bytes , -($ len % 3 ));
183+ $ bytes = \ substr ($ bytes , 0 , $ len - ($ len % 3 ));
194184 } else {
195185 $ this ->remainder = '' ;
196186 }
197- $ this ->stream ->write (base64_encode ($ bytes ));
198- $ written = strlen ($ string );
187+ $ this ->stream ->write (\ base64_encode ($ bytes ));
188+ $ written = \ strlen ($ string );
199189 $ this ->position += $ len ;
200190 return $ written ;
201191 }
202192
203193 /**
204194 * Writes out any remaining bytes at the end of the stream and closes.
205195 */
206- private function beforeClose ()
196+ private function beforeClose () : void
207197 {
208198 if ($ this ->isWritable () && $ this ->remainder !== '' ) {
209- $ this ->stream ->write (base64_encode ($ this ->remainder ));
199+ $ this ->stream ->write (\ base64_encode ($ this ->remainder ));
210200 $ this ->remainder = '' ;
211201 }
212202 }
213203
214204 /**
215- * Closes the underlying stream after writing out any remaining bytes
216- * needing to be encoded.
217- * @return void
205+ * @inheritDoc
218206 */
219- public function close ()
207+ public function close () : void
220208 {
221209 $ this ->beforeClose ();
222210 $ this ->stream ->close ();
223211 }
224212
225213 /**
226- * Detaches the underlying stream after writing out any remaining bytes
227- * needing to be encoded.
228- * @return resource|null Underlying PHP stream, if any
214+ * @inheritDoc
229215 */
230216 public function detach ()
231217 {
232218 $ this ->beforeClose ();
233219 $ this ->stream ->detach ();
220+
221+ return null ;
234222 }
235223}
0 commit comments