Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion beberlei/assert/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
},
"require": {
"php": "^7.0 || ^8.0",
"ext-intl": "*",
"ext-simplexml": "*",
"ext-mbstring": "*",
"ext-ctype": "*",
Expand Down Expand Up @@ -58,5 +57,8 @@
"assert:cs-fix": "php-cs-fixer fix . -vvv || true",
"assert:sa-code": "vendor/bin/phpstan analyse --configuration=phpstan-code.neon --no-progress --ansi -l 7 bin lib",
"assert:sa-tests": "vendor/bin/phpstan analyse --configuration=phpstan-tests.neon --no-progress --ansi -l 7 tests"
},
"suggest": {
"ext-intl": "Needed to allow Assertion::count(), Assertion::isCountable(), Assertion::minCount(), and Assertion::maxCount() to operate on ResourceBundles"
}
}
198 changes: 98 additions & 100 deletions beberlei/assert/lib/Assert/Assertion.php

Large diffs are not rendered by default.

200 changes: 105 additions & 95 deletions beberlei/assert/lib/Assert/LazyAssertion.php

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion brick/math/SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

## Supported Versions

Only the latest release stream is supported.
Only the last two release streams are supported.

| Version | Supported |
| ------- | ------------------ |
| 0.9.x | :white_check_mark: |
| 0.8.x | :white_check_mark: |
| < 0.8 | :x: |

Expand Down
6 changes: 3 additions & 3 deletions brick/math/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
],
"license": "MIT",
"require": {
"php": "^7.1|^8.0",
"php": "^7.1 || ^8.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^7.5.15|^8.5",
"phpunit/phpunit": "^7.5.15 || ^8.5 || ^9.0",
"php-coveralls/php-coveralls": "^2.2",
"vimeo/psalm": "^3.5"
"vimeo/psalm": "4.3.2"
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 14 additions & 8 deletions brick/math/src/BigDecimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ public static function ofUnscaledValue($value, int $scale = 0) : BigDecimal
*/
public static function zero() : BigDecimal
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigDecimal|null $zero
*/
static $zero;

if ($zero === null) {
Expand All @@ -115,7 +118,10 @@ public static function zero() : BigDecimal
*/
public static function one() : BigDecimal
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigDecimal|null $one
*/
static $one;

if ($one === null) {
Expand All @@ -134,7 +140,10 @@ public static function one() : BigDecimal
*/
public static function ten() : BigDecimal
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigDecimal|null $ten
*/
static $ten;

if ($ten === null) {
Expand Down Expand Up @@ -677,11 +686,7 @@ public function hasNonZeroFractionalPart() : bool
*/
public function toBigInteger() : BigInteger
{
if ($this->scale === 0) {
$zeroScaleDecimal = $this;
} else {
$zeroScaleDecimal = $this->dividedBy(1, 0);
}
$zeroScaleDecimal = $this->scale === 0 ? $this : $this->dividedBy(1, 0);

return BigInteger::create($zeroScaleDecimal->value);
}
Expand Down Expand Up @@ -763,6 +768,7 @@ public function serialize() : string
* This method is only here to implement interface Serializable and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param string $value
*
Expand Down
25 changes: 21 additions & 4 deletions brick/math/src/BigInteger.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ public static function fromBytes(string $value, bool $signed = true) : BigIntege
*
* Using the default random bytes generator, this method is suitable for cryptographic use.
*
* @psalm-param callable(int): string $randomBytesGenerator
*
* @param int $numBits The number of bits.
* @param callable|null $randomBytesGenerator A function that accepts a number of bytes as an integer, and returns a
* string of random bytes of the given length. Defaults to the
Expand Down Expand Up @@ -256,6 +258,8 @@ public static function randomBits(int $numBits, ?callable $randomBytesGenerator
*
* Using the default random bytes generator, this method is suitable for cryptographic use.
*
* @psalm-param (callable(int): string)|null $randomBytesGenerator
*
* @param BigNumber|int|float|string $min The lower bound. Must be convertible to a BigInteger.
* @param BigNumber|int|float|string $max The upper bound. Must be convertible to a BigInteger.
* @param callable|null $randomBytesGenerator A function that accepts a number of bytes as an integer,
Expand Down Expand Up @@ -300,7 +304,10 @@ public static function randomRange($min, $max, ?callable $randomBytesGenerator =
*/
public static function zero() : BigInteger
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigInteger|null $zero
*/
static $zero;

if ($zero === null) {
Expand All @@ -319,7 +326,10 @@ public static function zero() : BigInteger
*/
public static function one() : BigInteger
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigInteger|null $one
*/
static $one;

if ($one === null) {
Expand All @@ -338,7 +348,10 @@ public static function one() : BigInteger
*/
public static function ten() : BigInteger
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigInteger|null $ten
*/
static $ten;

if ($ten === null) {
Expand Down Expand Up @@ -1070,7 +1083,10 @@ public function toBytes(bool $signed = true) : string

if ($signed) {
if ($this->isNegative()) {
$hex = \bin2hex(~\hex2bin($hex));
$bin = \hex2bin($hex);
assert($bin !== false);

$hex = \bin2hex(~$bin);
$hex = self::fromBase($hex, 16)->plus(1)->toBase(16);

$hexLength = \strlen($hex);
Expand Down Expand Up @@ -1116,6 +1132,7 @@ public function serialize() : string
* This method is only here to implement interface Serializable and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param string $value
*
Expand Down
42 changes: 24 additions & 18 deletions brick/math/src/BigNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,10 @@ public static function of($value) : BigNumber
return new BigInteger((string) $value);
}

if (\is_float($value)) {
$value = self::floatToString($value);
} else {
$value = (string) $value;
}
/** @psalm-suppress RedundantCastGivenDocblockType We cannot trust the untyped $value here! */
$value = \is_float($value) ? self::floatToString($value) : (string) $value;

$throw = function() use ($value) : void {
$throw = static function() use ($value) : void {
throw new NumberFormatException(\sprintf(
'The given value "%s" does not represent a valid number.',
$value
Expand All @@ -84,7 +81,7 @@ public static function of($value) : BigNumber
$throw();
}

$getMatch = function(string $value) use ($matches) : ?string {
$getMatch = static function(string $value) use ($matches) : ?string {
return isset($matches[$value]) && $matches[$value] !== '' ? $matches[$value] : null;
};

Expand All @@ -93,7 +90,13 @@ public static function of($value) : BigNumber
$denominator = $getMatch('denominator');

if ($numerator !== null) {
$numerator = self::cleanUp($sign . $numerator);
assert($denominator !== null);

if ($sign !== null) {
$numerator = $sign . $numerator;
}

$numerator = self::cleanUp($numerator);
$denominator = self::cleanUp($denominator);

if ($denominator === '0') {
Expand Down Expand Up @@ -121,14 +124,14 @@ public static function of($value) : BigNumber
}

if ($point !== null || $exponent !== null) {
$fractional = $fractional ?? '';
$exponent = $exponent !== null ? (int) $exponent : 0;
$fractional = ($fractional ?? '');
$exponent = ($exponent !== null) ? (int) $exponent : 0;

if ($exponent === PHP_INT_MIN || $exponent === PHP_INT_MAX) {
throw new NumberFormatException('Exponent too large.');
}

$unscaledValue = self::cleanUp($sign . $integral . $fractional);
$unscaledValue = self::cleanUp(($sign ?? ''). $integral . $fractional);

$scale = \strlen($fractional) - $exponent;

Expand All @@ -142,7 +145,7 @@ public static function of($value) : BigNumber
return new BigDecimal($unscaledValue, $scale);
}

$integral = self::cleanUp($sign . $integral);
$integral = self::cleanUp(($sign ?? '') . $integral);

return new BigInteger($integral);
}
Expand Down Expand Up @@ -181,10 +184,11 @@ private static function floatToString(float $float) : string
* @return static
*
* @psalm-pure
* @psalm-suppress TooManyArguments
* @psalm-suppress UnsafeInstantiation
*/
protected static function create(... $args) : BigNumber
{
/** @psalm-suppress TooManyArguments */
return new static(... $args);
}

Expand All @@ -199,6 +203,8 @@ protected static function create(... $args) : BigNumber
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
* @psalm-pure
*/
public static function min(...$values) : BigNumber
Expand Down Expand Up @@ -231,6 +237,8 @@ public static function min(...$values) : BigNumber
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
* @psalm-pure
*/
public static function max(...$values) : BigNumber
Expand Down Expand Up @@ -263,6 +271,8 @@ public static function max(...$values) : BigNumber
* @throws \InvalidArgumentException If no values are given.
* @throws MathException If an argument is not valid.
*
* @psalm-suppress LessSpecificReturnStatement
* @psalm-suppress MoreSpecificReturnType
* @psalm-pure
*/
public static function sum(...$values) : BigNumber
Expand All @@ -273,11 +283,7 @@ public static function sum(...$values) : BigNumber
foreach ($values as $value) {
$value = static::of($value);

if ($sum === null) {
$sum = $value;
} else {
$sum = self::add($sum, $value);
}
$sum = $sum === null ? $value : self::add($sum, $value);
}

if ($sum === null) {
Expand Down
16 changes: 13 additions & 3 deletions brick/math/src/BigRational.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ public static function nd($numerator, $denominator) : BigRational
*/
public static function zero() : BigRational
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigRational|null $zero
*/
static $zero;

if ($zero === null) {
Expand All @@ -127,7 +130,10 @@ public static function zero() : BigRational
*/
public static function one() : BigRational
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigRational|null $one
*/
static $one;

if ($one === null) {
Expand All @@ -146,7 +152,10 @@ public static function one() : BigRational
*/
public static function ten() : BigRational
{
/** @psalm-suppress ImpureStaticVariable */
/**
* @psalm-suppress ImpureStaticVariable
* @var BigRational|null $ten
*/
static $ten;

if ($ten === null) {
Expand Down Expand Up @@ -458,6 +467,7 @@ public function serialize() : string
* This method is only here to implement interface Serializable and cannot be accessed directly.
*
* @internal
* @psalm-suppress RedundantPropertyInitializationCheck
*
* @param string $value
*
Expand Down
5 changes: 4 additions & 1 deletion brick/math/src/Internal/Calculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,9 @@ private function bitwise(string $operator, string $a, string $b) : string
}

/**
* @psalm-suppress InvalidOperand
* @see https://github.com/vimeo/psalm/issues/4456
*
* @param string $number A positive, binary number.
*
* @return string
Expand All @@ -685,7 +688,7 @@ private function twosComplement(string $number) : string
{
$xor = \str_repeat("\xff", \strlen($number));

$number = $number ^ $xor;
$number ^= $xor;

for ($i = \strlen($number) - 1; $i >= 0; $i--) {
$byte = \ord($number[$i]);
Expand Down
Loading