Skip to content

Commit dd3f2b8

Browse files
authored
Merge pull request #37 from samsonasik/fix-unioned-return-type-with-intersection
Fix unioned intersection on Assert Filter
2 parents 0726711 + 6b3397a commit dd3f2b8

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

src/Assert/Filter.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,26 @@ public static function boolean(callable $filter): void
4242
$separator = $returnType instanceof ReflectionUnionType ? '|' : '&';
4343
$types = $returnType->getTypes();
4444

45-
Assert::allIsInstanceOf($types, ReflectionNamedType::class);
45+
Assert::allIsInstanceOfAny($types, [ReflectionNamedType::class, ReflectionIntersectionType::class]);
4646

4747
throw new InvalidArgumentException(
4848
sprintf(
4949
self::MESSAGE,
5050
implode($separator, array_map(
51-
static fn (ReflectionNamedType $reflectionNamedType): string => $reflectionNamedType->getName(),
51+
function (ReflectionNamedType|ReflectionIntersectionType $reflectionType): string {
52+
if ($reflectionType instanceof ReflectionNamedType) {
53+
return $reflectionType->getName();
54+
}
55+
56+
$subTypes = $reflectionType->getTypes();
57+
Assert::allIsInstanceOf($subTypes, ReflectionNamedType::class);
58+
59+
return '(' . implode('&', array_map(
60+
static fn (ReflectionNamedType $reflectionNamedType): string
61+
=> $reflectionNamedType->getName(),
62+
$subTypes
63+
)) . ')';
64+
},
5265
$types
5366
))
5467
)

tests/FilterTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,21 @@ public function __invoke(int $datum): A&B
133133

134134
AtLeast::once($data, $filter);
135135
}
136+
137+
public function testWithUnionedIntersectionTypeCallable(): void
138+
{
139+
$this->expectException(InvalidArgumentException::class);
140+
$this->expectExceptionMessage(
141+
'Expected a bool return type on callable filter, (ArrayLookup\Tests\A&ArrayLookup\Tests\B)|string given'
142+
);
143+
144+
$data = [1, 2, 3];
145+
$filter = new class {
146+
public function __invoke(int $datum): (A&B)|string
147+
{
148+
}
149+
};
150+
151+
AtLeast::once($data, $filter);
152+
}
136153
}

0 commit comments

Comments
 (0)