-
-
Notifications
You must be signed in to change notification settings - Fork 72
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i just noticed this...