File tree Expand file tree Collapse file tree 2 files changed +48
-0
lines changed
Expand file tree Collapse file tree 2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change 77use Closure ;
88use InvalidArgumentException ;
99use ReflectionFunction ;
10+ use ReflectionIntersectionType ;
1011use ReflectionMethod ;
1112use ReflectionNamedType ;
13+ use ReflectionUnionType ;
1214
15+ use function array_map ;
1316use function gettype ;
17+ use function implode ;
1418use function is_object ;
1519use function sprintf ;
1620
@@ -30,6 +34,19 @@ public static function boolean(callable $filter): void
3034
3135 $ returnType = $ reflection ->getReturnType ();
3236
37+ if ($ returnType instanceof ReflectionUnionType || $ returnType instanceof ReflectionIntersectionType) {
38+ $ separator = $ returnType instanceof ReflectionUnionType ? '| ' : '& ' ;
39+ throw new InvalidArgumentException (
40+ sprintf (
41+ 'Expected a bool return type on callable filter, %s given ' ,
42+ implode ($ separator , array_map (
43+ static fn (ReflectionNamedType $ type ): string => $ type ->getName (),
44+ $ returnType ->getTypes ()
45+ ))
46+ )
47+ );
48+ }
49+
3350 if (! $ returnType instanceof ReflectionNamedType) {
3451 throw new InvalidArgumentException ('Expected a bool return type on callable filter, null given ' );
3552 }
Original file line number Diff line number Diff line change @@ -68,4 +68,35 @@ public function __invoke(int $datum): string
6868
6969 AtLeast::once ($ data , $ filter );
7070 }
71+
72+ public function testWithUnionReturnTypeCallable (): void
73+ {
74+ $ this ->expectException (InvalidArgumentException::class);
75+ $ this ->expectExceptionMessage ('Expected a bool return type on callable filter, string|bool given ' );
76+
77+ $ data = [1 , 2 , 3 ];
78+ $ filter = new class {
79+ public function __invoke (int $ datum ): string |bool
80+ {
81+ return 'test ' ;
82+ }
83+ };
84+
85+ AtLeast::once ($ data , $ filter );
86+ }
87+
88+ public function testWithIntersectionTypeCallable (): void
89+ {
90+ $ this ->expectException (InvalidArgumentException::class);
91+ $ this ->expectExceptionMessage ('Expected a bool return type on callable filter, ArrayLookup\Tests\A&ArrayLookup\Tests\B given ' );
92+
93+ $ data = [1 , 2 , 3 ];
94+ $ filter = new class {
95+ public function __invoke (int $ datum ): A &B
96+ {
97+ }
98+ };
99+
100+ AtLeast::once ($ data , $ filter );
101+ }
71102}
You can’t perform that action at this time.
0 commit comments