Skip to content

Commit

Permalink
feat(types): Memoize simple types without arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
veewee committed Apr 4, 2024
1 parent f7d74fc commit d96cdcc
Show file tree
Hide file tree
Showing 49 changed files with 330 additions and 35 deletions.
26 changes: 15 additions & 11 deletions src/Psl/Type/Internal/LiteralScalarType.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,38 @@ public function matches(mixed $value): bool
*/
public function coerce(mixed $value): string|int|float|bool
{
if ($value === $this->value) {
$expectedScalarValue = $this->value;
if ($value === $expectedScalarValue) {
/** @var T $value */
return $value;
}

if (Type\string()->matches($this->value)) {
$coerced_value = Type\string()->coerce($value);
if ($this->value === $coerced_value) {
$stringType = Type\string();
if ($stringType->matches($this->value)) {
$coerced_value = $stringType->coerce($value);
if ($expectedScalarValue === $coerced_value) {
/** @var T $coerced_value */
return $coerced_value;
}

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

if (Type\int()->matches($this->value)) {
$coerced_value = Type\int()->coerce($value);
if ($this->value === $coerced_value) {
$intType = Type\int();
if ($intType->matches($this->value)) {
$coerced_value = $intType->coerce($value);
if ($expectedScalarValue === $coerced_value) {
/** @var T $coerced_value */
return $coerced_value;
}

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

if (Type\float()->matches($this->value)) {
$coerced_value = Type\float()->coerce($value);
if ($this->value === $coerced_value) {
$floatType = Type\float();
if ($floatType->matches($this->value)) {
$coerced_value = $floatType->coerce($value);
if ($expectedScalarValue === $coerced_value) {
/** @var T $coerced_value */
return $coerced_value;
}
Expand All @@ -77,7 +81,7 @@ public function coerce(mixed $value): string|int|float|bool
}

/** @var bool $literal_value */
$literal_value = $this->value;
$literal_value = $expectedScalarValue;
$coerced_value = Type\bool()->coerce($value);
if ($literal_value === $coerced_value) {
/** @var T $coerced_value */
Expand Down
5 changes: 4 additions & 1 deletion src/Psl/Type/array_key.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function array_key(): TypeInterface
{
return new Internal\ArrayKeyType();
/** @var TypeInterface<array-key> $instance */
static $instance = new Internal\ArrayKeyType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/bool.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function bool(): TypeInterface
{
return new Internal\BoolType();
/** @var TypeInterface<bool> $instance */
static $instance = new Internal\BoolType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/f32.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function f32(): TypeInterface
{
return new Internal\F32Type();
/** @var TypeInterface<float> $instance */
static $instance = new Internal\F32Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/f64.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function f64(): TypeInterface
{
return new Internal\F64Type();
/** @var TypeInterface<float> $instance */
static $instance = new Internal\F64Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/float.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function float(): TypeInterface
{
return new Internal\FloatType();
/** @var TypeInterface<float> $instance */
static $instance = new Internal\FloatType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/i16.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function i16(): TypeInterface
{
return new Internal\I16Type();
/** @var TypeInterface<int<-32768, 32767>> $instance */
static $instance = new Internal\I16Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/i32.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function i32(): TypeInterface
{
return new Internal\I32Type();
/** @var TypeInterface<int<-2147483648, 2147483647>> $instance */
static $instance = new Internal\I32Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/i64.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function i64(): TypeInterface
{
return new Internal\I64Type();
/** @var TypeInterface<int> $instance */
static $instance = new Internal\I64Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/i8.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function i8(): TypeInterface
{
return new Internal\I8Type();
/** @var TypeInterface<int<-128, 127>> $instance */
static $instance = new Internal\I8Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/int.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function int(): TypeInterface
{
return new Internal\IntType();
/** @var TypeInterface<int> $instance */
static $instance = new Internal\IntType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/mixed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function mixed(): TypeInterface
{
return new Internal\MixedType();
/** @var TypeInterface<mixed> $instance */
static $instance = new Internal\MixedType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/non_empty_string.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function non_empty_string(): TypeInterface
{
return new Internal\NonEmptyStringType();
/** @var TypeInterface<non-empty-string> $instance */
static $instance = new Internal\NonEmptyStringType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/nonnull.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function nonnull(): TypeInterface
{
return new Internal\NonNullType();
/** @var TypeInterface<mixed> $instance */
static $instance = new Internal\NonNullType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/null.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function null(): TypeInterface
{
return new Internal\NullType();
/** @var TypeInterface<null> $instance */
static $instance = new Internal\NullType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/num.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function num(): TypeInterface
{
return new Internal\NumType();
/** @var TypeInterface<int|float> $instance */
static $instance = new Internal\NumType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/numeric_string.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function numeric_string(): TypeInterface
{
return new Internal\NumericStringType();
/** @var TypeInterface<numeric-string> $instance */
static $instance = new Internal\NumericStringType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/object.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function object(): TypeInterface
{
return new Internal\ObjectType();
/** @var TypeInterface<object> $instance */
static $instance = new Internal\ObjectType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/positive_int.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function positive_int(): TypeInterface
{
return new Internal\PositiveIntType();
/** @var TypeInterface<positive-int> $instance */
static $instance = new Internal\PositiveIntType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function scalar(): TypeInterface
{
return new Internal\ScalarType();
/** @var TypeInterface<string|bool|int|float> $instance */
static $instance = new Internal\ScalarType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/string.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
*/
function string(): TypeInterface
{
return new Internal\StringType();
/** @var TypeInterface<string> $instance */
static $instance = new Internal\StringType();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/u16.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function u16(): TypeInterface
{
return new Internal\U16Type();
/** @var TypeInterface<int<0, 65535>> $instance */
static $instance = new Internal\U16Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/u32.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function u32(): TypeInterface
{
return new Internal\U32Type();
/** @var TypeInterface<int<0, 4294967295>> $instance */
static $instance = new Internal\U32Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/u8.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function u8(): TypeInterface
{
return new Internal\U8Type();
/** @var TypeInterface<int<0, 255>> $instance */
static $instance = new Internal\U8Type();

return $instance;
}
5 changes: 4 additions & 1 deletion src/Psl/Type/uint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
*/
function uint(): TypeInterface
{
return new Internal\UIntType();
/** @var TypeInterface<int<0, max>> $instance */
static $instance = new Internal\UIntType();

return $instance;
}
Loading

0 comments on commit d96cdcc

Please sign in to comment.