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(types): Memoize simple types without arguments #457

Merged
merged 1 commit into from
Apr 4, 2024
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
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 Internal\ArrayKeyType $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 Internal\BoolType $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 Internal\F32Type $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 Internal\F64Type $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 Internal\FloatType $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 Internal\I16Type $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 Internal\I32Type $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 Internal\I64Type $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 Internal\I8Type $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 Internal\IntType $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 Internal\MixedType $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 Internal\NonEmptyStringType $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 Internal\NonNullType $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 Internal\NullType $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 Internal\NumType $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 Internal\NumericStringType $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 Internal\ObjectType $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 Internal\PositiveIntType $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 Internal\ScalarType $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 Internal\StringType $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 Internal\U16Type $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 Internal\U32Type $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 Internal\U8Type $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 Internal\UIntType $instance */
static $instance = new Internal\UIntType();

return $instance;
}
Loading