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 intersection type #185

Merged
merged 17 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 19 additions & 10 deletions src/Psl/Type/intersection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,29 @@
use Psl;

/**
* @template Tl
* @template Tr
* @template TFirst
* @template TSecond
* @template TRest
*
* @param TypeInterface<Tl> $left_type
* @param TypeInterface<Tr> $right_type
* @param TypeInterface<TFirst> $first
* @param TypeInterface<TSecond> $second
* @param TypeInterface<TRest> ...$rest
*
* @throws Psl\Exception\InvariantViolationException If $left_type, or $right_type is optional.
* @throws Psl\Exception\InvariantViolationException If $first, or $second is optional.
*
* @return TypeInterface<Tl&Tr>
* @return TypeInterface<TFirst&TSecond&TRest>
*/
function intersection(
TypeInterface $left_type,
TypeInterface $right_type
TypeInterface $first,
TypeInterface $second,
TypeInterface ...$rest
): TypeInterface {
/** @var TypeInterface<Tl&Tr> */
return new Internal\IntersectionType($left_type, $right_type);
$accumulated_type = new Internal\IntersectionType($first, $second);

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

/** @var TypeInterface<TFirst&TSecond&TRest> */
return $accumulated_type;
}
41 changes: 41 additions & 0 deletions tests/static-analysis/Type/intersection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

use Psl\Collection\Map;
use Psl\Result\ResultInterface;
use Psl\Type;

/**
* @psalm-suppress UnusedParam
azjezz marked this conversation as resolved.
Show resolved Hide resolved
*
* @param Map&ResultInterface&stdClass $value
*/
function takes_valid_intersection($value): void
{
}

function test(): void
{
/** @psalm-suppress MissingThrowsDocblock */
$old_school_codec = Type\intersection(
Type\object(Map::class),
Type\intersection(
Type\object(ResultInterface::class),
Type\object(stdClass::class),
),
);

/** @psalm-suppress MissingThrowsDocblock */
$new_codec = Type\intersection(
Type\object(Map::class),
Type\object(ResultInterface::class),
Type\object(stdClass::class),
);
azjezz marked this conversation as resolved.
Show resolved Hide resolved

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

/** @psalm-suppress MissingThrowsDocblock */
takes_valid_intersection($new_codec->assert('any'));
}