99 */
1010class ByteFormatter
1111{
12- const DEFAULT_BASE = Base::BINARY ;
12+ private const DEFAULT_BASE = Base::BINARY ;
1313
1414 /** @var int */
1515 private $ base = self ::DEFAULT_BASE ;
@@ -49,19 +49,19 @@ public function __construct(UnitDecorator $unitDecorator = null)
4949 /**
5050 * Formats the specified number of bytes as a human-readable string.
5151 *
52- * @param int $bytes Number of bytes.
52+ * @param int|float $bytes Number of bytes.
5353 * @param int|null $precision Optional. Number of fractional digits.
5454 *
5555 * @return string Formatted bytes.
5656 */
57- public function format ($ bytes , $ precision = null ): string
57+ public function format ($ bytes , int $ precision = null ): string
5858 {
5959 // Use default precision when not specified.
6060 $ precision === null && $ precision = $ this ->getPrecision ();
6161
6262 $ log = log ($ bytes , $ this ->getBase ());
6363 $ exponent = $ this ->hasFixedExponent () ? $ this ->getFixedExponent () : max (0 , $ log |0 );
64- $ value = round (pow ( $ this ->getBase (), $ log - $ exponent ), $ precision );
64+ $ value = round ($ this ->getBase () ** ( $ log - $ exponent ), $ precision );
6565 $ units = $ this ->getUnitDecorator ()->decorate ($ exponent , $ this ->getBase (), $ value );
6666
6767 return trim (sprintf ($ this ->sprintfFormat , $ this ->formatValue ($ value , $ precision ), $ units ));
@@ -75,11 +75,11 @@ public function format($bytes, $precision = null): string
7575 * be completely removed.
7676 *
7777 * @param float $value Number.
78- * @param $precision Number of fractional digits.
78+ * @param int $precision Number of fractional digits.
7979 *
8080 * @return string Formatted number.
8181 */
82- private function formatValue ($ value , $ precision ): string
82+ private function formatValue (float $ value , int $ precision ): string
8383 {
8484 $ formatted = sprintf ("%0. $ {precision}F " , $ value );
8585
@@ -108,7 +108,7 @@ private function formatValue($value, $precision): string
108108 *
109109 * @return string sprintf() format.
110110 */
111- private function convertFormat ($ format ): string
111+ private function convertFormat (string $ format ): string
112112 {
113113 return str_replace (['%v ' , '%u ' ], ['%1$s ' , '%2$s ' ], $ format );
114114 }
@@ -130,9 +130,9 @@ public function getBase(): int
130130 *
131131 * @return $this
132132 */
133- public function setBase ($ base ): self
133+ public function setBase (int $ base ): self
134134 {
135- $ this ->base = $ base| 0 ;
135+ $ this ->base = $ base ;
136136
137137 return $ this ;
138138 }
@@ -155,9 +155,9 @@ public function getFormat(): string
155155 *
156156 * @return $this
157157 */
158- public function setFormat ($ format ): self
158+ public function setFormat (string $ format ): self
159159 {
160- $ this ->sprintfFormat = $ this ->convertFormat ($ this ->format = " $ format" );
160+ $ this ->sprintfFormat = $ this ->convertFormat ($ this ->format = $ format );
161161
162162 return $ this ;
163163 }
@@ -175,13 +175,13 @@ public function getPrecision(): int
175175 /**
176176 * Sets the maximum number of fractional digits.
177177 *
178- * @param $precision
178+ * @param int $precision
179179 *
180180 * @return $this
181181 */
182- public function setPrecision ($ precision ): self
182+ public function setPrecision (int $ precision ): self
183183 {
184- $ this ->precision = $ precision| 0 ;
184+ $ this ->precision = $ precision ;
185185
186186 return $ this ;
187187 }
@@ -238,9 +238,9 @@ public function getFixedExponent(): int
238238 *
239239 * @return $this
240240 */
241- public function setFixedExponent ($ exponent ): self
241+ public function setFixedExponent (int $ exponent ): self
242242 {
243- $ this ->exponent = $ exponent| 0 ;
243+ $ this ->exponent = $ exponent ;
244244
245245 return $ this ;
246246 }
0 commit comments