Skip to content

Commit 3b815db

Browse files
committed
[DX] Early check nullable any type on Filter
1 parent cb4cc9a commit 3b815db

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Assert/Filter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ public static function boolean(callable $filter): void
6161

6262
$returnTypeName = $returnType->getName();
6363

64-
if ($returnTypeName !== 'bool') {
64+
if ($returnType->allowsNull()) {
6565
throw new InvalidArgumentException(sprintf(
6666
self::MESSAGE,
67-
$returnTypeName
67+
'?' . $returnTypeName
6868
));
6969
}
7070

71-
if ($returnType->allowsNull()) {
71+
if ($returnTypeName !== 'bool') {
7272
throw new InvalidArgumentException(sprintf(
7373
self::MESSAGE,
74-
'?' . $returnTypeName
74+
$returnTypeName
7575
));
7676
}
7777
}

tests/FilterTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ public function __invoke(int $datum): ?bool
8585
AtLeast::once($data, $filter);
8686
}
8787

88+
public function testWithNullableReturnNonBoolTypeCallable(): void
89+
{
90+
$this->expectException(InvalidArgumentException::class);
91+
$this->expectExceptionMessage('Expected a bool return type on callable filter, ?string given');
92+
93+
$data = [1, 2, 3];
94+
$filter = new class {
95+
public function __invoke(int $datum): ?string
96+
{
97+
return 'test';
98+
}
99+
};
100+
101+
AtLeast::once($data, $filter);
102+
}
103+
88104
public function testWithUnionReturnTypeCallable(): void
89105
{
90106
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)