Skip to content

Commit

Permalink
Removed @no-named-arguments, simplified union() and intersection() api
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Feb 25, 2024
1 parent f2d95e8 commit b9af741
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,14 @@ public static function constant(string $name): Type
return new ConstantType($name);
}

/**
* @no-named-arguments
*/
public static function intersection(Type $type1, Type $type2, Type ...$types): Type
public static function intersection(Type ...$types): Type
{
return new IntersectionType([$type1, $type2, ...$types]);
return match (\count($types)) {
0 => self::never,
1 => $types[array_key_first($types)],
/** @phpstan-ignore argument.type */
default => new UnionType(array_values($types)),
};
}

/**
Expand Down Expand Up @@ -328,7 +330,6 @@ public static function nonEmptyList(Type $value = self::mixed): Type
}

/**
* @no-named-arguments
* @template TType
* @param Type<TType> $type
* @return Type<?TType>
Expand All @@ -339,14 +340,13 @@ public static function nullable(Type $type): Type
}

/**
* @no-named-arguments
* @template TObject of object
* @param class-string<TObject>|non-empty-string $class
* @return ($class is class-string ? Type<TObject> : Type<object>)
*/
public static function object(string $class, Type ...$templateArguments): Type
{
return new NamedObjectType($class, $templateArguments);
return new NamedObjectType($class, array_values($templateArguments));
}

/**
Expand Down Expand Up @@ -398,16 +398,18 @@ public static function template(string $name, AtMethod|AtClass|AtFunction $decla
}

/**
* @no-named-arguments
* @template TType
* @param Type<TType> $type1
* @param Type<TType> $type2
* @param Type<TType> ...$types
* @return Type<TType>
*/
public static function union(Type $type1, Type $type2, Type ...$types): Type
public static function union(Type ...$types): Type
{
return new UnionType([$type1, $type2, ...$types]);
return match (\count($types)) {
0 => self::never,
1 => $types[array_key_first($types)],
/** @phpstan-ignore argument.type */
default => new UnionType(array_values($types)),
};
}

public static function value(Type $type): Type
Expand Down

0 comments on commit b9af741

Please sign in to comment.