diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 00c96bf..743780c 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,7 +21,7 @@ jobs: strategy: matrix: - php-version: [ '7.4' ] + php-version: [ '8.0' ] operating-system: [ 'ubuntu-latest' ] fail-fast: false @@ -80,7 +80,7 @@ jobs: strategy: matrix: - php-version: [ '7.4' ] + php-version: [ '8.0' ] operating-system: [ 'ubuntu-latest' ] fail-fast: false @@ -136,16 +136,9 @@ jobs: strategy: matrix: - php-version: [ '7.2', '7.3', '7.4' ] + php-version: [ '8.0' ] operating-system: [ 'ubuntu-latest' ] composer-args: [ '' ] - include: - - php-version: '7.2' - operating-system: 'ubuntu-latest' - composer-args: '--prefer-lowest' - - php-version: '8.0' - operating-system: 'ubuntu-latest' - composer-args: '--ignore-platform-reqs' fail-fast: false @@ -207,7 +200,7 @@ jobs: strategy: matrix: - php-version: [ '7.4' ] + php-version: [ '8.0' ] operating-system: [ 'ubuntu-latest' ] fail-fast: false diff --git a/composer.json b/composer.json index 31c5fef..8d9fa92 100644 --- a/composer.json +++ b/composer.json @@ -15,18 +15,17 @@ } ], "require": { - "php": "^7.2|^8.0", - "consistence/consistence": "^1.0 || ^2.0", + "php": "^8.0", + "consistence-community/consistence": "^2.1", "nette/utils": "^3.0 || ^2.4", "nette/http": "^3.0 || ^2.4", "jschaedl/iban-validation": "^1.0", - "egulias/email-validator": "^3.1.1" + "egulias/email-validator": "^3.1" }, "require-dev": { "nette/tester": "^2.3.0", "phpstan/phpstan-nette": "^0.12.8", - "slevomat/coding-standard": "^6.4", - "consistence/coding-standard": "^3.10", + "slevomat/coding-standard": "^7.0", "tracy/tracy": "^2.8" }, "autoload": { diff --git a/src/Address.php b/src/Address.php index 87e5dd2..7562d81 100644 --- a/src/Address.php +++ b/src/Address.php @@ -11,25 +11,13 @@ final class Address use ArrayExtractableTrait; - /** - * @var string - */ - private $streetAndNumber; + private string $streetAndNumber; - /** - * @var string - */ - private $town; + private string $town; - /** - * @var \SmartEmailing\Types\ZipCode - */ - private $zipCode; + private ZipCode $zipCode; - /** - * @var \SmartEmailing\Types\CountryCode - */ - private $country; + private CountryCode $country; /** * @param array $data @@ -37,10 +25,10 @@ final class Address private function __construct( array $data ) { - $this->streetAndNumber = PrimitiveTypes::extractString($data, 'street_and_number'); - $this->town = PrimitiveTypes::extractString($data, 'town'); - $this->zipCode = ZipCode::extract($data, 'zip_code'); + $this->streetAndNumber = (string) NonEmptyString::extract($data, 'street_and_number'); + $this->town = (string) NonEmptyString::extract($data, 'town'); $this->country = CountryCode::extract($data, 'country'); + $this->zipCode = ZipCode::extract($data, 'zip_code', countryCode: $this->country); } /** diff --git a/src/Arrays.php b/src/Arrays.php index b49e748..7ac9456 100644 --- a/src/Arrays.php +++ b/src/Arrays.php @@ -10,11 +10,10 @@ abstract class Arrays { /** - * @param mixed $value * @return array */ final public static function getArray( - $value + mixed $value ): array { if (\is_array($value)) { return $value; @@ -24,14 +23,12 @@ final public static function getArray( } /** - * @param mixed $value - * @param bool $nullIfInvalid * @return array|null */ final public static function getArrayOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?array { + ): array | null { if (\is_array($value)) { return $value; } @@ -54,14 +51,13 @@ final public static function getArrayOrNull( /** * Preserves keys * - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractArray( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { $value = ExtractableHelpers::extractValue($data, $key); @@ -76,17 +72,15 @@ final public static function extractArray( /** * Preserves keys * - * @param array $data - * @param string $key - * @param bool $nullIfInvalid + * @param array|\ArrayAccess $data * @return array|null * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractArrayOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?array { + ): array | null { if (!isset($data[$key])) { return null; } @@ -103,11 +97,10 @@ final public static function extractArrayOrNull( } /** - * @param mixed $value * @return array */ final public static function getIntArray( - $value + mixed $value ): array { $array = self::getArray($value); @@ -121,14 +114,12 @@ final public static function getIntArray( } /** - * @param mixed $value - * @param bool $nullIfInvalid * @return array|null */ final public static function getIntArrayOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?array { + ): array | null { $array = self::getArrayOrNull($value, $nullIfInvalid); if ($array === null) { @@ -153,14 +144,13 @@ final public static function getIntArrayOrNull( } /** - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractIntArray( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { $array = Arrays::extractArray($data, $key); @@ -174,17 +164,15 @@ final public static function extractIntArray( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid + * @param array|\ArrayAccess $data * @return array|null * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractIntArrayOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?array { + ): array | null { $array = Arrays::extractArrayOrNull($data, $key, $nullIfInvalid); if ($array === null) { @@ -195,11 +183,10 @@ final public static function extractIntArrayOrNull( } /** - * @param mixed $value * @return array */ final public static function getFloatArray( - $value + mixed $value ): array { $array = self::getArray($value); @@ -213,14 +200,12 @@ final public static function getFloatArray( } /** - * @param mixed $value - * @param bool $nullIfInvalid * @return array|null */ final public static function getFloatArrayOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?array { + ): array | null { $array = self::getArrayOrNull($value, $nullIfInvalid); if ($array === null) { @@ -245,14 +230,13 @@ final public static function getFloatArrayOrNull( } /** - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractFloatArray( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { $array = Arrays::extractArray($data, $key); @@ -266,17 +250,15 @@ final public static function extractFloatArray( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid + * @param array|\ArrayAccess $data * @return array|null * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractFloatArrayOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?array { + ): array | null { $array = Arrays::extractArrayOrNull($data, $key, $nullIfInvalid); if ($array === null) { @@ -287,11 +269,10 @@ final public static function extractFloatArrayOrNull( } /** - * @param mixed $value * @return array */ final public static function getStringArray( - $value + mixed $value ): array { $array = self::getArray($value); @@ -305,14 +286,12 @@ final public static function getStringArray( } /** - * @param mixed $value - * @param bool $nullIfInvalid * @return array|null */ final public static function getStringArrayOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?array { + ): array | null { $array = self::getArrayOrNull($value, $nullIfInvalid); if ($array === null) { @@ -337,14 +316,13 @@ final public static function getStringArrayOrNull( } /** - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractStringArray( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { $array = Arrays::extractArray($data, $key); @@ -358,17 +336,15 @@ final public static function extractStringArray( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid + * @param array|\ArrayAccess $data * @return array|null * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractStringArrayOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?array { + ): array | null { $array = Arrays::extractArrayOrNull($data, $key, $nullIfInvalid); if ($array === null) { diff --git a/src/Base64String.php b/src/Base64String.php index 3f372a0..f324076 100644 --- a/src/Base64String.php +++ b/src/Base64String.php @@ -12,10 +12,7 @@ final class Base64String implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value @@ -32,7 +29,7 @@ public static function encode( string $value ): self { - return new static( + return new self( \base64_encode($value) ); } diff --git a/src/CompanyRegistrationNumber.php b/src/CompanyRegistrationNumber.php index cce5c7a..c49bc15 100644 --- a/src/CompanyRegistrationNumber.php +++ b/src/CompanyRegistrationNumber.php @@ -13,10 +13,7 @@ final class CompanyRegistrationNumber implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value @@ -166,8 +163,6 @@ private function isValidPL( } /** - * @param string $value - * @return bool * @see https://en.wikipedia.org/wiki/Employer_Identification_Number */ private function isValidUS( diff --git a/src/DateTimeFormatter.php b/src/DateTimeFormatter.php index 88a212d..c1f59f3 100644 --- a/src/DateTimeFormatter.php +++ b/src/DateTimeFormatter.php @@ -8,9 +8,9 @@ abstract class DateTimeFormatter { final public static function formatOrNull( - ?\DateTimeInterface $dateTime, + \DateTimeInterface | null $dateTime, string $format = DateTimeFormat::DATETIME - ): ?string { + ): string | null { if ($dateTime === null) { return null; } diff --git a/src/DateTimeRange.php b/src/DateTimeRange.php index 3f4d99e..61aaa16 100644 --- a/src/DateTimeRange.php +++ b/src/DateTimeRange.php @@ -11,20 +11,11 @@ final class DateTimeRange implements ToArrayInterface use ArrayExtractableTrait; - /** - * @var \DateTimeImmutable - */ - private $from; + private \DateTimeImmutable $from; - /** - * @var \DateTimeImmutable - */ - private $to; + private \DateTimeImmutable $to; - /** - * @var int - */ - private $durationInSeconds; + private int $durationInSeconds; /** * @param array $data diff --git a/src/DateTimes.php b/src/DateTimes.php index acc2f80..468de37 100644 --- a/src/DateTimes.php +++ b/src/DateTimes.php @@ -4,17 +4,13 @@ namespace SmartEmailing\Types; -use Nette\Utils\Arrays; +use SmartEmailing\Types\Helpers\ExtractableHelpers; abstract class DateTimes { - /** - * @param mixed $value - * @return \DateTime - */ final public static function from( - $value + mixed $value ): \DateTime { if ($value instanceof \DateTime) { return $value; @@ -41,15 +37,10 @@ final public static function from( ); } - /** - * @param mixed $value - * @param bool $getNullIfInvalid - * @return \DateTime - */ public static function fromOrNull( - $value, + mixed $value, bool $getNullIfInvalid = false - ): ?\DateTime { + ): \DateTime | null { if ($value === null) { return null; } @@ -66,16 +57,14 @@ public static function fromOrNull( } /** - * @param array $data - * @param string $key - * @return \DateTime + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extract( - array &$data, + array | \ArrayAccess $data, string $key ): \DateTime { - $value = Arrays::get($data, $key, ''); + $value = ExtractableHelpers::extractValue($data, $key); try { return self::from($value); @@ -85,40 +74,10 @@ final public static function extract( } /** - * @param array $data - * @param string $key - * @return \DateTime - * @throws \SmartEmailing\Types\InvalidTypeException - * @deprecated Use Dates::extract - */ - final public static function extractDate( - array &$data, - string $key - ): \DateTime { - return Dates::extract($data, $key); - } - - /** - * @param array $data - * @param string $key - * @return \DateTime|null - * @deprecated Use Dates::extractDateOrNull - */ - final public static function extractDateOrNull( - array &$data, - string $key - ): ?\DateTime { - return Dates::extractOrNull($data, $key); - } - - /** - * @param array $data - * @param string $key - * @param bool $getNullIfInvalid - * @return \DateTime + * @param array|\ArrayAccess $data */ final public static function extractOrNull( - array &$data, + array | \ArrayAccess $data, string $key, bool $getNullIfInvalid = false ): ?\DateTime { @@ -129,7 +88,7 @@ final public static function extractOrNull( if ($getNullIfInvalid) { try { return self::extract($data, $key); - } catch (InvalidTypeException $e) { + } catch (InvalidTypeException) { return null; } } diff --git a/src/DateTimesImmutable.php b/src/DateTimesImmutable.php index 0738fef..25b85df 100644 --- a/src/DateTimesImmutable.php +++ b/src/DateTimesImmutable.php @@ -7,27 +7,18 @@ abstract class DateTimesImmutable { - /** - * @param mixed $value - * @return \DateTimeImmutable - */ final public static function from( - $value + mixed $value ): \DateTimeImmutable { $dateTime = DateTimes::from($value); return self::immutate($dateTime); } - /** - * @param mixed $value - * @param bool $getNullIfInvalid - * @return \DateTimeImmutable - */ public static function fromOrNull( - $value, + mixed $value, bool $getNullIfInvalid = false - ): ?\DateTimeImmutable { + ): \DateTimeImmutable | null { $dateTime = DateTimes::fromOrNull($value, $getNullIfInvalid); if ($dateTime === null) { @@ -38,13 +29,11 @@ public static function fromOrNull( } /** - * @param array $data - * @param string $key - * @return \DateTimeImmutable + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extract( - array &$data, + array | \ArrayAccess $data, string $key ): \DateTimeImmutable { $dateTime = DateTimes::extract( @@ -56,43 +45,14 @@ final public static function extract( } /** - * @param array $data - * @param string $key - * @return \DateTimeImmutable - * @throws \SmartEmailing\Types\InvalidTypeException - * @deprecated Use DatesImmutable::extract - */ - final public static function extractDate( - array &$data, - string $key - ): \DateTimeImmutable { - return DatesImmutable::extract($data, $key); - } - - /** - * @param array $data - * @param string $key - * @return \DateTimeImmutable|null - * @deprecated Use DatesImmutable::extractDateOrNull - */ - final public static function extractDateOrNull( - array &$data, - string $key - ): ?\DateTimeImmutable { - return DatesImmutable::extractOrNull($data, $key); - } - - /** - * @param array $data - * @param string $key - * @param bool $getNullIfInvalid + * @param array|\ArrayAccess $data * @return \DateTimeImmutable */ final public static function extractOrNull( - array &$data, + array | \ArrayAccess $data, string $key, bool $getNullIfInvalid = false - ): ?\DateTimeImmutable { + ): \DateTimeImmutable | null { $dateTime = DateTimes::extractOrNull( $data, $key, diff --git a/src/Dates.php b/src/Dates.php index 1de773f..869d906 100644 --- a/src/Dates.php +++ b/src/Dates.php @@ -4,17 +4,13 @@ namespace SmartEmailing\Types; -use Nette\Utils\Arrays; +use SmartEmailing\Types\Helpers\ExtractableHelpers; abstract class Dates { - /** - * @param mixed $value - * @return \DateTime - */ final public static function from( - $value + mixed $value ): \DateTime { if ($value instanceof \DateTimeInterface) { $value = $value->format('Y-m-d'); @@ -33,15 +29,10 @@ final public static function from( ); } - /** - * @param mixed $value - * @param bool $getNullIfInvalid - * @return \DateTime - */ public static function fromOrNull( - $value, + mixed $value, bool $getNullIfInvalid = false - ): ?\DateTime { + ): \DateTime | null { if ($value === null) { return null; } @@ -58,16 +49,14 @@ public static function fromOrNull( } /** - * @param array $data - * @param string $key - * @return \DateTime + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extract( - array &$data, + array | \ArrayAccess $data, string $key ): \DateTime { - $value = Arrays::get($data, $key, ''); + $value = ExtractableHelpers::extractValue($data, $key); try { return self::from($value); @@ -77,16 +66,13 @@ final public static function extract( } /** - * @param array $data - * @param string $key - * @param bool $getNullIfInvalid - * @return \DateTime + * @param array|\ArrayAccess $data */ final public static function extractOrNull( - array &$data, + array | \ArrayAccess $data, string $key, bool $getNullIfInvalid = false - ): ?\DateTime { + ): \DateTime | null { if (!isset($data[$key])) { return null; } @@ -94,7 +80,7 @@ final public static function extractOrNull( if ($getNullIfInvalid) { try { return self::extract($data, $key); - } catch (InvalidTypeException $e) { + } catch (InvalidTypeException) { return null; } } diff --git a/src/DatesImmutable.php b/src/DatesImmutable.php index 9295cc6..d5f8abd 100644 --- a/src/DatesImmutable.php +++ b/src/DatesImmutable.php @@ -7,27 +7,18 @@ abstract class DatesImmutable { - /** - * @param mixed $value - * @return \DateTimeImmutable - */ final public static function from( - $value + mixed $value ): \DateTimeImmutable { $dateTime = Dates::from($value); return self::immutate($dateTime); } - /** - * @param mixed $value - * @param bool $getNullIfInvalid - * @return \DateTimeImmutable - */ public static function fromOrNull( - $value, + mixed $value, bool $getNullIfInvalid = false - ): ?\DateTimeImmutable { + ): \DateTimeImmutable | null { $dateTime = Dates::fromOrNull($value, $getNullIfInvalid); if ($dateTime === null) { @@ -38,13 +29,11 @@ public static function fromOrNull( } /** - * @param array $data - * @param string $key - * @return \DateTimeImmutable + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extract( - array &$data, + array | \ArrayAccess $data, string $key ): \DateTimeImmutable { $dateTime = Dates::extract( @@ -56,16 +45,13 @@ final public static function extract( } /** - * @param array $data - * @param string $key - * @param bool $getNullIfInvalid - * @return \DateTimeImmutable + * @param array|\ArrayAccess $data */ final public static function extractOrNull( - array &$data, + array | \ArrayAccess $data, string $key, bool $getNullIfInvalid = false - ): ?\DateTimeImmutable { + ): \DateTimeImmutable | null { $dateTime = Dates::extractOrNull( $data, $key, diff --git a/src/Domain.php b/src/Domain.php index 8777dd0..048e088 100644 --- a/src/Domain.php +++ b/src/Domain.php @@ -19,10 +19,7 @@ final class Domain implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value diff --git a/src/DomainName.php b/src/DomainName.php index 70f8af0..b379664 100644 --- a/src/DomainName.php +++ b/src/DomainName.php @@ -13,10 +13,7 @@ final class DomainName implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value diff --git a/src/Duration.php b/src/Duration.php index 11f9385..dbbfa9b 100644 --- a/src/Duration.php +++ b/src/Duration.php @@ -12,20 +12,11 @@ final class Duration implements ToStringInterface, ToArrayInterface use ExtractableTrait; - /** - * @var int - */ - private $value; + private int $value; - /** - * @var \SmartEmailing\Types\TimeUnit - */ - private $unit; + private TimeUnit $unit; - /** - * @var int - */ - private $lengthInSeconds; + private int $lengthInSeconds; /** * @param array $data @@ -42,12 +33,9 @@ private function __construct( $this->lengthInSeconds = (int) \abs($diff); } - /** - * @param mixed $data - * @return \SmartEmailing\Types\Duration - */ public static function from( - $data + mixed $data, + mixed ...$params ): Duration { if ($data instanceof self) { return $data; @@ -85,7 +73,7 @@ public static function fromDateTimeModify( $value *= -1; } - return new static( + return new Duration( [ 'value' => $value, 'unit' => $unit->getValue(), diff --git a/src/Emailaddress.php b/src/Emailaddress.php index 5f6e61a..3bd9b4e 100644 --- a/src/Emailaddress.php +++ b/src/Emailaddress.php @@ -16,20 +16,11 @@ final class Emailaddress implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; - /** - * @var string - */ - private $localPart; + private string $localPart; - /** - * @var \SmartEmailing\Types\HostName - */ - private $hostName; + private HostName $hostName; private function __construct( string $value @@ -37,7 +28,7 @@ private function __construct( { try { $ok = $this->initialize($value); - } catch (\Throwable $e) { + } catch (\Throwable) { $ok = false; } diff --git a/src/Enum.php b/src/Enum.php index 199f4ab..284170a 100644 --- a/src/Enum.php +++ b/src/Enum.php @@ -9,11 +9,8 @@ abstract class Enum extends \Consistence\Enum\Enum { - /** - * @param mixed $value - */ public static function checkValue( - $value + mixed $value ): void { try { diff --git a/src/ExtractableTraits/ArrayExtractableTrait.php b/src/ExtractableTraits/ArrayExtractableTrait.php index 02fd373..5cad97c 100644 --- a/src/ExtractableTraits/ArrayExtractableTrait.php +++ b/src/ExtractableTraits/ArrayExtractableTrait.php @@ -18,12 +18,9 @@ abstract public function __construct( array $data ); - /** - * @param string|mixed|array $data - * @return self - */ final public static function from( - $data + mixed $data, + mixed ...$params ): self { if ($data instanceof self) { return $data; diff --git a/src/ExtractableTraits/EnumExtractableTrait.php b/src/ExtractableTraits/EnumExtractableTrait.php index e68c853..5dc2a41 100644 --- a/src/ExtractableTraits/EnumExtractableTrait.php +++ b/src/ExtractableTraits/EnumExtractableTrait.php @@ -12,27 +12,25 @@ trait EnumExtractableTrait use ExtractableTrait; /** - * @param mixed $value - * @return \Consistence\Enum\Enum * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint */ abstract public static function get( - $value + mixed $value ): Enum; /** - * @param mixed $data * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint - * @return self + * @return static */ final public static function from( - $data - ): self { + mixed $data, + mixed ...$params + ): static { if ($data instanceof self) { return $data; } - return self::get($data); + return static::get($data); }/** @noinspection ReturnTypeCanBeDeclaredInspection */ } diff --git a/src/ExtractableTraits/ExtractableTrait.php b/src/ExtractableTraits/ExtractableTrait.php index a3d62a9..8d52102 100644 --- a/src/ExtractableTraits/ExtractableTrait.php +++ b/src/ExtractableTraits/ExtractableTrait.php @@ -12,25 +12,21 @@ trait ExtractableTrait { - /** - * @param string|mixed|array $data - * @return self - */ abstract public static function from( - $data + mixed $data, + mixed ...$params ): self; /** - * @param mixed|array $data - * @param string $key - * @return self + * @param array|\ArrayAccess $data + * @return static * @throws \SmartEmailing\Types\InvalidTypeException */ public static function extract( - $data, - string $key - ): self - { + array | \ArrayAccess $data, + string | int $key, + mixed ...$params + ): static { $value = ExtractableHelpers::extractValue($data, $key); if ($value instanceof self) { @@ -38,29 +34,27 @@ public static function extract( } try { - return self::from($value); + return self::from($value, ...$params); } catch (InvalidTypeException $e) { throw $e->wrap($key); } } /** - * @param mixed $value - * @param bool $getNullIfInvalid - * @return self|null * @throws \SmartEmailing\Types\InvalidTypeException */ public static function fromOrNull( - $value, - bool $getNullIfInvalid = false - ): ?self + mixed $value, + bool $getNullIfInvalid = false, + mixed ...$params + ): self | null { if ($value === null) { return null; } try { - return self::from($value); + return self::from($value, ...$params); } catch (InvalidTypeException $e) { if ($getNullIfInvalid) { return null; @@ -71,17 +65,15 @@ public static function fromOrNull( } /** - * @param mixed|array $data - * @param string $key - * @param bool $nullIfInvalid - * @return self|null + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ public static function extractOrNull( - $data, - string $key, - bool $nullIfInvalid = false - ): ?self + array | \ArrayAccess $data, + string | int $key, + bool $nullIfInvalid = false, + mixed ...$params + ): self | null { if (!\is_array($data)) { throw InvalidTypeException::typeError('array', $data); @@ -98,19 +90,19 @@ public static function extractOrNull( return self::tryToExtract( $data, $key, - $nullIfInvalid + $nullIfInvalid, + ...$params ); } /** - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array * @throws \SmartEmailing\Types\InvalidTypeException */ public static function extractArrayOf( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { $typedArray = Arrays::extractArray($data, $key); @@ -123,14 +115,13 @@ public static function extractArrayOf( } /** - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array * @throws \SmartEmailing\Types\InvalidTypeException */ public static function extractArrayOfOrEmpty( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { if (!isset($data[$key])) { @@ -180,7 +171,7 @@ public static function getArrayOfSkipInvalid( foreach ($array as $item) { try { $return[] = self::from($item); - } catch (InvalidTypeException $e) { + } catch (InvalidTypeException) { // exclude from result } } @@ -189,13 +180,12 @@ public static function getArrayOfSkipInvalid( } /** - * @param array $data - * @param string $key + * @param array|\ArrayAccess $data * @return array */ public static function extractArrayOfSkipInvalid( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): array { $typedArray = Arrays::extractArray($data, $key); @@ -204,20 +194,17 @@ public static function extractArrayOfSkipInvalid( } /** - * @param mixed|array $data - * @param string $key - * @param bool $nullIfInvalid - * @return self|null * @throws \SmartEmailing\Types\InvalidTypeException */ private static function tryToExtract( - $data, - string $key, - bool $nullIfInvalid - ): ?self + mixed $data, + string | int $key, + bool $nullIfInvalid, + mixed ...$params + ): self | null { try { - return self::extract($data, $key); + return self::extract($data, $key, ...$params); } catch (InvalidTypeException $e) { if ($nullIfInvalid) { return null; diff --git a/src/ExtractableTraits/FloatExtractableTrait.php b/src/ExtractableTraits/FloatExtractableTrait.php index 6730786..8a70779 100644 --- a/src/ExtractableTraits/FloatExtractableTrait.php +++ b/src/ExtractableTraits/FloatExtractableTrait.php @@ -15,12 +15,9 @@ abstract public function __construct( float $value ); - /** - * @param string|mixed|array $data - * @return self - */ final public static function from( - $data + mixed $data, + mixed ...$params ): self { if ($data instanceof self) { return $data; diff --git a/src/ExtractableTraits/IntExtractableTrait.php b/src/ExtractableTraits/IntExtractableTrait.php index 7f0b3d2..fdf7558 100644 --- a/src/ExtractableTraits/IntExtractableTrait.php +++ b/src/ExtractableTraits/IntExtractableTrait.php @@ -15,12 +15,9 @@ abstract public function __construct( int $value ); - /** - * @param string|mixed|array $data - * @return self - */ final public static function from( - $data + mixed $data, + mixed ...$params ): self { if ($data instanceof self) { return $data; diff --git a/src/ExtractableTraits/StringExtractableTrait.php b/src/ExtractableTraits/StringExtractableTrait.php index e14f5a6..0dc5f07 100644 --- a/src/ExtractableTraits/StringExtractableTrait.php +++ b/src/ExtractableTraits/StringExtractableTrait.php @@ -15,12 +15,9 @@ abstract public function __construct( string $value ); - /** - * @param string|mixed|array $data - * @return self - */ final public static function from( - $data + mixed $data, + mixed ...$params ): self { if ($data instanceof self) { return $data; @@ -28,7 +25,7 @@ final public static function from( $data = PrimitiveTypes::getString($data); - return new static($data); + return new static($data, ...$params); } } diff --git a/src/Guid.php b/src/Guid.php index d9f2850..8323cc1 100644 --- a/src/Guid.php +++ b/src/Guid.php @@ -12,10 +12,7 @@ final class Guid implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value diff --git a/src/Helpers/ArrayHelpers.php b/src/Helpers/ArrayHelpers.php index f13aadd..099f27e 100644 --- a/src/Helpers/ArrayHelpers.php +++ b/src/Helpers/ArrayHelpers.php @@ -17,9 +17,7 @@ abstract class ArrayHelpers final public static function collectionItemsToArray( array $arrayableCollection ): array { - $toArrayCallback = static function (ToArrayInterface $toArray) { - return $toArray->toArray(); - }; + $toArrayCallback = static fn (ToArrayInterface $toArray): array => $toArray->toArray(); return \array_map( $toArrayCallback, @@ -34,9 +32,7 @@ final public static function collectionItemsToArray( final public static function stringExtractableCollectionToArray( array $stringableCollection ): array { - $toArrayCallback = static function (ToStringInterface $toString) { - return (string) $toString; - }; + $toArrayCallback = static fn (ToStringInterface $toString): string => (string) $toString; return \array_map( $toArrayCallback, diff --git a/src/Helpers/CountryCodeToPhoneCodeTable.php b/src/Helpers/CountryCodeToPhoneCodeTable.php index 7d0c9e3..fd01c12 100644 --- a/src/Helpers/CountryCodeToPhoneCodeTable.php +++ b/src/Helpers/CountryCodeToPhoneCodeTable.php @@ -12,7 +12,7 @@ abstract class CountryCodeToPhoneCodeTable /** * @var array */ - public static $countryCodesToPhoneCodes = [ + public static array $countryCodesToPhoneCodes = [ CountryCode::AF => 93, CountryCode::AL => 355, CountryCode::DZ => 213, diff --git a/src/Helpers/ExtractableHelpers.php b/src/Helpers/ExtractableHelpers.php index b5b33b3..c53ffd0 100644 --- a/src/Helpers/ExtractableHelpers.php +++ b/src/Helpers/ExtractableHelpers.php @@ -10,16 +10,22 @@ abstract class ExtractableHelpers { /** - * @param mixed|array $data - * @param string $key - * @return mixed + * @param array|\ArrayAccess $data */ final public static function extractValue( - $data, - string $key - ) { + array | \ArrayAccess $data, + string | int $key + ): mixed { + if ($data instanceof \ArrayAccess) { + if (!$data->offsetExists($key)) { + throw InvalidTypeException::missingKey($key); + } + + return $data->offsetGet($key); + } + if (!\is_array($data)) { - throw InvalidTypeException::typeError('array', $data); + throw InvalidTypeException::typeError('array|\ArrayAccess', $data); } if (!\array_key_exists($key, $data)) { diff --git a/src/Helpers/InvisibleSpaceCharacterCodes.php b/src/Helpers/InvisibleSpaceCharacterCodes.php index 24dbeb4..21ee63e 100644 --- a/src/Helpers/InvisibleSpaceCharacterCodes.php +++ b/src/Helpers/InvisibleSpaceCharacterCodes.php @@ -10,7 +10,7 @@ abstract class InvisibleSpaceCharacterCodes /** * @var array */ - private static $codes = [ + private static array $codes = [ 0x9, 0xA, 0xB, diff --git a/src/Helpers/StringHelpers.php b/src/Helpers/StringHelpers.php index 1949e6f..23128be 100644 --- a/src/Helpers/StringHelpers.php +++ b/src/Helpers/StringHelpers.php @@ -41,8 +41,8 @@ final public static function sanitizeUtf8Mb4( } final public static function sanitizeUtf8Mb4OrNull( - ?string $string - ): ?string { + string | null $string + ): string | null { if ($string === null) { return null; } diff --git a/src/Helpers/UniqueToStringArray.php b/src/Helpers/UniqueToStringArray.php index 80da546..7d101a9 100644 --- a/src/Helpers/UniqueToStringArray.php +++ b/src/Helpers/UniqueToStringArray.php @@ -26,12 +26,9 @@ final class UniqueToStringArray implements \Countable, \IteratorAggregate /** * @var array<\SmartEmailing\Types\ToStringInterface> */ - private $objects; + private array $objects; - /** - * @var string - */ - private $type; + private string $type; /** * @param array<\SmartEmailing\Types\ToStringInterface> $data @@ -135,9 +132,9 @@ public function toArray(): array public function add( ToStringInterface $valueObject ): bool { - $type = \get_class($valueObject); + $type = $valueObject::class; - if (!$this->type) { + if (!isset($this->type)) { $this->type = $type; } diff --git a/src/Helpers/ValidationHelpers.php b/src/Helpers/ValidationHelpers.php index 44778c7..7fd10f1 100644 --- a/src/Helpers/ValidationHelpers.php +++ b/src/Helpers/ValidationHelpers.php @@ -9,8 +9,6 @@ abstract class ValidationHelpers /** * @param array $array - * @param string $typeName - * @return bool */ final public static function isTypedObjectArray( array $array, @@ -29,7 +27,6 @@ final public static function isTypedObjectArray( * Validates multidimensional array to have scalar or NULL leaves * * @param array $array - * @return bool */ final public static function isScalarLeavesArray( array $array diff --git a/src/Hex32.php b/src/Hex32.php index b03e3b1..2e8e0a7 100644 --- a/src/Hex32.php +++ b/src/Hex32.php @@ -13,10 +13,7 @@ final class Hex32 implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value diff --git a/src/HexColor.php b/src/HexColor.php index 5973020..d5d885f 100644 --- a/src/HexColor.php +++ b/src/HexColor.php @@ -13,10 +13,7 @@ final class HexColor implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; public function __construct( string $value diff --git a/src/HostName.php b/src/HostName.php index 6755541..a4b7138 100644 --- a/src/HostName.php +++ b/src/HostName.php @@ -13,10 +13,7 @@ final class HostName implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value diff --git a/src/Iban.php b/src/Iban.php index f003c98..4bff2dd 100644 --- a/src/Iban.php +++ b/src/Iban.php @@ -16,15 +16,9 @@ final class Iban implements ToStringInterface public const FORMAT_ELECTRONIC = 'electronic'; public const FORMAT_PRINT = 'print'; - /** - * @var \Iban\Validation\Iban - */ - private $iban; + private \Iban\Validation\Iban $iban; - /** - * @var string - */ - private $value; + private string $value; public function __construct( string $value @@ -47,7 +41,7 @@ public function getValue(): string public function getCountry(): CountryCode { - return CountryCode::from($this->iban->getCountryCode()); + return CountryCode::from($this->iban->countryCode()); } public function getFormatted( @@ -59,7 +53,7 @@ public function getFormatted( public function getChecksum(): int { - return (int) $this->iban->getChecksum(); + return (int) $this->iban->checksum(); } } diff --git a/src/InvalidTypeException.php b/src/InvalidTypeException.php index 628ac73..68f1cd6 100644 --- a/src/InvalidTypeException.php +++ b/src/InvalidTypeException.php @@ -12,20 +12,18 @@ class InvalidTypeException extends \RuntimeException final public function __construct( string $message = '', int $code = 0, - ?\Throwable $previous = null + \Throwable | null $previous = null ) { parent::__construct($message, $code, $previous); } /** - * @param string $expected - * @param mixed|array $value * @return \SmartEmailing\Types\InvalidTypeException */ public static function typeError( string $expected, - $value + mixed $value ): self { $type = self::getType($value); $description = self::getDescription($value); @@ -41,12 +39,11 @@ public static function typeError( /** * @param array $expected - * @param mixed|array $value * @return \SmartEmailing\Types\InvalidTypeException */ public static function typesError( array $expected, - $value + mixed $value ): self { $type = self::getType($value); $description = self::getDescription($value); @@ -61,19 +58,19 @@ public static function typesError( } public static function missingKey( - string $key + string | int $key ): self { return new static('Missing key: ' . $key); } public static function cannotBeEmptyError( - string $key + string | int $key ): self { return new static('Array at key ' . $key . ' must not be empty.'); } public function wrap( - string $key + string | int $key ): self { $message = 'Problem at key ' . $key @@ -83,12 +80,8 @@ public function wrap( return new static($message); } - /** - * @param mixed $value - * @return string - */ private static function getType( - $value + mixed $value ): string { $type = \gettype($value); @@ -100,12 +93,8 @@ private static function getType( return $type; } - /** - * @param mixed $value - * @return string - */ private static function getDescription( - $value + mixed $value ): string { $description = ''; @@ -115,7 +104,7 @@ private static function getDescription( $stringValue = StringHelpers::sanitize($stringValue); $description = ' (' . $stringValue . ')'; } elseif (\is_object($value)) { - $description = ' (' . \get_class($value) . ')'; + $description = ' (' . $value::class . ')'; } return $description; diff --git a/src/IpAddress.php b/src/IpAddress.php index d2c2eb0..7d604b1 100644 --- a/src/IpAddress.php +++ b/src/IpAddress.php @@ -13,15 +13,9 @@ final class IpAddress implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; - - /** - * @var int - */ - private $version; + private string $value; + + private int $version; private function __construct( string $value diff --git a/src/JsonString.php b/src/JsonString.php index 1e3364d..7776bdd 100644 --- a/src/JsonString.php +++ b/src/JsonString.php @@ -14,10 +14,7 @@ final class JsonString implements ToStringInterface use ExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value @@ -31,12 +28,12 @@ private function __construct( } /** - * @param string|mixed|array $data * @return self * @throws \SmartEmailing\Types\InvalidTypeException */ public static function from( - $data + mixed $data, + mixed ...$params ): JsonString { if ($data instanceof self) { @@ -59,12 +56,10 @@ public static function from( } /** - * @param mixed $value - * @param bool $oneLine * @return \SmartEmailing\Types\JsonString */ public static function encode( - $value, + mixed $value, bool $oneLine = false ): self { @@ -85,10 +80,7 @@ public function getValue(): string return $this->value; } - /** - * @return mixed|array - */ - public function getDecodedValue() + public function getDecodedValue(): mixed { return Json::decode($this->value, Json::FORCE_ARRAY); } @@ -101,7 +93,7 @@ private function isValid( Json::decode($value); return true; - } catch (JsonException $e) { + } catch (JsonException) { return false; } } diff --git a/src/KeyValuePair.php b/src/KeyValuePair.php index 1c615d0..f955f8f 100644 --- a/src/KeyValuePair.php +++ b/src/KeyValuePair.php @@ -11,15 +11,9 @@ final class KeyValuePair implements ToArrayInterface use ArrayExtractableTrait; - /** - * @var string - */ - private $key; + private string $key; - /** - * @var string - */ - private $value; + private string $value; /** * @param array $data diff --git a/src/LoginCredentials.php b/src/LoginCredentials.php index d2e2a86..422b7d1 100644 --- a/src/LoginCredentials.php +++ b/src/LoginCredentials.php @@ -11,15 +11,9 @@ final class LoginCredentials use ArrayExtractableTrait; - /** - * @var string - */ - private $login; + private string $login; - /** - * @var string - */ - private $password; + private string $password; /** * @param array $data diff --git a/src/NonEmptyString.php b/src/NonEmptyString.php index ca877bb..c4f819c 100644 --- a/src/NonEmptyString.php +++ b/src/NonEmptyString.php @@ -13,10 +13,7 @@ final class NonEmptyString implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; public function __construct( string $value diff --git a/src/Part.php b/src/Part.php index b3727a9..8665cbd 100644 --- a/src/Part.php +++ b/src/Part.php @@ -12,10 +12,7 @@ final class Part implements ToStringInterface use FloatExtractableTrait; use ToStringTrait; - /** - * @var float - */ - private $value; + private float $value; public function __construct( float $value diff --git a/src/PhoneNumber.php b/src/PhoneNumber.php index dde1f33..32591bb 100644 --- a/src/PhoneNumber.php +++ b/src/PhoneNumber.php @@ -14,10 +14,7 @@ final class PhoneNumber implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; private function __construct( string $value @@ -32,24 +29,13 @@ private function __construct( $this->value = $preprocessed; } - /** - * @return \SmartEmailing\Types\CountryCode - * @deprecated use PhoneNumber::guessCountry() - */ - public function getCountry(): ?CountryCode - { - return $this->guessCountry(); - } - public function guessCountry(): ?CountryCode { $input = CountryCodeToPhoneCodeTable::$countryCodesToPhoneCodes; \uasort( $input, - static function (int $a, int $b) { - return Strings::length((string) $b) <=> Strings::length((string) $a); - } + static fn (int $a, int $b): int => Strings::length((string) $b) <=> Strings::length((string) $a) ); $justNumbers = Strings::replace( @@ -100,7 +86,7 @@ public static function preprocess( private function initialize( string $value - ): ?string + ): string | null { $value = self::preprocess($value); diff --git a/src/Port.php b/src/Port.php index cc762e9..6478afa 100644 --- a/src/Port.php +++ b/src/Port.php @@ -12,10 +12,7 @@ final class Port implements ToStringInterface use IntExtractableTrait; use ToStringTrait; - /** - * @var int - */ - private $value; + private int $value; public function __construct( int $value diff --git a/src/Price.php b/src/Price.php index 6183ea7..292a375 100644 --- a/src/Price.php +++ b/src/Price.php @@ -11,20 +11,11 @@ final class Price use ArrayExtractableTrait; - /** - * @var float - */ - private $withoutVat; + private float $withoutVat; - /** - * @var float - */ - private $withVat; + private float $withVat; - /** - * @var \SmartEmailing\Types\CurrencyCode - */ - private $currency; + private CurrencyCode $currency; /** * @param array $data diff --git a/src/PrimitiveTypes.php b/src/PrimitiveTypes.php index 29e3388..57abe7f 100644 --- a/src/PrimitiveTypes.php +++ b/src/PrimitiveTypes.php @@ -11,12 +11,8 @@ abstract class PrimitiveTypes { - /** - * @param mixed $value - * @return int - */ final public static function getInt( - $value + mixed $value ): int { if (Validators::isNumericInt($value)) { return (int) $value; @@ -25,15 +21,10 @@ final public static function getInt( throw InvalidTypeException::typeError('int', $value); } - /** - * @param mixed $value - * @param bool $nullIfInvalid - * @return int|null - */ final public static function getIntOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?int { + ): int | null { if ($value === null) { return null; } @@ -50,14 +41,12 @@ final public static function getIntOrNull( } /** - * @param array $data - * @param string $key - * @return int + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractInt( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): int { $value = ExtractableHelpers::extractValue($data, $key); @@ -68,12 +57,8 @@ final public static function extractInt( } } - /** - * @param mixed $value - * @return float - */ final public static function getFloat( - $value + mixed $value ): float { if (\is_string($value)) { $value = \strtr( @@ -91,15 +76,10 @@ final public static function getFloat( throw InvalidTypeException::typeError('float', $value); } - /** - * @param mixed $value - * @param bool $nullIfInvalid - * @return float|null - */ final public static function getFloatOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?float { + ): float | null { if ($value === null) { return null; } @@ -116,13 +96,11 @@ final public static function getFloatOrNull( } /** - * @param array $data - * @param string $key - * @return float + * @param array|\ArrayAccess $data */ final public static function extractFloat( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): float { $value = ExtractableHelpers::extractValue($data, $key); @@ -134,17 +112,14 @@ final public static function extractFloat( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid - * @return float + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractFloatOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?float { + ): float | null { if (!isset($data[$key])) { return null; } @@ -161,17 +136,14 @@ final public static function extractFloatOrNull( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid - * @return int + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractIntOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?int { + ): int | null { if (!isset($data[$key])) { return null; } @@ -188,15 +160,12 @@ final public static function extractIntOrNull( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid - * @return bool|null + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractBoolOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false ): ?bool { if (!isset($data[$key])) { @@ -215,17 +184,14 @@ final public static function extractBoolOrNull( } /** - * @param array $data - * @param string $key - * @param bool $nullIfInvalid - * @return string|null + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractStringOrNull( - array $data, - string $key, + array | \ArrayAccess $data, + string | int $key, bool $nullIfInvalid = false - ): ?string { + ): string | null { if ( !isset($data[$key]) || $data[$key] === '' @@ -244,12 +210,8 @@ final public static function extractStringOrNull( } } - /** - * @param mixed $value - * @return string - */ final public static function getString( - $value + mixed $value ): string { if (\is_scalar($value)) { return (string) $value; @@ -258,15 +220,10 @@ final public static function getString( throw InvalidTypeException::typeError('string', $value); } - /** - * @param mixed $value - * @param bool $nullIfInvalid - * @return string|null - */ final public static function getStringOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?string { + ): string | null { if ($value === null) { return null; } @@ -283,14 +240,12 @@ final public static function getStringOrNull( } /** - * @param array $data - * @param string $key - * @return string + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractString( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): string { $value = ExtractableHelpers::extractValue($data, $key); @@ -301,12 +256,8 @@ final public static function extractString( } } - /** - * @param mixed $value - * @return bool - */ final public static function getBool( - $value + mixed $value ): bool { if (\is_bool($value)) { return $value; @@ -327,15 +278,10 @@ final public static function getBool( throw InvalidTypeException::typeError('bool', $value); } - /** - * @param mixed $value - * @param bool $nullIfInvalid - * @return bool|null - */ final public static function getBoolOrNull( - $value, + mixed $value, bool $nullIfInvalid = false - ): ?bool { + ): bool | null { if ($value === null) { return null; } @@ -352,25 +298,12 @@ final public static function getBoolOrNull( } /** - * @param mixed $value - * @return array - * @deprecated use Arrays::getArray instead - */ - final public static function getArray( - $value - ): array { - return Arrays::getArray($value); - } - - /** - * @param array $data - * @param string $key - * @return bool + * @param array|\ArrayAccess $data * @throws \SmartEmailing\Types\InvalidTypeException */ final public static function extractBool( - array $data, - string $key + array | \ArrayAccess $data, + string | int $key ): bool { $value = ExtractableHelpers::extractValue($data, $key); @@ -381,78 +314,4 @@ final public static function extractBool( } } - /** - * @param array $data - * @param string $key - * @return array - * @throws \SmartEmailing\Types\InvalidTypeException - * @deprecated use Arrays::extractStringArray - */ - final public static function extractStringArray( - array $data, - string $key - ): array { - return Arrays::extractStringArray( - $data, - $key - ); - } - - /** - * @param array $data - * @param string $key - * @return array - * @throws \SmartEmailing\Types\InvalidTypeException - * @deprecated use Arrays::extractIntArray - */ - final public static function extractIntArray( - array $data, - string $key - ): array { - return Arrays::extractIntArray( - $data, - $key - ); - } - - /** - * Preserves keys - * - * @param array $data - * @param string $key - * @return array - * @throws \SmartEmailing\Types\InvalidTypeException - * @deprecated use Arrays::extractArray - */ - final public static function extractArray( - array &$data, - string $key - ): array - { - return Arrays::extractArray( - $data, - $key - ); - } - - /** - * Preserves keys - * - * @param array $data - * @param string $key - * @return array|null - * @throws \SmartEmailing\Types\InvalidTypeException - * @deprecated use Arrays::extractArrayOrNull - */ - final public static function extractArrayOrNull( - array &$data, - string $key - ): ?array - { - return Arrays::extractArrayOrNull( - $data, - $key - ); - } - } diff --git a/src/Quantity.php b/src/Quantity.php index d6446e6..cef53fc 100644 --- a/src/Quantity.php +++ b/src/Quantity.php @@ -12,10 +12,7 @@ final class Quantity implements ToStringInterface use IntExtractableTrait; use ToStringTrait; - /** - * @var int - */ - private $value; + private int $value; public function __construct( int $value diff --git a/src/ReLUValue.php b/src/ReLUValue.php index 922a089..df6a4dd 100644 --- a/src/ReLUValue.php +++ b/src/ReLUValue.php @@ -12,10 +12,7 @@ final class ReLUValue implements ToStringInterface use FloatExtractableTrait; use ToStringTrait; - /** - * @var float - */ - private $value; + private float $value; public function __construct( float $value diff --git a/src/ScalarLeavesArray.php b/src/ScalarLeavesArray.php index 30fb444..c0c9e7f 100644 --- a/src/ScalarLeavesArray.php +++ b/src/ScalarLeavesArray.php @@ -15,7 +15,7 @@ final class ScalarLeavesArray implements ToArrayInterface /** * @var array */ - private $data; + private array $data; /** * @param array $data @@ -32,8 +32,6 @@ public function __construct( /** * @param array $data - * @param string $key - * @return self */ public static function extractOrEmpty( array $data, diff --git a/src/SigmoidValue.php b/src/SigmoidValue.php index de332fd..d28f911 100644 --- a/src/SigmoidValue.php +++ b/src/SigmoidValue.php @@ -12,10 +12,7 @@ final class SigmoidValue implements ToStringInterface use FloatExtractableTrait; use ToStringTrait; - /** - * @var float - */ - private $value; + private float $value; public function __construct( float $value diff --git a/src/SwiftBic.php b/src/SwiftBic.php index 5102c97..6891e86 100644 --- a/src/SwiftBic.php +++ b/src/SwiftBic.php @@ -13,10 +13,7 @@ final class SwiftBic implements ToStringInterface use ToStringTrait; use StringExtractableTrait; - /** - * @var string - */ - private $value; + private string $value; public function __construct( string $value diff --git a/src/ToStringInterface.php b/src/ToStringInterface.php index 673c25b..538121c 100644 --- a/src/ToStringInterface.php +++ b/src/ToStringInterface.php @@ -4,7 +4,9 @@ namespace SmartEmailing\Types; -interface ToStringInterface +use Stringable; + +interface ToStringInterface extends Stringable { public function __toString(): string; diff --git a/src/UniqueArrayFeatures.php b/src/UniqueArrayFeatures.php index cd8dce6..cb490f3 100644 --- a/src/UniqueArrayFeatures.php +++ b/src/UniqueArrayFeatures.php @@ -14,8 +14,6 @@ final public static function empty(): self /** * @param array $data - * @param string $key - * @return self */ public static function extractOrEmpty( array $data, @@ -34,7 +32,6 @@ public static function extractOrEmpty( } /** - * @param int $chunkSize * @return array */ public function split( diff --git a/src/UniqueIntArray.php b/src/UniqueIntArray.php index 10f873a..49a9a12 100644 --- a/src/UniqueIntArray.php +++ b/src/UniqueIntArray.php @@ -18,7 +18,7 @@ final class UniqueIntArray implements \Countable, \IteratorAggregate, ToArrayInt /** * @var array */ - private $valuesPresenceMap; + private array $valuesPresenceMap; /** * @param array $data @@ -32,7 +32,7 @@ private function __construct( foreach ($data as $value) { try { $this->add(PrimitiveTypes::getInt($value)); - } catch (InvalidTypeException $e) { + } catch (InvalidTypeException) { throw InvalidTypeException::typeError('all members of array to be int', $value); } } @@ -40,7 +40,6 @@ private function __construct( /** * @param array $data - * @param string $key * @return \SmartEmailing\Types\UniqueIntArray */ public static function extractNotEmpty( @@ -61,7 +60,6 @@ public static function extractNotEmpty( /** * @param array<\SmartEmailing\Types\UniqueIntArray> $uniqueIntArrays - * @return \SmartEmailing\Types\UniqueIntArray */ public static function intersect( array $uniqueIntArrays @@ -88,7 +86,6 @@ public static function intersect( /** * @param array<\SmartEmailing\Types\UniqueIntArray> $uniqueIntArrays - * @return \SmartEmailing\Types\UniqueIntArray */ public static function union( array $uniqueIntArrays diff --git a/src/UniqueStringArray.php b/src/UniqueStringArray.php index 42ca95f..f8812da 100644 --- a/src/UniqueStringArray.php +++ b/src/UniqueStringArray.php @@ -18,7 +18,7 @@ final class UniqueStringArray implements \Countable, \IteratorAggregate, ToArray /** * @var array */ - private $valuesPresenceMap; + private array $valuesPresenceMap; /** * @param array $data @@ -32,7 +32,7 @@ private function __construct( foreach ($data as $value) { try { $this->add(PrimitiveTypes::getString($value)); - } catch (InvalidTypeException $e) { + } catch (InvalidTypeException) { throw InvalidTypeException::typeError('all members of array to be string', $value); } } @@ -71,14 +71,6 @@ public function toArray(): array return $this->getValues(); } - /** - * @deprecated This method does nothing because array is already unique - */ - public function removeDuplicities(): void - { - $this->valuesPresenceMap = \array_unique($this->valuesPresenceMap); - } - public function add( string $id ): bool { diff --git a/src/UnsignedFloat.php b/src/UnsignedFloat.php index f7e6a8f..870c171 100644 --- a/src/UnsignedFloat.php +++ b/src/UnsignedFloat.php @@ -12,10 +12,7 @@ final class UnsignedFloat implements ToStringInterface use FloatExtractableTrait; use ToStringTrait; - /** - * @var float - */ - private $value; + private float $value; public function __construct( float $value diff --git a/src/UnsignedInt.php b/src/UnsignedInt.php index 76068d4..4a089cb 100644 --- a/src/UnsignedInt.php +++ b/src/UnsignedInt.php @@ -12,10 +12,7 @@ final class UnsignedInt implements ToStringInterface use IntExtractableTrait; use ToStringTrait; - /** - * @var int - */ - private $value; + private int $value; public function __construct( int $value diff --git a/src/UrlType.php b/src/UrlType.php index 8f6787e..d9ae81b 100644 --- a/src/UrlType.php +++ b/src/UrlType.php @@ -16,10 +16,7 @@ final class UrlType implements ToStringInterface use StringExtractableTrait; use ToStringTrait; - /** - * @var \Nette\Http\Url - */ - private $url; + private Url $url; private function __construct( string $value @@ -36,9 +33,7 @@ private function __construct( // urlencode non-ascii chars $value = (string) \preg_replace_callback( '/[^\x20-\x7f]/', - static function ($match) { - return \urlencode($match[0]); - }, + static fn ($match): string => \urlencode($match[0]), $value ); @@ -51,7 +46,7 @@ static function ($match) { try { $this->url = new Url($value); - } catch (InvalidArgumentException $e) { + } catch (InvalidArgumentException) { throw new InvalidTypeException('Invalid URL or missing protocol: ' . $value); } } @@ -81,32 +76,11 @@ public function getAbsoluteUrl(): string return $this->getValue(); } - /** - * @param string $name - * @return string|null - * @deprecated use getQueryParameter instead - */ - public function getParameter( - string $name - ): ?string - { - return $this->url->getQueryParameter($name) ?? null; - } - public function getBaseUrl(): string { return $this->url->getBaseUrl(); } - /** - * @return string - * @deprecated use getValue or (string) type cast - */ - public function toString(): string - { - return (string) $this->url; - } - public function getScheme(): string { return $this->url->getScheme(); @@ -114,7 +88,6 @@ public function getScheme(): string /** * @param array $names - * @return bool */ public function hasParameters( array $names @@ -140,13 +113,11 @@ public function getParameters(): array } /** - * @param string $name - * @param mixed|null $value * @return \SmartEmailing\Types\UrlType */ public function withQueryParameter( string $name, - $value + mixed $value ): self { $dolly = clone $this; @@ -159,7 +130,6 @@ public function withQueryParameter( } /** - * @param \SmartEmailing\Types\Domain $host * @return $this * @deprecated use withHostName */ @@ -211,15 +181,10 @@ public function withPath( return $dolly; } - /** - * @param string $name - * @param mixed|null $default - * @return mixed - */ public function getQueryParameter( string $name, - $default = null - ) + mixed $default = null + ): mixed { return $this->url->getQueryParameter($name) ?? $default; } @@ -231,7 +196,7 @@ public function getValue(): string private function addSlashToPathOrFail( string $value - ): ?string + ): string | null { $urlParts = \parse_url($value); @@ -244,7 +209,6 @@ private function addSlashToPathOrFail( /** * @param array $urlParts - * @return string */ private function buildUrl( array $urlParts diff --git a/src/VatId.php b/src/VatId.php index 31c2a9c..87d55c2 100644 --- a/src/VatId.php +++ b/src/VatId.php @@ -15,25 +15,16 @@ final class VatId implements ToStringInterface use ToStringTrait; use StringExtractableTrait; - /** - * @var \SmartEmailing\Types\CountryCode|null - */ - private $country; + private CountryCode | null $country; - /** - * @var string|null - */ - private $prefix; + private string | null $prefix; - /** - * @var string - */ - private $vatNumber; + private string $vatNumber; /** * @var array */ - private static $patternsByCountry = [ + private static array $patternsByCountry = [ CountryCode::AT => 'ATU\d{8}', CountryCode::BE => 'BE[0-1]\d{9}', CountryCode::BG => 'BG\d{9,10}', @@ -77,12 +68,12 @@ private function __construct( } } - public function getCountry(): ?CountryCode + public function getCountry(): CountryCode | null { return $this->country; } - public function getPrefix(): ?string + public function getPrefix(): string | null { return $this->prefix; } @@ -122,15 +113,15 @@ private static function parseCountryOrNull( try { return CountryCode::from($countryCode); - } catch (InvalidTypeException $e) { + } catch (InvalidTypeException) { return null; } } private static function parsePrefixOrNull( - ?CountryCode $country, + CountryCode | null $country, string $vatId - ): ?string + ): string | null { if (!$country) { return null; @@ -140,7 +131,7 @@ private static function parsePrefixOrNull( } private static function parseVatNumber( - ?CountryCode $country, + CountryCode | null $country, string $vatId ): string { @@ -150,8 +141,8 @@ private static function parseVatNumber( } private static function validate( - ?CountryCode $country, - ?string $prefix, + CountryCode | null $country, + string | null $prefix, string $vatNumber ): bool { @@ -164,7 +155,7 @@ private static function validate( private static function isValidForCountry( CountryCode $country, - ?string $prefix, + string | null $prefix, string $vatNumber ): bool { @@ -196,8 +187,6 @@ private static function getDivisible(): array } /** - * @param string $vatNumber - * @return bool * @phpcsSuppress SlevomatCodingStandard.Functions.UnusedParameter.UnusedParameter */ private static function isValidForNonCountry( @@ -218,7 +207,6 @@ private static function preProcessVatId( } /** - * @param string $vatId * @return array */ private static function extractCountryAndPrefixAndNumber( diff --git a/src/ZipCode.php b/src/ZipCode.php index b2e1c6a..652ed73 100644 --- a/src/ZipCode.php +++ b/src/ZipCode.php @@ -12,18 +12,17 @@ final class ZipCode implements ToStringInterface { - use StringExtractableTrait; + use StringExtractableTrait { + extract as traitExtract; + } use ToStringTrait; - /** - * @var string - */ - private $value; + private string $value; /** - * @var array + * @var array */ - private static $patternsByCountry = [ + private static array $patternsByCountry = [ CountryCode::CZ => '#^\d{3}?\d{2}\z#', CountryCode::SK => '#^\d{3}?\d{2}\z#', CountryCode::AT => '#^\d{4}\z#', @@ -48,28 +47,53 @@ final class ZipCode implements ToStringInterface ]; private function __construct( - string $value + string $value, + ?CountryCode $countryCode = null ) { $value = StringHelpers::removeWhitespace($value); $value = Strings::upper($value); - if (!$this->isValid($value)) { + if (!$this->isValid($value, $countryCode)) { throw new InvalidTypeException('Invalid ZIP code: ' . $value); } $this->value = $value; } + /** + * @param \ArrayAccess|array $data + * @param int|string $key + * @param \SmartEmailing\Types\CountryCode|null $countryCode + * @return self + */ + public static function extract( + \ArrayAccess|array $data, + int|string $key, + ?CountryCode $countryCode = null + ): self + { + return self::traitextract($data, $key, $countryCode); + } + public function getValue(): string { return $this->value; } private function isValid( - string $value + string $value, + ?CountryCode $countryCode ): bool { + if ($countryCode !== null) { + $pattern = self::$patternsByCountry[(string) $countryCode] ?? null; + + if ($pattern !== null) { + return $this->validate($value, $pattern); + } + } + foreach (self::$patternsByCountry as $pattern) { - if (Strings::match($value, $pattern)) { + if ($this->validate($value, $pattern)) { return true; } } @@ -77,4 +101,12 @@ private function isValid( return false; } + private function validate( + string $value, + string $pattern + ): bool + { + return Strings::match($value, $pattern) !== null; + } + } diff --git a/tests/ArraysTest.phpt b/tests/ArraysTest.phpt index 530ddf1..42df4f0 100644 --- a/tests/ArraysTest.phpt +++ b/tests/ArraysTest.phpt @@ -4,9 +4,9 @@ declare(strict_types = 1); namespace SmartEmailing\Types; +use stdClass; use Tester\Assert; use Tester\TestCase; -use stdClass; require_once __DIR__ . '/bootstrap.php'; diff --git a/tests/DateTimesImmutableTest.phpt b/tests/DateTimesImmutableTest.phpt index 9d75e5c..5f5f900 100644 --- a/tests/DateTimesImmutableTest.phpt +++ b/tests/DateTimesImmutableTest.phpt @@ -45,28 +45,6 @@ final class DateTimesImmutableTest extends TestCase Assert::type(\DateTimeImmutable::class, $d); } - public function testExtractDate(): void - { - $data = [ - 'b' => '2000-01-01', - ]; - $d = DateTimesImmutable::extractDate($data, 'b'); - Assert::type(\DateTimeImmutable::class, $d); - } - - public function testExtractDateOrNull(): void - { - $data = [ - 'b' => '2000-01-01', - ]; - - $d = DateTimesImmutable::extractDateOrNull($data, 'not-a-key'); - Assert::null($d); - - $d = DateTimesImmutable::extractDateOrNull($data, 'b'); - Assert::type(\DateTimeImmutable::class, $d); - } - public function testExtractOrNull(): void { $data = [ diff --git a/tests/DateTimesTest.phpt b/tests/DateTimesTest.phpt index 37ad367..3e2de29 100644 --- a/tests/DateTimesTest.phpt +++ b/tests/DateTimesTest.phpt @@ -81,51 +81,6 @@ final class DateTimesTest extends TestCase Assert::type(\DateTime::class, $d); } - public function testExtractDate(): void - { - $d = DateTimes::from('2010-01-01 10:00:00'); - $data = [ - 'a' => 'xx', - 'b' => '2000-01-01', - 'c' => $d, - ]; - - Assert::throws( - static function () use ($data): void { - DateTimes::extractDate($data, 'a'); - }, - InvalidTypeException::class - ); - - Assert::throws( - static function () use ($data): void { - DateTimes::extractDate($data, 'not-key'); - }, - InvalidTypeException::class - ); - - $d = DateTimes::extractDate($data, 'b'); - Assert::type(\DateTime::class, $d); - Assert::equal('2000-01-01 00:00:00', DateTimeFormatter::format($d)); - - $d = DateTimes::extractDate($data, 'c'); - Assert::type(\DateTime::class, $d); - Assert::equal('2010-01-01 00:00:00', DateTimeFormatter::format($d)); - } - - public function testExtractDateOrNull(): void - { - $data = [ - 'b' => '2000-01-01', - ]; - - $d = DateTimes::extractDateOrNull($data, 'not-a-key'); - Assert::null($d); - - $d = DateTimes::extractDateOrNull($data, 'b'); - Assert::type(\DateTime::class, $d); - } - public function testExtractOrNull(): void { $d = DateTimes::from('2010-01-01 10:00:00'); diff --git a/tests/DurationTest.phpt b/tests/DurationTest.phpt index 3907e75..0606ec6 100644 --- a/tests/DurationTest.phpt +++ b/tests/DurationTest.phpt @@ -4,9 +4,9 @@ declare(strict_types = 1); namespace SmartEmailing\Types; +use stdClass; use Tester\Assert; use Tester\TestCase; -use stdClass; require_once __DIR__ . '/bootstrap.php'; @@ -24,10 +24,12 @@ final class DurationTest extends TestCase Assert::throws( static function (): void { - Duration::from([ - 'value' => 0, - 'unit' => 'week', - ]); + Duration::from( + [ + 'value' => 0, + 'unit' => 'week', + ] + ); }, InvalidTypeException::class ); @@ -66,10 +68,12 @@ final class DurationTest extends TestCase public function testCreate(): void { - Duration::from([ - 'value' => 1, - 'unit' => TimeUnit::HOURS, - ]); + Duration::from( + [ + 'value' => 1, + 'unit' => TimeUnit::HOURS, + ] + ); Duration::extract([ 'duration' => [ @@ -87,10 +91,12 @@ final class DurationTest extends TestCase public function testGetUnit(): void { - $duration = Duration::from([ - 'value' => 1, - 'unit' => TimeUnit::YEARS, - ]); + $duration = Duration::from( + [ + 'value' => 1, + 'unit' => TimeUnit::YEARS, + ] + ); Assert::type(Duration::class, $duration); Assert::type(TimeUnit::class, $duration->getUnit()); @@ -98,10 +104,12 @@ final class DurationTest extends TestCase public function testGetValue(): void { - $duration = Duration::from([ - 'value' => 1, - 'unit' => TimeUnit::DAYS, - ]); + $duration = Duration::from( + [ + 'value' => 1, + 'unit' => TimeUnit::DAYS, + ] + ); Assert::type(Duration::class, $duration); Assert::type('int', $duration->getValue()); @@ -110,17 +118,21 @@ final class DurationTest extends TestCase public function testLengthInSeconds(): void { - $duration = Duration::from([ - 'value' => 3, - 'unit' => TimeUnit::HOURS, - ]); + $duration = Duration::from( + [ + 'value' => 3, + 'unit' => TimeUnit::HOURS, + ] + ); Assert::type(Duration::class, $duration); Assert::equal(10800, $duration->getLengthInSeconds()); - $duration = Duration::from([ - 'value' => 10, - 'unit' => TimeUnit::MINUTES, - ]); + $duration = Duration::from( + [ + 'value' => 10, + 'unit' => TimeUnit::MINUTES, + ] + ); Assert::type(Duration::class, $duration); Assert::equal(600, $duration->getLengthInSeconds()); } @@ -128,9 +140,9 @@ final class DurationTest extends TestCase public function testGetDateTimeModify(): void { foreach ($this->getTestDateTimeModifyData() as $data) { - /** @var \SmartEmailing\Types\Duration $duration */ - /** @var string $expectedDateTimeModify */ [$duration, $expectedDateTimeModify] = $data; + \assert($duration instanceof \SmartEmailing\Types\Duration); + \assert(\is_string($expectedDateTimeModify)); Assert::equal($expectedDateTimeModify, $duration->getDateTimeModify()); } @@ -139,9 +151,9 @@ final class DurationTest extends TestCase public function testToArray(): void { foreach ($this->getTestToArrayData() as $data) { - /** @var \SmartEmailing\Types\Duration $duration */ - /** @var array $expectedArray */ [$duration, $expectedArray] = $data; + \assert($duration instanceof \SmartEmailing\Types\Duration); + \assert(\is_array($expectedArray)); Assert::equal($expectedArray, $duration->toArray()); } diff --git a/tests/ExtractableTraitTest.phpt b/tests/ExtractableTraitTest.phpt index 13875ff..0a9f937 100644 --- a/tests/ExtractableTraitTest.phpt +++ b/tests/ExtractableTraitTest.phpt @@ -17,12 +17,13 @@ final class ExtractableTraitTest extends TestCase public function testExtract(): void { - Assert::throws( - static function (): void { - Emailaddress::extract('aa', 'x'); - }, - InvalidTypeException::class - ); + // todo temove + // Assert::throws( + // static function (): void { + // Emailaddress::extract('aa', 'x'); + // }, + // InvalidTypeException::class + // ); $emailaddress1 = Emailaddress::from('martin+1@smartemailing.cz'); $data = [ @@ -135,13 +136,14 @@ final class ExtractableTraitTest extends TestCase public function testExtractOrNull(): void { - Assert::throws( - static function (): void { - $data = 'a'; - Emailaddress::extractOrNull($data, 'a'); - }, - InvalidTypeException::class - ); + // todo remove + // Assert::throws( + // static function (): void { + // $data = 'a'; + // Emailaddress::extractOrNull($data, 'a'); + // }, + // InvalidTypeException::class + // ); $data = [ 'a' => '', diff --git a/tests/Helpers/ExtractableHelpersTest.phpt b/tests/Helpers/ExtractableHelpersTest.phpt new file mode 100644 index 0000000..1bf8c27 --- /dev/null +++ b/tests/Helpers/ExtractableHelpersTest.phpt @@ -0,0 +1,61 @@ + 'a', 1 => 1]; + Assert::same('a', ExtractableHelpers::extractValue($array, 'a')); + + Assert::same(1, ExtractableHelpers::extractValue($array, 1)); + + Assert::exception( + static fn () => ExtractableHelpers::extractValue($array, 'b'), + InvalidTypeException::class, + 'Missing key: b' + ); + + Assert::exception( + static fn () => ExtractableHelpers::extractValue($array, '2'), + InvalidTypeException::class, + 'Missing key: 2' + ); + } + + public function testExtractValueFromArrayAccess(): void + { + $arrayObject = new ArrayObject(['a' => 'a', 1 => 1]); + + Assert::same('a', ExtractableHelpers::extractValue($arrayObject, 'a')); + + Assert::same(1, ExtractableHelpers::extractValue($arrayObject, 1)); + + Assert::exception( + static fn () => ExtractableHelpers::extractValue($arrayObject, 'b'), + InvalidTypeException::class, + 'Missing key: b' + ); + + Assert::exception( + static fn () => ExtractableHelpers::extractValue($arrayObject, '2'), + InvalidTypeException::class, + 'Missing key: 2' + ); + } + +} + +(new ExtractableHelpersTest())->run(); diff --git a/tests/PrimitiveTypesTest.phpt b/tests/PrimitiveTypesTest.phpt index d6aab13..af8ba00 100644 --- a/tests/PrimitiveTypesTest.phpt +++ b/tests/PrimitiveTypesTest.phpt @@ -77,25 +77,6 @@ final class PrimitiveTypesTest extends TestCase InvalidTypeException::class ); - $array = [ - 121, - 112, - 211, - 111, - ]; - - Assert::equal( - $array, - PrimitiveTypes::extractIntArray(['test' => $array], 'test') - ); - - Assert::throws( - static function (): void { - PrimitiveTypes::extractIntArray(['test' => [[]]], 'test'); - }, - InvalidTypeException::class - ); - Assert::equal(10174, PrimitiveTypes::getInt('0010174')); Assert::equal(0, PrimitiveTypes::getInt('0')); @@ -224,25 +205,6 @@ final class PrimitiveTypesTest extends TestCase }, InvalidTypeException::class ); - - $array = [ - 'aaa', - 'bbb', - 'ccc', - ]; - - Assert::equal( - $array, - PrimitiveTypes::extractStringArray(['test' => $array], 'test') - ); - - Assert::throws( - static function (): void { - PrimitiveTypes::extractStringArray(['test' => [[]]], 'test'); - }, - InvalidTypeException::class, - 'Problem at key test: Expected string, got array' - ); } public function testBool(): void @@ -321,55 +283,6 @@ final class PrimitiveTypesTest extends TestCase Assert::null(PrimitiveTypes::extractBoolOrNull(['test' => []], 'test', true)); } - public function testArray(): void - { - Assert::equal([1, 2, 3], PrimitiveTypes::getArray([1, 2, 3])); - - Assert::throws( - static function (): void { - PrimitiveTypes::getArray('aaa'); - }, - InvalidTypeException::class, - 'Expected array, got string (aaa)' - ); - } - - public function testExtractArray(): void - { - $data = [ - 'non_empty_array' => [1, 2, 3], - 'not_an_array' => 'hello!', - ]; - - Assert::same([1, 2, 3], PrimitiveTypes::extractArray($data, 'non_empty_array')); - Assert::exception( - static function () use ($data): void { - PrimitiveTypes::extractArrayOrNull($data, 'not_an_array'); - }, - InvalidTypeException::class, - 'Problem at key not_an_array: Expected array, got string (hello!)' - ); - } - - public function testExtractArrayOrNull(): void - { - $data = [ - 'null' => null, - 'non_empty_array' => [1, 2, 3], - 'not_an_array' => 'hello!', - ]; - - Assert::null(PrimitiveTypes::extractArrayOrNull($data, 'test')); - Assert::null(PrimitiveTypes::extractArrayOrNull($data, 'null')); - Assert::same([1, 2, 3], PrimitiveTypes::extractArrayOrNull($data, 'non_empty_array')); - Assert::exception( - static function () use ($data): void { - PrimitiveTypes::extractArrayOrNull($data, 'not_an_array'); - }, - InvalidTypeException::class - ); - } - } (new PrimitiveTypesTest())->run(); diff --git a/tests/UniqueStringArrayTest.phpt b/tests/UniqueStringArrayTest.phpt index 8e08dde..7f61992 100644 --- a/tests/UniqueStringArrayTest.phpt +++ b/tests/UniqueStringArrayTest.phpt @@ -127,8 +127,6 @@ final class UniqueStringArrayTest extends TestCase $result->toArray() ); - $result->removeDuplicities(); - foreach ($result as $item) { Assert::type('string', $item); } diff --git a/tests/UrlTypeTest.phpt b/tests/UrlTypeTest.phpt index b63a199..607ff1f 100644 --- a/tests/UrlTypeTest.phpt +++ b/tests/UrlTypeTest.phpt @@ -101,10 +101,10 @@ final class UrlTypeTest extends TestCase Assert::equal('x=y', $url->getQueryString()); - Assert::equal('y', $url->getParameter('x')); + Assert::equal('y', $url->getQueryParameter('x')); Assert::equal('https://www.seznam.cz/?x=y', $url->getValue()); - Assert::equal('https://www.seznam.cz/?x=y', $url->toString()); + Assert::equal('https://www.seznam.cz/?x=y', (string) $url); Assert::throws( static function (): void { diff --git a/tests/VatIdTest.phpt b/tests/VatIdTest.phpt index 154b75d..afbe15c 100644 --- a/tests/VatIdTest.phpt +++ b/tests/VatIdTest.phpt @@ -26,7 +26,6 @@ final class VatIdTest extends TestCase public function testIsValid(): void { - /** @var string $vatId */ foreach ($this->getValidVatIds() as $vatId) { Assert::true(VatId::isValid($vatId)); } @@ -34,7 +33,6 @@ final class VatIdTest extends TestCase public function testIsInvalid(): void { - /** @var string $vatId */ foreach ($this->getInvalidVatIds() as $vatId) { Assert::false(VatId::isValid($vatId)); } diff --git a/tests/ZipCodeTest.phpt b/tests/ZipCodeTest.phpt index 7850b2b..eb7ad74 100644 --- a/tests/ZipCodeTest.phpt +++ b/tests/ZipCodeTest.phpt @@ -43,9 +43,23 @@ final class ZipCodeTest extends TestCase Assert::equal($validValue, $zip->getValue()); } + Assert::equal('39174', ZipCode::extract($validValues, 0, countryCode: CountryCode::from(CountryCode::CZ))->getValue()); + + Assert::exception( + static fn () => ZipCode::extract($validValues, 0, countryCode: CountryCode::from(CountryCode::GB)), + InvalidTypeException::class + ); + Assert::equal('WC2N5DU', ZipCode::from('WC2N 5DU')->getValue()); Assert::equal('W22LW', ZipCode::from('w2 2lw')->getValue()); + + Assert::equal('WC2N5DU', ZipCode::from('WC2N 5DU', countryCode: CountryCode::from(CountryCode::GB))->getValue()); + + Assert::exception( + static fn () => ZipCode::from('WC2N 5DU', countryCode: CountryCode::from(CountryCode::SK)), + InvalidTypeException::class + ); } } diff --git a/tools/cs/ruleset.xml b/tools/cs/ruleset.xml index 0867b21..fe8dbbf 100644 --- a/tools/cs/ruleset.xml +++ b/tools/cs/ruleset.xml @@ -1,38 +1,9 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + @@ -41,7 +12,6 @@ - @@ -58,23 +28,19 @@ + + - - - - - - + - - - - - - - + + + + + + @@ -122,7 +88,5 @@ - app/config/environment.php tests/bootstrap.php - libs/SE20/MTA/Messages/CustomNetteMail/*