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

Feature/variadic union type improvemence #184

Merged
merged 6 commits into from
Apr 27, 2021
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
2 changes: 1 addition & 1 deletion src/Psl/Type/literal_scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
function literal_scalar($value): TypeInterface
{
/** @psalm-suppress MissingThrowsDocblock */
union(union(string(), bool()), num())->assert($value);
union(string(), bool(), num())->assert($value);

return new Internal\LiteralScalarType($value);
}
25 changes: 16 additions & 9 deletions src/Psl/Type/union.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@
use Psl;

/**
* @template Tl
* @template Tr
* @template T
*
* @param TypeInterface<Tl> $left_type
* @param TypeInterface<Tr> $right_type
* @param TypeInterface<T> $first
* @param TypeInterface<T> $second
* @param TypeInterface<T> ...$rest
*
* @throws Psl\Exception\InvariantViolationException If $left_type, or $right_type is optional.
* @throws Psl\Exception\InvariantViolationException If $first, $second or one of $rest is optional.
*
* @return TypeInterface<Tl|Tr>
* @return TypeInterface<T>
*/
function union(
TypeInterface $left_type,
TypeInterface $right_type
TypeInterface $first,
TypeInterface $second,
TypeInterface ...$rest
): TypeInterface {
return new Internal\UnionType($left_type, $right_type);
$accumulated_type = new Internal\UnionType($first, $second);

foreach ($rest as $type) {
$accumulated_type = new Internal\UnionType($accumulated_type, $type);
}

return $accumulated_type;
}
43 changes: 43 additions & 0 deletions tests/static-analysis/Type/union.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

use Psl\Type;

/**
* @psalm-suppress UnusedParam
*
* @param 'PENDING'|'PROCESSING'|'COMPLETED'|'ERROR' $state
*/
function takes_valid_state(string $state): void
{
}

function test(): void
{
/** @psalm-suppress MissingThrowsDocblock */
$old_school_codec = Type\union(
Type\literal_scalar('PENDING'),
Type\union(
Type\literal_scalar('PROCESSING'),
Type\union(
Type\literal_scalar('COMPLETED'),
Type\literal_scalar('ERROR'),
)
)
);

/** @psalm-suppress MissingThrowsDocblock */
$new_codec = Type\union(
Type\literal_scalar('PENDING'),
Type\literal_scalar('PROCESSING'),
Type\literal_scalar('COMPLETED'),
Type\literal_scalar('ERROR'),
);

/** @psalm-suppress MissingThrowsDocblock */
takes_valid_state($old_school_codec->assert('any'));

/** @psalm-suppress MissingThrowsDocblock */
takes_valid_state($new_codec->assert('any'));
}
17 changes: 14 additions & 3 deletions tests/unit/Type/UnionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getToStringExamples(): iterable
{
yield [Type\union(Type\bool(), Type\string()), 'bool|string'];
yield [Type\union(Type\bool(), Type\float()), 'bool|float'];
yield [Type\union(Type\bool(), Type\union(Type\float(), Type\int())), 'bool|float|int'];
yield [Type\union(Type\bool(), Type\float(), Type\int()), 'bool|float|int'];
yield [Type\union(Type\bool(), Type\num()), 'bool|num'];
yield [Type\union(Type\bool(), Type\array_key()), 'bool|array-key'];
yield [
Expand All @@ -58,9 +58,20 @@ public function getToStringExamples(): iterable
Type\object(IndexAccessInterface::class),
Type\object(CollectionInterface::class)
),
Type\bool()
Type\bool(),
Type\non_empty_string()
),
'((Psl\Collection\IndexAccessInterface&Psl\Collection\CollectionInterface)|bool)|non-empty-string'
];
yield [
Type\union(
Type\null(),
Type\vec(Type\positive_int()),
Type\literal_scalar('php'),
Type\literal_scalar('still'),
Type\literal_scalar('alive'),
),
'(Psl\Collection\IndexAccessInterface&Psl\Collection\CollectionInterface)|bool'
'null|list<positive-int>|"php"|"still"|"alive"'
];
}
}