Skip to content

Commit

Permalink
chore: clean up types and PHP 8-ify the code
Browse files Browse the repository at this point in the history
  • Loading branch information
ramsey committed Sep 16, 2022
1 parent ef84248 commit ae247f1
Show file tree
Hide file tree
Showing 39 changed files with 222 additions and 605 deletions.
2 changes: 1 addition & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ parameters:
count: 9
path: ./src/Lazy/LazyUuidFromString.php
-
message: '#^Property Ramsey\\Uuid\\FeatureSet::\$disableBigNumber is never read, only written#'
message: '#^Constructor of class Ramsey\\Uuid\\FeatureSet has an unused parameter \$forceNoBigNumber\.#'
count: 1
path: ./src/FeatureSet.php

Expand Down
13 changes: 2 additions & 11 deletions src/Builder/DegradedUuidBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@
*/
class DegradedUuidBuilder implements UuidBuilderInterface
{
/**
* @var NumberConverterInterface
*/
private $numberConverter;

/**
* @var TimeConverterInterface
*/
private $timeConverter;
private TimeConverterInterface $timeConverter;

/**
* @param NumberConverterInterface $numberConverter The number converter to
Expand All @@ -47,10 +39,9 @@ class DegradedUuidBuilder implements UuidBuilderInterface
* for converting timestamps extracted from a UUID to Unix timestamps
*/
public function __construct(
NumberConverterInterface $numberConverter,
private NumberConverterInterface $numberConverter,
?TimeConverterInterface $timeConverter = null
) {
$this->numberConverter = $numberConverter;
$this->timeConverter = $timeConverter ?: new DegradedTimeConverter();
}

Expand Down
8 changes: 1 addition & 7 deletions src/Builder/FallbackBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,11 @@
*/
class FallbackBuilder implements UuidBuilderInterface
{
/**
* @var iterable<UuidBuilderInterface>
*/
private $builders;

/**
* @param iterable<UuidBuilderInterface> $builders An array of UUID builders
*/
public function __construct(iterable $builders)
public function __construct(private iterable $builders)
{
$this->builders = $builders;
}

/**
Expand Down
8 changes: 1 addition & 7 deletions src/Codec/StringCodec.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,13 @@
*/
class StringCodec implements CodecInterface
{
/**
* @var UuidBuilderInterface
*/
private $builder;

/**
* Constructs a StringCodec
*
* @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
*/
public function __construct(UuidBuilderInterface $builder)
public function __construct(private UuidBuilderInterface $builder)
{
$this->builder = $builder;
}

public function encode(UuidInterface $uuid): string
Expand Down
5 changes: 1 addition & 4 deletions src/Converter/Number/BigNumberConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
*/
class BigNumberConverter implements NumberConverterInterface
{
/**
* @var NumberConverterInterface
*/
private $converter;
private NumberConverterInterface $converter;

public function __construct()
{
Expand Down
8 changes: 1 addition & 7 deletions src/Converter/Number/GenericNumberConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,8 @@
*/
class GenericNumberConverter implements NumberConverterInterface
{
/**
* @var CalculatorInterface
*/
private $calculator;

public function __construct(CalculatorInterface $calculator)
public function __construct(private CalculatorInterface $calculator)
{
$this->calculator = $calculator;
}

/**
Expand Down
5 changes: 1 addition & 4 deletions src/Converter/Time/BigNumberTimeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
*/
class BigNumberTimeConverter implements TimeConverterInterface
{
/**
* @var TimeConverterInterface
*/
private $converter;
private TimeConverterInterface $converter;

public function __construct()
{
Expand Down
8 changes: 1 addition & 7 deletions src/Converter/Time/GenericTimeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,8 @@ class GenericTimeConverter implements TimeConverterInterface
*/
private const MICROSECOND_INTERVALS = '10';

/**
* @var CalculatorInterface
*/
private $calculator;

public function __construct(CalculatorInterface $calculator)
public function __construct(private CalculatorInterface $calculator)
{
$this->calculator = $calculator;
}

public function calculateTime(string $seconds, string $microseconds): Hexadecimal
Expand Down
21 changes: 5 additions & 16 deletions src/Converter/Time/PhpTimeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,9 @@ class PhpTimeConverter implements TimeConverterInterface
*/
private const MICROSECOND_INTERVALS = 10;

/**
* @var CalculatorInterface
*/
private $calculator;

/**
* @var TimeConverterInterface
*/
private $fallbackConverter;

/**
* @var int
*/
private $phpPrecision;
private int $phpPrecision;
private CalculatorInterface $calculator;
private TimeConverterInterface $fallbackConverter;

public function __construct(
?CalculatorInterface $calculator = null,
Expand Down Expand Up @@ -132,11 +121,11 @@ public function convertTime(Hexadecimal $uuidTimestamp): Time
}

/**
* @param int|float $time The time to split into seconds and microseconds
* @param float|int $time The time to split into seconds and microseconds
*
* @return string[]
*/
private function splitTime($time): array
private function splitTime(float | int $time): array
{
$split = explode('.', (string) $time, 2);

Expand Down
5 changes: 1 addition & 4 deletions src/Converter/Time/UnixTimeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,8 @@ class UnixTimeConverter implements TimeConverterInterface
{
private const MILLISECONDS = 1000;

private CalculatorInterface $calculator;

public function __construct(CalculatorInterface $calculator)
public function __construct(private CalculatorInterface $calculator)
{
$this->calculator = $calculator;
}

public function calculateTime(string $seconds, string $microseconds): Hexadecimal
Expand Down
108 changes: 17 additions & 91 deletions src/FeatureSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,111 +63,37 @@
*/
class FeatureSet
{
/**
* @var bool
*/
private $disableBigNumber = false;

/**
* @var bool
*/
private $disable64Bit = false;

/**
* @var bool
*/
private $ignoreSystemNode = false;

/**
* @var bool
*/
private $enablePecl = false;

/**
* @var UuidBuilderInterface
*/
private $builder;

/**
* @var CodecInterface
*/
private $codec;

/**
* @var DceSecurityGeneratorInterface
*/
private $dceSecurityGenerator;

/**
* @var NameGeneratorInterface
*/
private $nameGenerator;

/**
* @var NodeProviderInterface
*/
private $nodeProvider;

/**
* @var NumberConverterInterface
*/
private $numberConverter;

/**
* @var TimeConverterInterface
*/
private $timeConverter;

/**
* @var RandomGeneratorInterface
*/
private $randomGenerator;

/**
* @var TimeGeneratorInterface
*/
private $timeGenerator;

/**
* @var TimeProviderInterface|null
*/
private $timeProvider;

/**
* @var ValidatorInterface
*/
private $validator;

/**
* @var CalculatorInterface
*/
private $calculator;

private ?TimeProviderInterface $timeProvider = null;
private CalculatorInterface $calculator;
private CodecInterface $codec;
private DceSecurityGeneratorInterface $dceSecurityGenerator;
private NameGeneratorInterface $nameGenerator;
private NodeProviderInterface $nodeProvider;
private NumberConverterInterface $numberConverter;
private RandomGeneratorInterface $randomGenerator;
private TimeConverterInterface $timeConverter;
private TimeGeneratorInterface $timeGenerator;
private TimeGeneratorInterface $unixTimeGenerator;
private UuidBuilderInterface $builder;
private ValidatorInterface $validator;

/**
* @param bool $useGuids True build UUIDs using the GuidStringCodec
* @param bool $force32Bit True to force the use of 32-bit functionality
* (primarily for testing purposes)
* @param bool $forceNoBigNumber True to disable the use of moontoast/math
* (primarily for testing purposes)
* @param bool $forceNoBigNumber (obsolete)
* @param bool $ignoreSystemNode True to disable attempts to check for the
* system node ID (primarily for testing purposes)
* @param bool $enablePecl True to enable the use of the PeclUuidTimeGenerator
* to generate version 1 UUIDs
*/
public function __construct(
bool $useGuids = false,
bool $force32Bit = false,
private bool $force32Bit = false,
bool $forceNoBigNumber = false,
bool $ignoreSystemNode = false,
bool $enablePecl = false
private bool $ignoreSystemNode = false,
private bool $enablePecl = false
) {
$this->disableBigNumber = $forceNoBigNumber;
$this->disable64Bit = $force32Bit;
$this->ignoreSystemNode = $ignoreSystemNode;
$this->enablePecl = $enablePecl;

$this->randomGenerator = $this->buildRandomGenerator();
$this->setCalculator(new BrickMathCalculator());
$this->builder = $this->buildUuidBuilder($useGuids);
Expand Down Expand Up @@ -474,6 +400,6 @@ private function buildUuidBuilder(bool $useGuids = false): UuidBuilderInterface
*/
private function is64BitSystem(): bool
{
return PHP_INT_SIZE === 8 && !$this->disable64Bit;
return PHP_INT_SIZE === 8 && !$this->force32Bit;
}
}
13 changes: 7 additions & 6 deletions src/Fields/SerializableFieldsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,23 @@ public function __serialize(): array
/**
* Constructs the object from a serialized string representation
*
* @param string $serialized The serialized string representation of the object
* @param string $data The serialized string representation of the object
*
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
* @psalm-suppress UnusedMethodCall
*/
public function unserialize($serialized): void
public function unserialize(string $data): void
{
if (strlen($serialized) === 16) {
$this->__construct($serialized);
if (strlen($data) === 16) {
$this->__construct($data);
} else {
$this->__construct(base64_decode($serialized));
$this->__construct(base64_decode($data));
}
}

/**
* @param array{bytes?: string} $data
*
* @psalm-suppress UnusedMethodCall
*/
public function __unserialize(array $data): void
{
Expand Down
20 changes: 4 additions & 16 deletions src/Generator/CombGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,10 @@ class CombGenerator implements RandomGeneratorInterface
{
public const TIMESTAMP_BYTES = 6;

/**
* @var RandomGeneratorInterface
*/
private $randomGenerator;

/**
* @var NumberConverterInterface
*/
private $converter;

public function __construct(
RandomGeneratorInterface $generator,
NumberConverterInterface $numberConverter
private RandomGeneratorInterface $generator,
private NumberConverterInterface $numberConverter
) {
$this->converter = $numberConverter;
$this->randomGenerator = $generator;
}

/**
Expand All @@ -95,11 +83,11 @@ public function generate(int $length): string

$hash = '';
if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {
$hash = $this->randomGenerator->generate($length - self::TIMESTAMP_BYTES);
$hash = $this->generator->generate($length - self::TIMESTAMP_BYTES);
}

$lsbTime = str_pad(
$this->converter->toHex($this->timestamp()),
$this->numberConverter->toHex($this->timestamp()),
self::TIMESTAMP_BYTES * 2,
'0',
STR_PAD_LEFT
Expand Down
Loading

0 comments on commit ae247f1

Please sign in to comment.