Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(type): add signed integer types #394

Merged
merged 1 commit into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
# Changelog

## 3.0.0

### features

* introduced a new `Psl\Range` component - by @azjezz
* introduced a new `Psl\Str\range` function - by @azjezz
* introduced a new `Psl\Str\Byte\range` function - by @azjezz
* introduced a new `Psl\Str\Grapheme\range` function - by @azjezz
* introduced a new `Psl\Type\uint` function - by @azjezz
* introduced a new `Psl\Type\i8` function - by @azjezz
* introduced a new `Psl\Type\i16` function - by @azjezz
* introduced a new `Psl\Type\i32` function - by @azjezz
* introduced a new `Psl\Type\i64` function - by @azjezz

### deprecations

* deprecated `Psl\Type\positive_int` function, use `Psl\Type\uint` instead - by @azjezz

## 2.1.0

### features
Expand Down
4 changes: 4 additions & 0 deletions docs/component/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
- [bool](./../../src/Psl/Type/bool.php#L10)
- [dict](./../../src/Psl/Type/dict.php#L16)
- [float](./../../src/Psl/Type/float.php#L10)
- [i16](./../../src/Psl/Type/i16.php#L12)
- [i32](./../../src/Psl/Type/i32.php#L12)
- [i64](./../../src/Psl/Type/i64.php#L12)
- [i8](./../../src/Psl/Type/i8.php#L12)
- [instance_of](./../../src/Psl/Type/instance_of.php#L14)
- [int](./../../src/Psl/Type/int.php#L10)
- [intersection](./../../src/Psl/Type/intersection.php#L18)
Expand Down
8 changes: 8 additions & 0 deletions src/Psl/Internal/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,10 @@ final class Loader
'Psl\\Type\\scalar' => 'Psl/Type/scalar.php',
'Psl\\Type\\shape' => 'Psl/Type/shape.php',
'Psl\\Type\\uint' => 'Psl/Type/uint.php',
'Psl\\Type\\i8' => 'Psl/Type/i8.php',
'Psl\\Type\\i16' => 'Psl/Type/i16.php',
'Psl\\Type\\i32' => 'Psl/Type/i32.php',
'Psl\\Type\\i64' => 'Psl/Type/i64.php',
'Psl\\Type\\union' => 'Psl/Type/union.php',
'Psl\\Type\\vec' => 'Psl/Type/vec.php',
'Psl\\Type\\dict' => 'Psl/Type/dict.php',
Expand Down Expand Up @@ -638,6 +642,10 @@ final class Loader
'Psl\\Type\\Internal\\NonEmptyStringType' => 'Psl/Type/Internal/NonEmptyStringType.php',
'Psl\\Type\\Internal\\NonEmptyVecType' => 'Psl/Type/Internal/NonEmptyVecType.php',
'Psl\\Type\\Internal\\UIntType' => 'Psl/Type/Internal/UIntType.php',
'Psl\\Type\\Internal\\I8Type' => 'Psl/Type/Internal/I8Type.php',
'Psl\\Type\\Internal\\I16Type' => 'Psl/Type/Internal/I16Type.php',
'Psl\\Type\\Internal\\I32Type' => 'Psl/Type/Internal/I32Type.php',
'Psl\\Type\\Internal\\I64Type' => 'Psl/Type/Internal/I64Type.php',
'Psl\\Type\\Internal\\UnionType' => 'Psl/Type/Internal/UnionType.php',
'Psl\\Type\\Internal\\VecType' => 'Psl/Type/Internal/VecType.php',
'Psl\\Type\\Internal\\DictType' => 'Psl/Type/Internal/DictType.php',
Expand Down
2 changes: 1 addition & 1 deletion src/Psl/Math/constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
*
* @var int
*/
const INT8_MAX = 128;
const INT8_MAX = 127;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i just noticed this...


/**
* The minimum integer value representable in a 8-bit binary-coded decimal.
Expand Down
77 changes: 77 additions & 0 deletions src/Psl/Type/Internal/I16Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Psl\Type\Internal;

use Psl\Math;
use Psl\Type;
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function is_int;

/**
* @ara-extends Type\Type<i16>
*
* @extends Type\Type<int<-32768, 32767>>
*
* @internal
*/
final class I16Type extends Type\Type
{
/**
* @ara-assert-if-true i16 $value
*
* @psalm-assert-if-true int<-32768, 32767> $value
*/
public function matches(mixed $value): bool
{
return is_int($value) && $value >= Math\INT16_MIN && $value <= MATH\INT16_MAX;
}

/**
* @throws CoercionException
*
* @ara-return i16
*
* @return int<-32768, 32767>
*/
public function coerce(mixed $value): int
{
$integer = Type\int()
->withTrace($this->getTrace()->withFrame($this->toString()))
->coerce($value);

if ($integer >= Math\INT16_MIN && $integer <= MATH\INT16_MAX) {
return $integer;
}

throw CoercionException::withValue($value, $this->toString(), $this->getTrace());
}

/**
* @ara-assert i16 $value
*
* @psalm-assert int<-32768, 32767> $value
*
* @throws AssertException
*
* @ara-return i16
*
* @return int<-32768, 32767>
*/
public function assert(mixed $value): int
{
if (is_int($value) && $value >= Math\INT16_MIN && $value <= MATH\INT16_MAX) {
return $value;
}

throw AssertException::withValue($value, $this->toString(), $this->getTrace());
}

public function toString(): string
{
return 'i16';
}
}
78 changes: 78 additions & 0 deletions src/Psl/Type/Internal/I32Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Psl\Type\Internal;

use Psl\Math;
use Psl\Type;
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function is_int;
use function Psl\Type;

/**
* @ara-extends Type\Type<i32>
*
* @extends Type\Type<int<-2147483648, 2147483647>>
*
* @internal
*/
final class I32Type extends Type\Type
{
/**
* @ara-assert-if-true i32 $value
*
* @psalm-assert-if-true int<-2147483648, 2147483647> $value
*/
public function matches(mixed $value): bool
{
return is_int($value) && $value >= Math\INT32_MIN && $value <= MATH\INT32_MAX;
}

/**
* @throws CoercionException
*
* @ara-return i32
*
* @return int<-2147483648, 2147483647>
*/
public function coerce(mixed $value): int
{
$integer = Type\int()
->withTrace($this->getTrace()->withFrame($this->toString()))
->coerce($value);

if ($integer >= Math\INT32_MIN && $integer <= MATH\INT32_MAX) {
return $integer;
}

throw CoercionException::withValue($value, $this->toString(), $this->getTrace());
}

/**
* @ara-assert i32 $value
*
* @psalm-assert int<-2147483648, 2147483647> $value
*
* @throws AssertException
*
* @ara-return i32
*
* @return int<-2147483648, 2147483647>
*/
public function assert(mixed $value): int
{
if (is_int($value) && $value >= Math\INT32_MIN && $value <= MATH\INT32_MAX) {
return $value;
}

throw AssertException::withValue($value, $this->toString(), $this->getTrace());
}

public function toString(): string
{
return 'i32';
}
}
70 changes: 70 additions & 0 deletions src/Psl/Type/Internal/I64Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace Psl\Type\Internal;

use Psl\Type;
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function is_int;

/**
* @ara-extends Type\Type<i64>
*
* @extends Type\Type<int>
*
* @internal
*/
final class I64Type extends Type\Type
{
/**
* @ara-assert-if-true i64 $value
*
* @psalm-assert-if-true int $value
*/
public function matches(mixed $value): bool
{
return is_int($value);
}

/**
* @throws CoercionException
*
* @ara-return i64
*
* @return int
*/
public function coerce(mixed $value): int
{
return Type\int()
->withTrace($this->getTrace()->withFrame($this->toString()))
->coerce($value);
}

/**
* @ara-assert i64 $value
*
* @psalm-assert int $value
*
* @throws AssertException
*
* @ara-return i64
*
* @return int
*/
public function assert(mixed $value): int
{
if (is_int($value)) {
return $value;
}

throw AssertException::withValue($value, $this->toString(), $this->getTrace());
}

public function toString(): string
{
return 'i64';
}
}
77 changes: 77 additions & 0 deletions src/Psl/Type/Internal/I8Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace Psl\Type\Internal;

use Psl\Math;
use Psl\Type;
use Psl\Type\Exception\AssertException;
use Psl\Type\Exception\CoercionException;

use function is_int;

/**
* @ara-extends Type\Type<i8>
*
* @extends Type\Type<int<-128, 127>>
*
* @internal
*/
final class I8Type extends Type\Type
{
/**
* @ara-assert-if-true i8 $value
*
* @psalm-assert-if-true int<-128, 127> $value
*/
public function matches(mixed $value): bool
{
return is_int($value) && $value >= Math\INT8_MIN && $value <= Math\INT8_MAX;
}

/**
* @throws CoercionException
*
* @ara-return i8
*
* @return int<-128, 127>
*/
public function coerce(mixed $value): int
{
$integer = Type\int()
->withTrace($this->getTrace()->withFrame($this->toString()))
->coerce($value);

if ($integer >= Math\INT8_MIN && $integer <= Math\INT8_MAX) {
return $integer;
}

throw CoercionException::withValue($value, $this->toString(), $this->getTrace());
}

/**
* @ara-assert i8 $value
*
* @psalm-assert int<-128, 127> $value
*
* @throws AssertException
*
* @ara-return i8
*
* @return int<-128, 127>
*/
public function assert(mixed $value): int
{
if (is_int($value) && $value >= Math\INT8_MIN && $value <= Math\INT8_MAX) {
return $value;
}

throw AssertException::withValue($value, $this->toString(), $this->getTrace());
}

public function toString(): string
{
return 'i8';
}
}
2 changes: 1 addition & 1 deletion src/Psl/Type/Internal/VecType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function coerce(mixed $value): iterable
/** @var Type\Type<Tv> $value_type */
$value_type = $this->value_type->withTrace(
$this->getTrace()
->withFrame('vec<' . $this->value_type->toString() . '>')
->withFrame($this->toString())
);

/**
Expand Down
15 changes: 15 additions & 0 deletions src/Psl/Type/i16.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Psl\Type;

/**
* @ara-return TypeInterface<i16>
*
* @return TypeInterface<int<-32768, 32767>>
*/
function i16(): TypeInterface
{
return new Internal\I16Type();
}
Loading