Make fully qualified instead of import everything
parent
17211c7228
commit
161bc30b89
|
@ -4,11 +4,16 @@
|
|||
-->
|
||||
<ruleset name="Custom Standard" namespace="MyProject\CS\Standard">
|
||||
<rule ref="SandFox_PHP71">
|
||||
<!-- all functions are imported -->
|
||||
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions"/>
|
||||
<!-- crashes -->
|
||||
<exclude name="Security.BadFunctions.CallbackFunctions"/>
|
||||
</rule>
|
||||
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions">
|
||||
<properties>
|
||||
<!-- Reset to default (include all functions) -->
|
||||
<property name="includeSpecialFunctions" value="0"/>
|
||||
</properties>
|
||||
</rule>
|
||||
<rule ref="SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants"/>
|
||||
<!-- no need to cover 8.2 and later -->
|
||||
<config name="testVersion" value="7.1-8.1"/>
|
||||
<file>src</file>
|
||||
|
|
|
@ -13,8 +13,6 @@ namespace Arokettu\Random;
|
|||
|
||||
use GMP;
|
||||
|
||||
use function extension_loaded;
|
||||
|
||||
/**
|
||||
* @codeCoverageIgnore We don't care about math that was not used
|
||||
*/
|
||||
|
@ -35,11 +33,11 @@ abstract class Math
|
|||
protected static function build(int $sizeof): self
|
||||
{
|
||||
// only less because PHP int is always signed
|
||||
if ($sizeof < PHP_INT_SIZE) {
|
||||
if ($sizeof < \PHP_INT_SIZE) {
|
||||
return new MathNative($sizeof);
|
||||
}
|
||||
|
||||
if (extension_loaded('gmp')) {
|
||||
if (\extension_loaded('gmp')) {
|
||||
return new MathGMP($sizeof);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,17 +15,6 @@ namespace Arokettu\Random;
|
|||
|
||||
use GMP;
|
||||
|
||||
use function gmp_export;
|
||||
use function gmp_import;
|
||||
use function gmp_init;
|
||||
use function gmp_intval;
|
||||
use function gmp_pow;
|
||||
use function str_pad;
|
||||
use function strlen;
|
||||
|
||||
use const GMP_LITTLE_ENDIAN;
|
||||
use const GMP_LSW_FIRST;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @psalm-suppress MoreSpecificImplementedParamType
|
||||
|
@ -45,7 +34,7 @@ final class MathGMP extends Math
|
|||
*/
|
||||
public function __construct(int $sizeof)
|
||||
{
|
||||
$this->mask = gmp_pow(2, $sizeof * 8) - 1;
|
||||
$this->mask = \gmp_pow(2, $sizeof * 8) - 1;
|
||||
$this->sizeof = $sizeof;
|
||||
}
|
||||
|
||||
|
@ -145,7 +134,7 @@ final class MathGMP extends Math
|
|||
*/
|
||||
public function fromHex(string $value)
|
||||
{
|
||||
return gmp_init($value, 16);
|
||||
return \gmp_init($value, 16);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -163,16 +152,16 @@ final class MathGMP extends Math
|
|||
*/
|
||||
public function fromBinary(string $value)
|
||||
{
|
||||
switch (strlen($value) <=> $this->sizeof) {
|
||||
switch (\strlen($value) <=> $this->sizeof) {
|
||||
case -1:
|
||||
$value = str_pad($value, $this->sizeof, "\0");
|
||||
$value = \str_pad($value, $this->sizeof, "\0");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
$value = substr($value, 0, $this->sizeof);
|
||||
$value = \substr($value, 0, $this->sizeof);
|
||||
}
|
||||
|
||||
return gmp_import($value, $this->sizeof, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST);
|
||||
return \gmp_import($value, $this->sizeof, \GMP_LITTLE_ENDIAN | \GMP_LSW_FIRST);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -180,7 +169,7 @@ final class MathGMP extends Math
|
|||
*/
|
||||
public function toInt($value): int
|
||||
{
|
||||
return gmp_intval($value);
|
||||
return \gmp_intval($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -189,10 +178,10 @@ final class MathGMP extends Math
|
|||
public function toSignedInt($value): int
|
||||
{
|
||||
if (($value & 1 << ($this->sizeof * 8 - 1)) != 0) { // sign
|
||||
$value -= gmp_pow(2, $this->sizeof * 8);
|
||||
$value -= \gmp_pow(2, $this->sizeof * 8);
|
||||
}
|
||||
|
||||
return gmp_intval($value);
|
||||
return \gmp_intval($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -201,7 +190,7 @@ final class MathGMP extends Math
|
|||
public function toBinary($value): string
|
||||
{
|
||||
// gmp_export returns empty string for zero, we should return exact bytes as sizeof
|
||||
return str_pad(gmp_export($value, $this->sizeof, GMP_LITTLE_ENDIAN | GMP_LSW_FIRST), $this->sizeof, "\0");
|
||||
return \str_pad(\gmp_export($value, $this->sizeof, \GMP_LITTLE_ENDIAN | \GMP_LSW_FIRST), $this->sizeof, "\0");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -11,14 +11,6 @@ declare(strict_types=1);
|
|||
|
||||
namespace Arokettu\Random;
|
||||
|
||||
use function bin2hex;
|
||||
use function dechex;
|
||||
use function extension_loaded;
|
||||
use function hex2bin;
|
||||
use function hexdec;
|
||||
use function strlen;
|
||||
use function strrev;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* @psalm-suppress MoreSpecificImplementedParamType
|
||||
|
@ -43,7 +35,7 @@ class MathNative extends Math
|
|||
$this->mask = (2 ** ($sizeof * 8)) - 1;
|
||||
$this->sizeof = $sizeof;
|
||||
// multiplier to avoid overflow
|
||||
$this->auxMath = extension_loaded('gmp') ? new MathGMP($sizeof) : new MathUnsigned($sizeof);
|
||||
$this->auxMath = \extension_loaded('gmp') ? new MathGMP($sizeof) : new MathUnsigned($sizeof);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -144,7 +136,7 @@ class MathNative extends Math
|
|||
*/
|
||||
public function fromHex(string $value)
|
||||
{
|
||||
return hexdec($value);
|
||||
return \hexdec($value);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -162,16 +154,16 @@ class MathNative extends Math
|
|||
*/
|
||||
public function fromBinary(string $value)
|
||||
{
|
||||
switch (strlen($value) <=> $this->sizeof) {
|
||||
switch (\strlen($value) <=> $this->sizeof) {
|
||||
case -1:
|
||||
$value = str_pad($value, $this->sizeof, "\0");
|
||||
$value = \str_pad($value, $this->sizeof, "\0");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
$value = substr($value, 0, $this->sizeof);
|
||||
$value = \substr($value, 0, $this->sizeof);
|
||||
}
|
||||
|
||||
return $this->fromHex(bin2hex(strrev($value)));
|
||||
return $this->fromHex(\bin2hex(\strrev($value)));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -199,9 +191,9 @@ class MathNative extends Math
|
|||
*/
|
||||
public function toBinary($value): string
|
||||
{
|
||||
$hex = dechex($value);
|
||||
$bin = hex2bin(strlen($hex) % 2 ? '0' . $hex : $hex);
|
||||
return str_pad(strrev($bin), $this->sizeof, "\0");
|
||||
$hex = \dechex($value);
|
||||
$bin = \hex2bin(\strlen($hex) % 2 ? '0' . $hex : $hex);
|
||||
return \str_pad(\strrev($bin), $this->sizeof, "\0");
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -24,8 +24,6 @@ use function Arokettu\Unsigned\sub;
|
|||
use function Arokettu\Unsigned\sub_int;
|
||||
use function Arokettu\Unsigned\to_int;
|
||||
use function Arokettu\Unsigned\to_signed_int;
|
||||
use function str_split;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
|
@ -161,13 +159,13 @@ final class MathUnsigned extends Math
|
|||
*/
|
||||
public function fromBinary(string $value)
|
||||
{
|
||||
switch (strlen($value) <=> $this->sizeof) {
|
||||
switch (\strlen($value) <=> $this->sizeof) {
|
||||
case -1:
|
||||
$value = str_pad($value, $this->sizeof, "\0");
|
||||
$value = \str_pad($value, $this->sizeof, "\0");
|
||||
break;
|
||||
|
||||
case 1:
|
||||
$value = substr($value, 0, $this->sizeof);
|
||||
$value = \substr($value, 0, $this->sizeof);
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
@ -204,7 +202,7 @@ final class MathUnsigned extends Math
|
|||
public function splitHiLo($value): array
|
||||
{
|
||||
/** @psalm-suppress PossiblyInvalidArrayAccess */
|
||||
[$lo, $hi] = str_split($value, $this->sizeof >> 1);
|
||||
[$lo, $hi] = \str_split($value, $this->sizeof >> 1);
|
||||
return [$hi, $lo];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,10 +11,6 @@ namespace Arokettu\Random;
|
|||
|
||||
use Exception;
|
||||
|
||||
use function serialize;
|
||||
use function trigger_error;
|
||||
use function unserialize;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
|
@ -26,8 +22,8 @@ trait Serialization
|
|||
|
||||
public function serialize(): string
|
||||
{
|
||||
trigger_error('Serialized object will be incompatible with PHP 8.2', E_USER_WARNING);
|
||||
return serialize($this->__serialize());
|
||||
\trigger_error('Serialized object will be incompatible with PHP 8.2', \E_USER_WARNING);
|
||||
return \serialize($this->__serialize());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,7 +33,7 @@ trait Serialization
|
|||
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
||||
public function unserialize($data): void
|
||||
{
|
||||
$this->__unserialize(unserialize($data));
|
||||
$this->__unserialize(\unserialize($data));
|
||||
}
|
||||
|
||||
public function __serialize(): array
|
||||
|
@ -53,7 +49,7 @@ trait Serialization
|
|||
$this->initMath();
|
||||
$result = $this->loadStates($data[1] ?? []);
|
||||
if ($result === false) {
|
||||
throw new Exception(sprintf('Invalid serialization data for %s object', static::class));
|
||||
throw new Exception(\sprintf('Invalid serialization data for %s object', static::class));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -25,17 +25,6 @@ use RuntimeException;
|
|||
use Serializable;
|
||||
use ValueError;
|
||||
|
||||
use function array_fill;
|
||||
use function array_map;
|
||||
use function bin2hex;
|
||||
use function hex2bin;
|
||||
use function random_int;
|
||||
|
||||
use const MT_RAND_MT19937;
|
||||
use const MT_RAND_PHP;
|
||||
use const PHP_INT_MAX;
|
||||
use const PHP_INT_MIN;
|
||||
|
||||
/*
|
||||
The following mt19937 algorithms are based on a C++ class MTRand by
|
||||
Richard J. Wagner. For more information see the web page at
|
||||
|
@ -125,17 +114,17 @@ final class Mt19937 implements Engine, Serializable
|
|||
/** @var GMP|string|int */
|
||||
private static $ZERO;
|
||||
|
||||
public function __construct(?int $seed = null, int $mode = MT_RAND_MT19937)
|
||||
public function __construct(?int $seed = null, int $mode = \MT_RAND_MT19937)
|
||||
{
|
||||
$this->initMath();
|
||||
|
||||
if ($mode !== MT_RAND_PHP && $mode !== MT_RAND_MT19937) {
|
||||
if ($mode !== \MT_RAND_PHP && $mode !== \MT_RAND_MT19937) {
|
||||
throw new ValueError(__METHOD__ . '(): Argument #2 ($mode) must be either MT_RAND_MT19937 or MT_RAND_PHP');
|
||||
}
|
||||
$this->mode = $mode;
|
||||
|
||||
try {
|
||||
$this->seed($seed ?? random_int(PHP_INT_MIN, PHP_INT_MAX));
|
||||
$this->seed($seed ?? \random_int(\PHP_INT_MIN, \PHP_INT_MAX));
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException('Random number generation failed');
|
||||
}
|
||||
|
@ -165,7 +154,7 @@ final class Mt19937 implements Engine, Serializable
|
|||
private function seed(int $seed): void
|
||||
{
|
||||
/** @var GMP[]|string[]|int[] $state */
|
||||
$state = array_fill(0, self::N, null);
|
||||
$state = \array_fill(0, self::N, null);
|
||||
|
||||
$prevState = $state[0] = self::$math->fromInt($seed);
|
||||
for ($i = 1; $i < self::N; $i++) {
|
||||
|
@ -215,7 +204,7 @@ final class Mt19937 implements Engine, Serializable
|
|||
|
||||
$mixBits = self::$math->shiftRight($u & self::$HI_BIT | $v & self::$LO_BITS, 1);
|
||||
|
||||
if ($this->mode === MT_RAND_MT19937) {
|
||||
if ($this->mode === \MT_RAND_MT19937) {
|
||||
$twist = self::$math->toInt($v & self::$LO_BIT) ? self::$TWIST_CONST : self::$ZERO;
|
||||
} else {
|
||||
$twist = self::$math->toInt($u & self::$LO_BIT) ? self::$TWIST_CONST : self::$ZERO;
|
||||
|
@ -245,8 +234,8 @@ final class Mt19937 implements Engine, Serializable
|
|||
*/
|
||||
private function getStates(): array
|
||||
{
|
||||
$states = array_map(function ($state) {
|
||||
return bin2hex(self::$math->toBinary($state));
|
||||
$states = \array_map(function ($state) {
|
||||
return \bin2hex(self::$math->toBinary($state));
|
||||
}, $this->state);
|
||||
$states[] = $this->stateCount;
|
||||
$states[] = $this->mode;
|
||||
|
@ -261,13 +250,13 @@ final class Mt19937 implements Engine, Serializable
|
|||
private function loadStates(array $states): bool
|
||||
{
|
||||
/** @var GMP[] $state */
|
||||
$state = array_fill(0, self::N, null);
|
||||
$state = \array_fill(0, self::N, null);
|
||||
|
||||
for ($i = 0; $i < self::N; $i++) {
|
||||
if (!isset($states[$i])) {
|
||||
return false;
|
||||
}
|
||||
$stateBin = @hex2bin($states[$i]);
|
||||
$stateBin = @\hex2bin($states[$i]);
|
||||
if ($stateBin === false) {
|
||||
return false;
|
||||
}
|
||||
|
@ -278,7 +267,7 @@ final class Mt19937 implements Engine, Serializable
|
|||
$count = $states[self::N];
|
||||
$mode = $states[self::N + 1];
|
||||
|
||||
if ($mode !== MT_RAND_PHP && $mode !== MT_RAND_MT19937) {
|
||||
if ($mode !== \MT_RAND_PHP && $mode !== \MT_RAND_MT19937) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -26,15 +26,6 @@ use Serializable;
|
|||
use TypeError;
|
||||
use ValueError;
|
||||
|
||||
use function array_is_list;
|
||||
use function count;
|
||||
use function get_debug_type;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
use function random_bytes;
|
||||
use function str_split;
|
||||
use function strlen;
|
||||
|
||||
final class PcgOneseq128XslRr64 implements Engine, Serializable
|
||||
{
|
||||
use NoDynamicProperties;
|
||||
|
@ -67,22 +58,22 @@ final class PcgOneseq128XslRr64 implements Engine, Serializable
|
|||
{
|
||||
$this->initMath();
|
||||
|
||||
if (is_int($seed)) {
|
||||
if (\is_int($seed)) {
|
||||
$this->seedInt($seed);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($seed === null) {
|
||||
try {
|
||||
$seed = random_bytes(Math::SIZEOF_UINT128_T);
|
||||
$seed = \random_bytes(Math::SIZEOF_UINT128_T);
|
||||
} catch (Exception $e) {
|
||||
throw new RuntimeException('Failed to generate a random seed');
|
||||
}
|
||||
}
|
||||
|
||||
/** @psalm-suppress RedundantConditionGivenDocblockType we don't trust user input */
|
||||
if (is_string($seed)) {
|
||||
if (strlen($seed) !== Math::SIZEOF_UINT128_T) {
|
||||
if (\is_string($seed)) {
|
||||
if (\strlen($seed) !== Math::SIZEOF_UINT128_T) {
|
||||
throw new ValueError(__METHOD__ . '(): Argument #1 ($seed) must be a 16 byte (128 bit) string');
|
||||
}
|
||||
|
||||
|
@ -93,7 +84,7 @@ final class PcgOneseq128XslRr64 implements Engine, Serializable
|
|||
throw new TypeError(
|
||||
__METHOD__ .
|
||||
'(): Argument #1 ($seed) must be of type string|int|null, ' .
|
||||
get_debug_type($seed) . ' given'
|
||||
\get_debug_type($seed) . ' given'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -125,7 +116,7 @@ final class PcgOneseq128XslRr64 implements Engine, Serializable
|
|||
|
||||
private function seedString(string $seed): void
|
||||
{
|
||||
[$hi, $lo] = str_split($seed, Math::SIZEOF_UINT64_T);
|
||||
[$hi, $lo] = \str_split($seed, Math::SIZEOF_UINT64_T);
|
||||
|
||||
$this->seed128(self::$math128->fromBinary($lo . $hi));
|
||||
}
|
||||
|
@ -202,10 +193,10 @@ final class PcgOneseq128XslRr64 implements Engine, Serializable
|
|||
*/
|
||||
private function getStates(): array
|
||||
{
|
||||
[$lo, $hi] = str_split(self::$math128->toBinary($this->state), Math::SIZEOF_UINT64_T);
|
||||
[$lo, $hi] = \str_split(self::$math128->toBinary($this->state), Math::SIZEOF_UINT64_T);
|
||||
return [
|
||||
bin2hex($hi),
|
||||
bin2hex($lo),
|
||||
\bin2hex($hi),
|
||||
\bin2hex($lo),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -215,15 +206,15 @@ final class PcgOneseq128XslRr64 implements Engine, Serializable
|
|||
*/
|
||||
private function loadStates(array $states): bool
|
||||
{
|
||||
if (!array_is_list($states) || count($states) < 2) {
|
||||
if (!\array_is_list($states) || \count($states) < 2) {
|
||||
return false;
|
||||
}
|
||||
[$hi, $lo] = $states;
|
||||
if (strlen($hi) !== Math::SIZEOF_UINT64_T * 2 || strlen($lo) !== Math::SIZEOF_UINT64_T * 2) {
|
||||
if (\strlen($hi) !== Math::SIZEOF_UINT64_T * 2 || \strlen($lo) !== Math::SIZEOF_UINT64_T * 2) {
|
||||
return false;
|
||||
}
|
||||
$hiBin = @hex2bin($hi);
|
||||
$loBin = @hex2bin($lo);
|
||||
$hiBin = @\hex2bin($hi);
|
||||
$loBin = @\hex2bin($lo);
|
||||
if ($hiBin === false || $loBin === false) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -18,20 +18,18 @@ use Error;
|
|||
use Exception;
|
||||
use Random\CryptoSafeEngine;
|
||||
|
||||
use function random_bytes;
|
||||
|
||||
final class Secure implements CryptoSafeEngine
|
||||
{
|
||||
use NoDynamicProperties;
|
||||
|
||||
public function generate(): string
|
||||
{
|
||||
return random_bytes(PHP_INT_SIZE);
|
||||
return \random_bytes(\PHP_INT_SIZE);
|
||||
}
|
||||
|
||||
private function range(int $min, int $max): ?int
|
||||
{
|
||||
return random_int($min, $max);
|
||||
return \random_int($min, $max);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,14 +26,6 @@ use Serializable;
|
|||
use TypeError;
|
||||
use ValueError;
|
||||
|
||||
use function bin2hex;
|
||||
use function get_debug_type;
|
||||
use function is_int;
|
||||
use function is_string;
|
||||
use function random_bytes;
|
||||
use function str_split;
|
||||
use function strlen;
|
||||
|
||||
/**
|
||||
* @noinspection PhpComposerExtensionStubsInspection
|
||||
*/
|
||||
|
@ -92,7 +84,7 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
if ($seed === null) {
|
||||
try {
|
||||
do {
|
||||
$seed = random_bytes(32);
|
||||
$seed = \random_bytes(32);
|
||||
} while ($seed === "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
|
||||
$this->seedString($seed);
|
||||
} catch (Exception $e) {
|
||||
|
@ -101,8 +93,8 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
return;
|
||||
}
|
||||
|
||||
if (is_string($seed)) {
|
||||
if (strlen($seed) !== 32) {
|
||||
if (\is_string($seed)) {
|
||||
if (\strlen($seed) !== 32) {
|
||||
throw new ValueError(__METHOD__ . '(): Argument #1 ($seed) must be a 32 byte (256 bit) string');
|
||||
}
|
||||
|
||||
|
@ -115,7 +107,7 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
}
|
||||
|
||||
/** @psalm-suppress RedundantConditionGivenDocblockType we don't trust user input */
|
||||
if (is_int($seed)) {
|
||||
if (\is_int($seed)) {
|
||||
$this->seedInt($seed);
|
||||
return;
|
||||
}
|
||||
|
@ -123,7 +115,7 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
throw new TypeError(
|
||||
__METHOD__ .
|
||||
'(): Argument #1 ($seed) must be of type string|int|null, ' .
|
||||
get_debug_type($seed) . ' given'
|
||||
\get_debug_type($seed) . ' given'
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -183,7 +175,7 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
|
||||
private function seedString(string $seed): void
|
||||
{
|
||||
$seeds = str_split($seed, 8);
|
||||
$seeds = \str_split($seed, 8);
|
||||
|
||||
$this->seed256(
|
||||
self::$math->fromBinary($seeds[0]),
|
||||
|
@ -285,10 +277,10 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
private function getStates(): array
|
||||
{
|
||||
return [
|
||||
bin2hex(self::$math->toBinary($this->state[0])),
|
||||
bin2hex(self::$math->toBinary($this->state[1])),
|
||||
bin2hex(self::$math->toBinary($this->state[2])),
|
||||
bin2hex(self::$math->toBinary($this->state[3])),
|
||||
\bin2hex(self::$math->toBinary($this->state[0])),
|
||||
\bin2hex(self::$math->toBinary($this->state[1])),
|
||||
\bin2hex(self::$math->toBinary($this->state[2])),
|
||||
\bin2hex(self::$math->toBinary($this->state[3])),
|
||||
];
|
||||
}
|
||||
|
||||
|
@ -299,7 +291,7 @@ final class Xoshiro256StarStar implements Engine, Serializable
|
|||
{
|
||||
$this->state = [];
|
||||
for ($i = 0; $i < 4; $i++) {
|
||||
$stateBin = @hex2bin($states[$i]);
|
||||
$stateBin = @\hex2bin($states[$i]);
|
||||
if ($stateBin === false) {
|
||||
return false;
|
||||
}
|
||||
|
|
|
@ -27,22 +27,6 @@ use Random\Engine\Secure;
|
|||
use Serializable;
|
||||
use ValueError;
|
||||
|
||||
use function array_key_exists;
|
||||
use function array_keys;
|
||||
use function array_values;
|
||||
use function count;
|
||||
use function floatval;
|
||||
use function intval;
|
||||
use function serialize;
|
||||
use function strlen;
|
||||
use function substr;
|
||||
use function trigger_error;
|
||||
use function unserialize;
|
||||
|
||||
use const E_USER_WARNING;
|
||||
use const MT_RAND_PHP;
|
||||
use const SORT_NUMERIC;
|
||||
|
||||
/**
|
||||
* @property-read Engine $engine
|
||||
*/
|
||||
|
@ -108,12 +92,12 @@ final class Randomizer implements Serializable
|
|||
{
|
||||
$retval = $this->engine->generate();
|
||||
|
||||
$size = strlen($retval);
|
||||
$size = \strlen($retval);
|
||||
|
||||
if ($size === 0) {
|
||||
throw new BrokenRandomEngineError('A random engine must return a non-empty string');
|
||||
} elseif ($size > Math::SIZEOF_UINT64_T) {
|
||||
$retval = substr($retval, 0, Math::SIZEOF_UINT64_T);
|
||||
$retval = \substr($retval, 0, Math::SIZEOF_UINT64_T);
|
||||
}
|
||||
|
||||
return $retval;
|
||||
|
@ -144,7 +128,7 @@ final class Randomizer implements Serializable
|
|||
$this->engine instanceof Mt19937 &&
|
||||
Closure::bind(function () {
|
||||
/** @psalm-suppress UndefinedThisPropertyFetch */
|
||||
return $this->mode === MT_RAND_PHP; // read private property
|
||||
return $this->mode === \MT_RAND_PHP; // read private property
|
||||
}, $this->engine, $this->engine)()
|
||||
) {
|
||||
return $this->rangeBadscaling($min, $max);
|
||||
|
@ -175,7 +159,7 @@ final class Randomizer implements Serializable
|
|||
$result = '';
|
||||
do {
|
||||
$result .= $this->generate();
|
||||
} while (strlen($result) < Math::SIZEOF_UINT32_T);
|
||||
} while (\strlen($result) < Math::SIZEOF_UINT32_T);
|
||||
|
||||
$result = self::$math32->fromBinary($result);
|
||||
|
||||
|
@ -209,7 +193,7 @@ final class Randomizer implements Serializable
|
|||
$result = '';
|
||||
do {
|
||||
$result .= $this->generate();
|
||||
} while (strlen($result) < Math::SIZEOF_UINT32_T);
|
||||
} while (\strlen($result) < Math::SIZEOF_UINT32_T);
|
||||
|
||||
$result = self::$math32->fromBinary($result);
|
||||
}
|
||||
|
@ -226,7 +210,7 @@ final class Randomizer implements Serializable
|
|||
$result = '';
|
||||
do {
|
||||
$result .= $this->generate();
|
||||
} while (strlen($result) < Math::SIZEOF_UINT64_T);
|
||||
} while (\strlen($result) < Math::SIZEOF_UINT64_T);
|
||||
|
||||
$result = self::$math64->fromBinary($result);
|
||||
|
||||
|
@ -260,7 +244,7 @@ final class Randomizer implements Serializable
|
|||
$result = '';
|
||||
do {
|
||||
$result .= $this->generate();
|
||||
} while (strlen($result) < Math::SIZEOF_UINT64_T);
|
||||
} while (\strlen($result) < Math::SIZEOF_UINT64_T);
|
||||
|
||||
$result = self::$math64->fromBinary($result);
|
||||
}
|
||||
|
@ -275,7 +259,7 @@ final class Randomizer implements Serializable
|
|||
$n = self::$math32->toInt(self::$math32->shiftRight($n, 1));
|
||||
// (__n) = (__min) + (zend_long) ((double) ( (double) (__max) - (__min) + 1.0) * ((__n) / ((__tmax) + 1.0)))
|
||||
/** @noinspection PhpCastIsUnnecessaryInspection */
|
||||
return intval($min + intval((floatval($max) - $min + 1.0) * ($n / (self::PHP_MT_RAND_MAX + 1.0))));
|
||||
return \intval($min + \intval((\floatval($max) - $min + 1.0) * ($n / (self::PHP_MT_RAND_MAX + 1.0))));
|
||||
}
|
||||
|
||||
public function nextInt(): int
|
||||
|
@ -300,9 +284,9 @@ final class Randomizer implements Serializable
|
|||
throw new BrokenRandomEngineError('A random engine must return a non-empty string');
|
||||
}
|
||||
$retval .= $result;
|
||||
} while (strlen($retval) < $length);
|
||||
} while (\strlen($retval) < $length);
|
||||
|
||||
return substr($retval, 0, $length);
|
||||
return \substr($retval, 0, $length);
|
||||
}
|
||||
|
||||
public function shuffleArray(array $array): array
|
||||
|
@ -312,8 +296,8 @@ final class Randomizer implements Serializable
|
|||
return [];
|
||||
}
|
||||
|
||||
$hash = array_values($array);
|
||||
$nLeft = count($hash);
|
||||
$hash = \array_values($array);
|
||||
$nLeft = \count($hash);
|
||||
|
||||
while (--$nLeft) {
|
||||
$rndIdx = $this->getInt(0, $nLeft);
|
||||
|
@ -327,11 +311,11 @@ final class Randomizer implements Serializable
|
|||
|
||||
public function shuffleBytes(string $bytes): string
|
||||
{
|
||||
if (strlen($bytes) <= 1) {
|
||||
if (\strlen($bytes) <= 1) {
|
||||
return $bytes;
|
||||
}
|
||||
|
||||
$nLeft = strlen($bytes);
|
||||
$nLeft = \strlen($bytes);
|
||||
|
||||
while (--$nLeft) {
|
||||
$rndIdx = $this->getInt(0, $nLeft);
|
||||
|
@ -347,15 +331,15 @@ final class Randomizer implements Serializable
|
|||
{
|
||||
if (!($this->engine instanceof CryptoSafeEngine)) {
|
||||
// Crypto-safe engines are not expected to produce reproducible sequences
|
||||
trigger_error('pickArrayKeys() may produce results incompatible with native ext-random', E_USER_WARNING);
|
||||
\trigger_error('pickArrayKeys() may produce results incompatible with native ext-random', \E_USER_WARNING);
|
||||
}
|
||||
|
||||
if ($array === []) {
|
||||
throw new ValueError(__METHOD__ . '(): Argument #1 ($array) cannot be empty');
|
||||
}
|
||||
|
||||
$numAvail = count($array);
|
||||
$keys = array_keys($array);
|
||||
$numAvail = \count($array);
|
||||
$keys = \array_keys($array);
|
||||
|
||||
if ($num === 1) {
|
||||
return [$keys[$this->getInt(0, $numAvail - 1)]];
|
||||
|
@ -375,15 +359,15 @@ final class Randomizer implements Serializable
|
|||
while ($i) {
|
||||
$idx = $this->getInt(0, $numAvail - 1);
|
||||
|
||||
if (array_key_exists($idx, $retval) === false) {
|
||||
if (\array_key_exists($idx, $retval) === false) {
|
||||
$retval[$idx] = $keys[$idx];
|
||||
$i--;
|
||||
}
|
||||
}
|
||||
|
||||
ksort($retval, SORT_NUMERIC); // sort by indexes
|
||||
\ksort($retval, \SORT_NUMERIC); // sort by indexes
|
||||
|
||||
return array_values($retval); // remove indexes
|
||||
return \array_values($retval); // remove indexes
|
||||
}
|
||||
|
||||
public function __serialize(): array
|
||||
|
@ -401,8 +385,8 @@ final class Randomizer implements Serializable
|
|||
|
||||
public function serialize(): string
|
||||
{
|
||||
trigger_error('Serialized object will be incompatible with PHP 8.2', E_USER_WARNING);
|
||||
return serialize($this->__serialize());
|
||||
\trigger_error('Serialized object will be incompatible with PHP 8.2', \E_USER_WARNING);
|
||||
return \serialize($this->__serialize());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -411,7 +395,7 @@ final class Randomizer implements Serializable
|
|||
// phpcs:ignore SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
|
||||
public function unserialize($data): void
|
||||
{
|
||||
$this->__unserialize(unserialize($data));
|
||||
$this->__unserialize(\unserialize($data));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -424,7 +408,7 @@ final class Randomizer implements Serializable
|
|||
return $this->engine;
|
||||
}
|
||||
|
||||
trigger_error('Undefined property: ' . self::class . '::$' . $name);
|
||||
\trigger_error('Undefined property: ' . self::class . '::$' . $name);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue