Skip to content

Commit

Permalink
dx(TypeCheck): Now accept MixedType as an argument.
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Jan 15, 2024
1 parent 66fdf09 commit 07797cd
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* fix(`TransformerRegistry`): Non-object target type is always invariant.
* dx(`SearchResult`): Now an `ArrayAccess`.
* feat(`TryPropertyCommand`): Improve output.
* dx(`TypeCheck`): Now accept `MixedType` as an argument.

## 0.5.10

Expand Down
72 changes: 60 additions & 12 deletions src/Util/TypeCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,33 +35,57 @@ public static function nameExists(string $class): bool
|| enum_exists($class);
}

public static function isInt(?Type $type): bool
public static function isInt(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_INT;
}

public static function isFloat(?Type $type): bool
public static function isFloat(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_FLOAT;
}

public static function isString(?Type $type): bool
public static function isString(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_STRING;
}

public static function isBool(?Type $type): bool
public static function isBool(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_BOOL;
}

public static function isArray(?Type $type): bool
public static function isArray(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_ARRAY;
}

public static function isObject(?Type $type): bool
public static function isObject(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

if ($type === null) {
return false;
}
Expand Down Expand Up @@ -107,8 +131,12 @@ public static function isObjectOfType(null|Type|MixedType $type, string ...$clas
return false;
}

public static function isEnum(?Type $type): bool
public static function isEnum(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

if ($type === null) {
return false;
}
Expand All @@ -120,8 +148,12 @@ public static function isEnum(?Type $type): bool
&& enum_exists($class);
}

public static function isBackedEnum(?Type $type): bool
public static function isBackedEnum(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

if ($type === null) {
return false;
}
Expand All @@ -134,26 +166,42 @@ public static function isBackedEnum(?Type $type): bool
&& is_a($class, \BackedEnum::class, true);
}

public static function isResource(?Type $type): bool
public static function isResource(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_RESOURCE;
}

public static function isNull(?Type $type): bool
public static function isNull(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_NULL;
}

public static function isScalar(?Type $type): bool
public static function isScalar(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return self::isInt($type)
|| self::isFloat($type)
|| self::isString($type)
|| self::isBool($type);
}

public static function isIterable(?Type $type): bool
public static function isIterable(null|Type|MixedType $type): bool
{
if ($type instanceof MixedType) {
return false;
}

return $type?->getBuiltinType() === Type::BUILTIN_TYPE_ITERABLE;
}

Expand Down

0 comments on commit 07797cd

Please sign in to comment.