11<?php
22
3+ declare (strict_types=1 );
4+
35namespace BitWasp \Buffertools ;
46
57use Mdanter \Ecc \EccFactory ;
@@ -17,20 +19,14 @@ class Buffer implements BufferInterface
1719 */
1820 protected $ buffer ;
1921
20- /**
21- * @var GmpMathInterface
22- */
23- protected $ math ;
24-
2522 /**
2623 * @param string $byteString
2724 * @param null|integer $byteSize
2825 * @param GmpMathInterface $math
2926 * @throws \Exception
3027 */
31- public function __construct ($ byteString = '' , $ byteSize = null , GmpMathInterface $ math = null )
28+ public function __construct (string $ byteString = '' , int $ byteSize = null , GmpMathInterface $ math = null )
3229 {
33- $ this ->math = $ math ?: EccFactory::getAdapter ();
3430 if ($ byteSize !== null ) {
3531 // Check the integer doesn't overflow its supposed size
3632 if (strlen ($ byteString ) > $ byteSize ) {
@@ -64,7 +60,7 @@ public function __debugInfo()
6460 * @return Buffer
6561 * @throws \Exception
6662 */
67- public static function hex ($ hexString = '' , $ byteSize = null , GmpMathInterface $ math = null )
63+ public static function hex (string $ hexString = '' , int $ byteSize = null , GmpMathInterface $ math = null ): Buffer
6864 {
6965 if (strlen ($ hexString ) > 0 && !ctype_xdigit ($ hexString )) {
7066 throw new \InvalidArgumentException ('Buffer::hex: non-hex character passed ' );
@@ -81,7 +77,7 @@ public static function hex($hexString = '', $byteSize = null, GmpMathInterface $
8177 * @param GmpMathInterface|null $math
8278 * @return Buffer
8379 */
84- public static function int ($ integer , $ byteSize = null , GmpMathInterface $ math = null )
80+ public static function int ($ integer , $ byteSize = null , GmpMathInterface $ math = null ): Buffer
8581 {
8682 if ($ integer < 0 ) {
8783 throw new \InvalidArgumentException ('Negative integers not supported by Buffer::int. This could be an application error, or you should be using templates. ' );
@@ -95,10 +91,10 @@ public static function int($integer, $byteSize = null, GmpMathInterface $math =
9591 /**
9692 * @param integer $start
9793 * @param integer|null $end
98- * @return Buffer
94+ * @return BufferInterface
9995 * @throws \Exception
10096 */
101- public function slice ($ start , $ end = null )
97+ public function slice (int $ start , int $ end = null ): BufferInterface
10298 {
10399 if ($ start > $ this ->getSize ()) {
104100 throw new \Exception ('Start exceeds buffer length ' );
@@ -118,15 +114,15 @@ public function slice($start, $end = null)
118114 }
119115
120116 $ length = strlen ($ string );
121- return new self ($ string , $ length, $ this -> math );
117+ return new self ($ string , $ length );
122118 }
123119
124120 /**
125121 * Get the size of the buffer to be returned
126122 *
127123 * @return int
128124 */
129- public function getSize ()
125+ public function getSize (): int
130126 {
131127 return $ this ->size ;
132128 }
@@ -136,15 +132,15 @@ public function getSize()
136132 *
137133 * @return int
138134 */
139- public function getInternalSize ()
135+ public function getInternalSize (): int
140136 {
141137 return strlen ($ this ->buffer );
142138 }
143139
144140 /**
145141 * @return string
146142 */
147- public function getBinary ()
143+ public function getBinary (): string
148144 {
149145 // if a size is specified we'll make sure the value returned is that size
150146 if ($ this ->size !== null ) {
@@ -161,15 +157,15 @@ public function getBinary()
161157 /**
162158 * @return string
163159 */
164- public function getHex ()
160+ public function getHex (): string
165161 {
166162 return unpack ("H* " , $ this ->getBinary ())[1 ];
167163 }
168164
169165 /**
170166 * @return \GMP
171167 */
172- public function getGmp ()
168+ public function getGmp (): \ GMP
173169 {
174170 $ gmp = gmp_init ($ this ->getHex (), 16 );
175171 return $ gmp ;
@@ -178,24 +174,26 @@ public function getGmp()
178174 /**
179175 * @return int|string
180176 */
181- public function getInt ()
177+ public function getInt (): string
182178 {
183179 return gmp_strval ($ this ->getGmp (), 10 );
184180 }
185181
186182 /**
187- * @return BufferInterface
183+ * @return Buffer
188184 */
189- public function flip ()
185+ public function flip (): Buffer
190186 {
191- return Buffertools::flipBytes ($ this );
187+ /** @var Buffer $buffer */
188+ $ buffer = Buffertools::flipBytes ($ this );
189+ return $ buffer ;
192190 }
193191
194192 /**
195193 * @param BufferInterface $other
196194 * @return bool
197195 */
198- public function equals (BufferInterface $ other )
196+ public function equals (BufferInterface $ other ): bool
199197 {
200198 return ($ other ->getSize () === $ this ->getSize ()
201199 && $ other ->getBinary () === $ this ->getBinary ());
0 commit comments