Skip to content

Commit f236fd6

Browse files
authored
feat: narrow types (#108)
1 parent 1b43d2a commit f236fd6

5 files changed

Lines changed: 31 additions & 19 deletions

File tree

src/BigDecimal.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public function multipliedBy(BigNumber|int|string $that): BigDecimal
242242
* Returns the result of the division of this number by the given one, at the given scale.
243243
*
244244
* @param BigNumber|int|string $that The divisor. Must be convertible to a BigDecimal.
245-
* @param int $scale The desired scale. Must be non-negative.
245+
* @param non-negative-int $scale The desired scale. Must be non-negative.
246246
* @param RoundingMode $roundingMode An optional rounding mode, defaults to Unnecessary.
247247
*
248248
* @throws MathException If the divisor is not valid, or is not convertible to a BigDecimal.
@@ -255,7 +255,7 @@ public function multipliedBy(BigNumber|int|string $that): BigDecimal
255255
*/
256256
public function dividedBy(BigNumber|int|string $that, int $scale, RoundingMode $roundingMode = RoundingMode::Unnecessary): BigDecimal
257257
{
258-
if ($scale < 0) {
258+
if ($scale < 0) { // @phpstan-ignore smaller.alwaysFalse
259259
throw InvalidArgumentException::negativeScale();
260260
}
261261

@@ -331,6 +331,8 @@ public function dividedByExact(BigNumber|int|string $that): BigDecimal
331331
*
332332
* The result has a scale of `$this->scale * $exponent`.
333333
*
334+
* @param non-negative-int $exponent
335+
*
334336
* @throws InvalidArgumentException If the exponent is negative.
335337
*
336338
* @pure
@@ -345,7 +347,7 @@ public function power(int $exponent): BigDecimal
345347
return $this;
346348
}
347349

348-
if ($exponent < 0) {
350+
if ($exponent < 0) { // @phpstan-ignore smaller.alwaysFalse
349351
throw InvalidArgumentException::negativeExponent();
350352
}
351353

@@ -470,8 +472,8 @@ public function quotientAndRemainder(BigNumber|int|string $that): array
470472
/**
471473
* Returns the square root of this number, rounded to the given scale according to the given rounding mode.
472474
*
473-
* @param int $scale The target scale. Must be non-negative.
474-
* @param RoundingMode $roundingMode An optional rounding mode, defaults to Unnecessary.
475+
* @param non-negative-int $scale The target scale. Must be non-negative.
476+
* @param RoundingMode $roundingMode An optional rounding mode, defaults to Unnecessary.
475477
*
476478
* @throws InvalidArgumentException If the scale is negative.
477479
* @throws NegativeNumberException If this number is negative.
@@ -482,7 +484,7 @@ public function quotientAndRemainder(BigNumber|int|string $that): array
482484
*/
483485
public function sqrt(int $scale, RoundingMode $roundingMode = RoundingMode::Unnecessary): BigDecimal
484486
{
485-
if ($scale < 0) {
487+
if ($scale < 0) { // @phpstan-ignore smaller.alwaysFalse
486488
throw InvalidArgumentException::negativeScale();
487489
}
488490

@@ -731,7 +733,7 @@ public function toBigRational(): BigRational
731733
#[Override]
732734
public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::Unnecessary): BigDecimal
733735
{
734-
if ($scale < 0) {
736+
if ($scale < 0) { // @phpstan-ignore smaller.alwaysFalse
735737
throw InvalidArgumentException::negativeScale();
736738
}
737739

src/BigInteger.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ protected function __construct(string $value)
7878
*
7979
* For bases greater than 36, and/or custom alphabets, use the fromArbitraryBase() method.
8080
*
81-
* @param string $number The number to convert, in the given base.
82-
* @param int $base The base of the number, between 2 and 36.
81+
* @param string $number The number to convert, in the given base.
82+
* @param int<2, 36> $base The base of the number, between 2 and 36.
8383
*
8484
* @throws NumberFormatException If the number is empty, or contains invalid chars for the given base.
8585
* @throws InvalidArgumentException If the base is out of range.
@@ -88,7 +88,7 @@ protected function __construct(string $value)
8888
*/
8989
public static function fromBase(string $number, int $base): BigInteger
9090
{
91-
if ($base < 2 || $base > 36) {
91+
if ($base < 2 || $base > 36) { // @phpstan-ignore smaller.alwaysFalse, greater.alwaysFalse, booleanOr.alwaysFalse
9292
throw InvalidArgumentException::baseOutOfRange($base);
9393
}
9494

@@ -232,7 +232,7 @@ public static function fromBytes(string $value, bool $signed = true): BigInteger
232232
*
233233
* Using the default random bytes generator, this method is suitable for cryptographic use.
234234
*
235-
* @param int $bitCount The number of bits.
235+
* @param non-negative-int $bitCount The number of bits.
236236
* @param (callable(int): string)|null $randomBytesGenerator A function that accepts a number of bytes, and returns
237237
* a string of random bytes of the given length. Defaults
238238
* to the `random_bytes()` function.
@@ -242,7 +242,7 @@ public static function fromBytes(string $value, bool $signed = true): BigInteger
242242
*/
243243
public static function randomBits(int $bitCount, ?callable $randomBytesGenerator = null): BigInteger
244244
{
245-
if ($bitCount < 0) {
245+
if ($bitCount < 0) { // @phpstan-ignore smaller.alwaysFalse
246246
throw InvalidArgumentException::negativeBitCount();
247247
}
248248

@@ -527,6 +527,8 @@ public function dividedBy(BigNumber|int|string $that, RoundingMode $roundingMode
527527
/**
528528
* Returns this number exponentiated to the given value.
529529
*
530+
* @param non-negative-int $exponent
531+
*
530532
* @throws InvalidArgumentException If the exponent is negative.
531533
*
532534
* @pure
@@ -541,7 +543,7 @@ public function power(int $exponent): BigInteger
541543
return $this;
542544
}
543545

544-
if ($exponent < 0) {
546+
if ($exponent < 0) { // @phpstan-ignore smaller.alwaysFalse
545547
throw InvalidArgumentException::negativeExponent();
546548
}
547549

@@ -1004,6 +1006,8 @@ public function shiftedRight(int $bits): BigInteger
10041006
* For positive BigIntegers, this is equivalent to the number of bits in the ordinary binary representation.
10051007
* Computes (ceil(log2(this < 0 ? -this : this+1))).
10061008
*
1009+
* @return non-negative-int
1010+
*
10071011
* @pure
10081012
*/
10091013
public function getBitLength(): int
@@ -1047,15 +1051,15 @@ public function getLowestSetBit(): int
10471051
*
10481052
* Computes ((this & (1<<bitIndex)) != 0).
10491053
*
1050-
* @param int $bitIndex The bit to test, 0-based.
1054+
* @param non-negative-int $bitIndex The bit to test, 0-based.
10511055
*
10521056
* @throws InvalidArgumentException If the bit to test is negative.
10531057
*
10541058
* @pure
10551059
*/
10561060
public function isBitSet(int $bitIndex): bool
10571061
{
1058-
if ($bitIndex < 0) {
1062+
if ($bitIndex < 0) { // @phpstan-ignore smaller.alwaysFalse
10591063
throw InvalidArgumentException::negativeBitIndex();
10601064
}
10611065

@@ -1147,6 +1151,8 @@ public function toFloat(): float
11471151
*
11481152
* The output will always be lowercase for bases greater than 10.
11491153
*
1154+
* @param int<2, 36> $base
1155+
*
11501156
* @throws InvalidArgumentException If the base is out of range.
11511157
*
11521158
* @pure
@@ -1157,7 +1163,7 @@ public function toBase(int $base): string
11571163
return $this->value;
11581164
}
11591165

1160-
if ($base < 2 || $base > 36) {
1166+
if ($base < 2 || $base > 36) { // @phpstan-ignore smaller.alwaysFalse, greater.alwaysFalse, booleanOr.alwaysFalse
11611167
throw InvalidArgumentException::baseOutOfRange($base);
11621168
}
11631169

src/BigNumber.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,8 @@ abstract public function toBigRational(): BigRational;
425425
/**
426426
* Converts this number to a BigDecimal with the given scale, using rounding if necessary.
427427
*
428-
* @param int $scale The scale of the resulting `BigDecimal`. Must be non-negative.
429-
* @param RoundingMode $roundingMode An optional rounding mode, defaults to Unnecessary.
428+
* @param non-negative-int $scale The scale of the resulting `BigDecimal`. Must be non-negative.
429+
* @param RoundingMode $roundingMode An optional rounding mode, defaults to Unnecessary.
430430
*
431431
* @throws InvalidArgumentException If the scale is negative.
432432
* @throws RoundingNecessaryException If RoundingMode::Unnecessary is used, and this number cannot be converted to

src/BigRational.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public function toBigRational(): BigRational
426426
#[Override]
427427
public function toScale(int $scale, RoundingMode $roundingMode = RoundingMode::Unnecessary): BigDecimal
428428
{
429-
if ($scale < 0) {
429+
if ($scale < 0) { // @phpstan-ignore smaller.alwaysFalse
430430
throw InvalidArgumentException::negativeScale();
431431
}
432432

src/Internal/DecimalHelper.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,17 @@ private function __construct()
3333
*
3434
* @param string $denominator The denominator of the reduced fraction. Must be strictly positive.
3535
*
36+
* @return non-negative-int|null
37+
*
3638
* @pure
3739
*/
3840
public static function computeScaleFromReducedFractionDenominator(string $denominator): ?int
3941
{
4042
$calculator = CalculatorRegistry::get();
4143

4244
$d = rtrim($denominator, '0');
45+
46+
/** @var non-negative-int $scale rtrim can only shorten a string */
4347
$scale = strlen($denominator) - strlen($d);
4448

4549
foreach ([5, 2] as $prime) {

0 commit comments

Comments
 (0)