Skip to content

Commit cb4cc9a

Browse files
committed
Fix nullable message
1 parent b475faa commit cb4cc9a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Assert/Filter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,19 @@ public static function boolean(callable $filter): void
6060
}
6161

6262
$returnTypeName = $returnType->getName();
63+
6364
if ($returnTypeName !== 'bool') {
6465
throw new InvalidArgumentException(sprintf(
6566
self::MESSAGE,
6667
$returnTypeName
6768
));
6869
}
70+
71+
if ($returnType->allowsNull()) {
72+
throw new InvalidArgumentException(sprintf(
73+
self::MESSAGE,
74+
'?' . $returnTypeName
75+
));
76+
}
6977
}
7078
}

tests/FilterTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ public function __invoke(int $datum): string
6969
AtLeast::once($data, $filter);
7070
}
7171

72+
public function testWithNullableReturnTypeCallable(): void
73+
{
74+
$this->expectException(InvalidArgumentException::class);
75+
$this->expectExceptionMessage('Expected a bool return type on callable filter, ?bool given');
76+
77+
$data = [1, 2, 3];
78+
$filter = new class {
79+
public function __invoke(int $datum): ?bool
80+
{
81+
return true;
82+
}
83+
};
84+
85+
AtLeast::once($data, $filter);
86+
}
87+
7288
public function testWithUnionReturnTypeCallable(): void
7389
{
7490
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)