Skip to content

Commit b99205d

Browse files
committed
Added method types.
1 parent 1235e35 commit b99205d

File tree

5 files changed

+32
-32
lines changed

5 files changed

+32
-32
lines changed

src/Byte/ByteFormatter.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*/
1010
class 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
}

src/Byte/Unit/NameDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class NameDecorator implements UnitDecorator
1313
Base::DECIMAL => ['kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'yotta'],
1414
];
1515

16-
public function decorate($exponent, $base, $value): string
16+
public function decorate(int $exponent, int $base, float $value): string
1717
{
18-
$suffix = $value === 1 ? 'byte' : 'bytes';
18+
$suffix = $value === 1. ? 'byte' : 'bytes';
1919

2020
if (!$exponent) {
2121
return $suffix;

src/Byte/Unit/SymbolDecorator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct($suffix = null)
2222
$this->setSuffix($suffix);
2323
}
2424

25-
public function decorate($exponent, $base, $value): string
25+
public function decorate(int $exponent, int $base, float $value): string
2626
{
2727
if (($suffix = $this->suffix) === null) {
2828
switch ($base) {
@@ -65,7 +65,7 @@ public function getSuffix()
6565
*
6666
* @return $this
6767
*/
68-
public function setSuffix($suffix): self
68+
public function setSuffix(?string $suffix): self
6969
{
7070
$this->suffix = $suffix;
7171

src/Byte/Unit/UnitDecorator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ interface UnitDecorator
1616
*
1717
* @return string Units.
1818
*/
19-
public function decorate($exponent, $base, $value);
19+
public function decorate(int $exponent, int $base, float $value): string;
2020
}

test/Integration/Byte/ByteFormatterTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ public function provideFixedExponents(): array
144144
[6, 0x8000000000, '0.00000048E'],
145145

146146
[1, 0, '0K'],
147-
[1, pow(Base::BINARY, 0), '0.00097656K'],
148-
[1, pow(Base::BINARY, 1), '1K'],
149-
[1, pow(Base::BINARY, 2), '1024K'],
150-
[1, pow(Base::BINARY, 3), '1048576K'],
151-
[1, pow(Base::BINARY, 4), '1073741824K'],
152-
[1, pow(Base::BINARY, 5), '1099511627776K'],
153-
[1, pow(Base::BINARY, 6), '1125899906842624K'],
154-
[1, pow(Base::BINARY, 7), '1152921504606846976K'],
155-
[1, pow(Base::BINARY, 8), '1180591620717411303424K'],
156-
[1, pow(Base::BINARY, 9), '1208925819614629174706176K'],
157-
[1, pow(Base::BINARY, 10), '1237940039285380274899124224K'],
147+
[1, Base::BINARY ** 0, '0.00097656K'],
148+
[1, Base::BINARY ** 1, '1K'],
149+
[1, Base::BINARY ** 2, '1024K'],
150+
[1, Base::BINARY ** 3, '1048576K'],
151+
[1, Base::BINARY ** 4, '1073741824K'],
152+
[1, Base::BINARY ** 5, '1099511627776K'],
153+
[1, Base::BINARY ** 6, '1125899906842624K'],
154+
[1, Base::BINARY ** 7, '1152921504606846976K'],
155+
[1, Base::BINARY ** 8, '1180591620717411303424K'],
156+
[1, Base::BINARY ** 9, '1208925819614629174706176K'],
157+
[1, Base::BINARY ** 10, '1237940039285380274899124224K'],
158158
];
159159
}
160160

0 commit comments

Comments
 (0)