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

Added ConditionalType #17

Merged
merged 4 commits into from
Sep 29, 2023
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
28 changes: 28 additions & 0 deletions src/Argument.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @psalm-immutable
*/
final class Argument
vudaltsov marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* @var non-empty-string
*/
public readonly string $name;

/**
* @internal
* @psalm-internal Typhoon\Type
* @param non-empty-string $name
*/
public function __construct(
string $name,
) {
$this->name = $name;
}
}
42 changes: 42 additions & 0 deletions src/ConditionalType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Typhoon\Type;

/**
* @api
* @psalm-immutable
* @implements Type<mixed>
*/
final class ConditionalType implements Type
{
public readonly Argument|TemplateType $subject;

public readonly Type $is;

public readonly Type $if;

public readonly Type $else;

/**
* @internal
* @psalm-internal Typhoon\Type
*/
public function __construct(
Argument|TemplateType $subject,
Type $is,
Type $if,
Type $else,
) {
$this->subject = $subject;
$this->is = $is;
$this->if = $if;
$this->else = $else;
}

public function accept(TypeVisitor $visitor): mixed
{
return $visitor->visitConditional($this);
}
}
3 changes: 3 additions & 0 deletions src/TypeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ public function visitValueOf(ValueOfType $type): mixed;
/** @return TReturn */
public function visitTemplate(TemplateType $type): mixed;

/** @return TReturn */
public function visitConditional(ConditionalType $type): mixed;

/** @return TReturn */
public function visitIntersection(IntersectionType $type): mixed;

Expand Down
17 changes: 17 additions & 0 deletions src/types.php
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ public static function template(string $name): TemplateType
return new TemplateType($name);
}

/**
* @psalm-pure
*/
public static function conditional(Argument|TemplateType $subject, Type $is, Type $if, Type $else): ConditionalType
{
return new ConditionalType($subject, $is, $if, $else);
}

/**
* @psalm-pure
* @no-named-arguments
Expand Down Expand Up @@ -491,6 +499,15 @@ public static function union(Type $type1, Type $type2, Type ...$moreTypes): Unio
{
return new UnionType([$type1, $type2, ...$moreTypes]);
}

/**
* @psalm-pure
* @param non-empty-string $name
*/
public function arg(string $name): Argument
{
return new Argument($name);
}
}

/**
Expand Down