@@ -342,33 +342,49 @@ function _checkTypehint(callable $callback, \Throwable $reason): bool
342
342
return true ;
343
343
}
344
344
345
- $ type = $ parameters [0 ]-> getType () ;
345
+ $ expectedException = $ parameters [0 ];
346
346
347
- if (!$ type ) {
348
- return true ;
349
- }
347
+ // PHP before v8 used an easy API:
348
+ if (\PHP_VERSION_ID < 70100 || \defined ('HHVM_VERSION ' )) {
349
+ if (!$ expectedException ->getClass ()) {
350
+ return true ;
351
+ }
350
352
351
- $ types = [$ type ];
353
+ return $ expectedException ->getClass ()->isInstance ($ reason );
354
+ }
352
355
353
- if ($ type instanceof \ReflectionUnionType) {
354
- $ types = $ type ->getTypes ();
356
+ // Extract the type of the argument and handle different possibilities
357
+ $ type = $ expectedException ->getType ();
358
+ $ types = [];
359
+
360
+ switch (true ) {
361
+ case $ type === null :
362
+ break ;
363
+ case $ type instanceof \ReflectionNamedType:
364
+ $ types = [$ type ];
365
+ break ;
366
+ case $ type instanceof \ReflectionUnionType;
367
+ $ types = $ type ->getTypes ();
368
+ break ;
369
+ default :
370
+ throw new \LogicException ('Unexpected return value of ReflectionParameter::getType ' );
355
371
}
356
372
357
- $ mismatched = false ;
373
+ // If there is no type restriction, it matches
374
+ if (empty ($ types )) {
375
+ return true ;
376
+ }
358
377
378
+ // Search for one matching named-type for success, otherwise return false
379
+ // A named-type can be either a class-name or a built-in type like string, int, array, etc.
359
380
foreach ($ types as $ type ) {
360
- if (!$ type || $ type ->isBuiltin ()) {
361
- continue ;
362
- }
381
+ $ matches = ($ type ->isBuiltin () && \gettype ($ reason ) === $ type ->getName ())
382
+ || (new \ReflectionClass ($ type ->getName ()))->isInstance ($ reason );
363
383
364
- $ expectedClass = $ type ->getName ();
365
-
366
- if ($ reason instanceof $ expectedClass ) {
384
+ if ($ matches ) {
367
385
return true ;
368
386
}
369
-
370
- $ mismatched = true ;
371
387
}
372
388
373
- return ! $ mismatched ;
389
+ return false ;
374
390
}
0 commit comments