3v4l.org

run code in 500+ PHP versions simultaneously
<?php class X { } class Y { } function foo(X|Y|null $param, string|bool|null $anotherParam) { } class Z extends Y { } class U { } function isCompatibleWithType(string $type, mixed $value): bool { if (gettype($value) === 'object') { return get_class($value) === $type || is_subclass_of($value, $type); } return gettype($value) === $type; } function canAccept(callable $func, int $paramPosition, mixed $paramValue): bool { $r = new \ReflectionFunction($func); $canAccept = false; foreach ($r->getParameters() as $parameter) { if ($parameter->getPosition() !== $paramPosition) { continue; } $type = $parameter->getType(); if ($type instanceof \ReflectionUnionType) { foreach ($type->getTypes() as $t) { if (isCompatibleWithType($t->getName(), $paramValue) || ($t->allowsNull() && is_null($paramValue))) { return true; } } return false; } if ($type instanceof \ReflectionIntersectionType) { foreach ($type->getTypes() as $t) { if (!isCompatibleWithType($t->getName(), $paramValue)) /* intersection types do not support nullables for now */ { return false; } } return true; } return isCompatibleWithType($type->getName(), $paramValue) || ($t->allowsNull() && is_null($paramValue)); } return false; } var_dump(canAccept('foo', 0, null)); var_dump(canAccept('foo', 1, null));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6AnNZ
function name:  (null)
number of ops:  17
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   62     0  E >   INIT_FCALL                                                   'var_dump'
          1        INIT_FCALL                                                   'canaccept'
          2        SEND_VAL                                                     'foo'
          3        SEND_VAL                                                     0
          4        SEND_VAL                                                     null
          5        DO_FCALL                                          0  $0      
          6        SEND_VAR                                                     $0
          7        DO_ICALL                                                     
   63     8        INIT_FCALL                                                   'var_dump'
          9        INIT_FCALL                                                   'canaccept'
         10        SEND_VAL                                                     'foo'
         11        SEND_VAL                                                     1
         12        SEND_VAL                                                     null
         13        DO_FCALL                                          0  $2      
         14        SEND_VAR                                                     $2
         15        DO_ICALL                                                     
         16      > RETURN                                                       1

Function foo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6AnNZ
function name:  foo
number of ops:  3
compiled vars:  !0 = $param, !1 = $anotherParam
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
    8     2      > RETURN                                                       null

End of function foo

Function iscompatiblewithtype:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6AnNZ
function name:  isCompatibleWithType
number of ops:  21
compiled vars:  !0 = $type, !1 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   16     2        GET_TYPE                                             ~2      !1
          3        IS_IDENTICAL                                                 ~2, 'object'
          4      > JMPZ                                                         ~3, ->15
   17     5    >   GET_CLASS                                            ~4      !1
          6        IS_IDENTICAL                                         ~5      !0, ~4
          7      > JMPNZ_EX                                             ~5      ~5, ->13
          8    >   INIT_FCALL                                                   'is_subclass_of'
          9        SEND_VAR                                                     !1
         10        SEND_VAR                                                     !0
         11        DO_ICALL                                             $6      
         12        BOOL                                                 ~5      $6
         13    >   VERIFY_RETURN_TYPE                                           ~5
         14      > RETURN                                                       ~5
   20    15    >   GET_TYPE                                             ~7      !1
         16        IS_IDENTICAL                                         ~8      !0, ~7
         17        VERIFY_RETURN_TYPE                                           ~8
         18      > RETURN                                                       ~8
   21    19*       VERIFY_RETURN_TYPE                                           
         20*     > RETURN                                                       null

End of function iscompatiblewithtype

Function canaccept:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 85
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 85
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 17
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 47
Branch analysis from position: 22
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 44
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 44
Branch analysis from position: 26
2 jumps found. (Code = 47) Position 1 = 33, Position 2 = 39
Branch analysis from position: 33
2 jumps found. (Code = 46) Position 1 = 36, Position 2 = 38
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 40, Position 2 = 43
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 38
Branch analysis from position: 39
Branch analysis from position: 44
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 44
Branch analysis from position: 47
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 68
Branch analysis from position: 49
2 jumps found. (Code = 77) Position 1 = 52, Position 2 = 65
Branch analysis from position: 52
2 jumps found. (Code = 78) Position 1 = 53, Position 2 = 65
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 61, Position 2 = 64
Branch analysis from position: 61
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 65
Branch analysis from position: 68
2 jumps found. (Code = 47) Position 1 = 75, Position 2 = 81
Branch analysis from position: 75
2 jumps found. (Code = 46) Position 1 = 78, Position 2 = 80
Branch analysis from position: 78
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 80
Branch analysis from position: 81
Branch analysis from position: 85
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 85
filename:       /in/6AnNZ
function name:  canAccept
number of ops:  89
compiled vars:  !0 = $func, !1 = $paramPosition, !2 = $paramValue, !3 = $r, !4 = $canAccept, !5 = $parameter, !6 = $type, !7 = $t
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   24     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV                                                 !2      
   25     3        NEW                                                  $8      'ReflectionFunction'
          4        SEND_VAR_EX                                                  !0
          5        DO_FCALL                                          0          
          6        ASSIGN                                                       !3, $8
   26     7        ASSIGN                                                       !4, <false>
   29     8        INIT_METHOD_CALL                                             !3, 'getParameters'
          9        DO_FCALL                                          0  $12     
         10      > FE_RESET_R                                           $13     $12, ->85
         11    > > FE_FETCH_R                                                   $13, !5, ->85
   30    12    >   INIT_METHOD_CALL                                             !5, 'getPosition'
         13        DO_FCALL                                          0  $14     
         14        IS_NOT_IDENTICAL                                             !1, $14
         15      > JMPZ                                                         ~15, ->17
   31    16    > > JMP                                                          ->11
   34    17    >   INIT_METHOD_CALL                                             !5, 'getType'
         18        DO_FCALL                                          0  $16     
         19        ASSIGN                                                       !6, $16
   35    20        INSTANCEOF                                                   !6, 'ReflectionUnionType'
         21      > JMPZ                                                         ~18, ->47
   36    22    >   INIT_METHOD_CALL                                             !6, 'getTypes'
         23        DO_FCALL                                          0  $19     
         24      > FE_RESET_R                                           $20     $19, ->44
         25    > > FE_FETCH_R                                                   $20, !7, ->44
   37    26    >   INIT_FCALL                                                   'iscompatiblewithtype'
         27        INIT_METHOD_CALL                                             !7, 'getName'
         28        DO_FCALL                                          0  $21     
         29        SEND_VAR                                                     $21
         30        SEND_VAR                                                     !2
         31        DO_FCALL                                          0  $22     
         32      > JMPNZ_EX                                             ~23     $22, ->39
         33    >   INIT_METHOD_CALL                                             !7, 'allowsNull'
         34        DO_FCALL                                          0  $24     
         35      > JMPZ_EX                                              ~25     $24, ->38
         36    >   TYPE_CHECK                                        2  ~26     !2
         37        BOOL                                                 ~25     ~26
         38    >   BOOL                                                 ~23     ~25
         39    > > JMPZ                                                         ~23, ->43
   38    40    >   FE_FREE                                                      $20
         41        FE_FREE                                                      $13
         42      > RETURN                                                       <true>
   36    43    > > JMP                                                          ->25
         44    >   FE_FREE                                                      $20
   42    45        FE_FREE                                                      $13
         46      > RETURN                                                       <false>
   45    47    >   INSTANCEOF                                                   !6, 'ReflectionIntersectionType'
         48      > JMPZ                                                         ~27, ->68
   46    49    >   INIT_METHOD_CALL                                             !6, 'getTypes'
         50        DO_FCALL                                          0  $28     
         51      > FE_RESET_R                                           $29     $28, ->65
         52    > > FE_FETCH_R                                                   $29, !7, ->65
   47    53    >   INIT_FCALL                                                   'iscompatiblewithtype'
         54        INIT_METHOD_CALL                                             !7, 'getName'
         55        DO_FCALL                                          0  $30     
         56        SEND_VAR                                                     $30
         57        SEND_VAR                                                     !2
         58        DO_FCALL                                          0  $31     
         59        BOOL_NOT                                             ~32     $31
         60      > JMPZ                                                         ~32, ->64
   48    61    >   FE_FREE                                                      $29
         62        FE_FREE                                                      $13
         63      > RETURN                                                       <false>
   46    64    > > JMP                                                          ->52
         65    >   FE_FREE                                                      $29
   52    66        FE_FREE                                                      $13
         67      > RETURN                                                       <true>
   55    68    >   INIT_FCALL                                                   'iscompatiblewithtype'
         69        INIT_METHOD_CALL                                             !6, 'getName'
         70        DO_FCALL                                          0  $33     
         71        SEND_VAR                                                     $33
         72        SEND_VAR                                                     !2
         73        DO_FCALL                                          0  $34     
         74      > JMPNZ_EX                                             ~35     $34, ->81
         75    >   INIT_METHOD_CALL                                             !7, 'allowsNull'
         76        DO_FCALL                                          0  $36     
         77      > JMPZ_EX                                              ~37     $36, ->80
         78    >   TYPE_CHECK                                        2  ~38     !2
         79        BOOL                                                 ~37     ~38
         80    >   BOOL                                                 ~35     ~37
         81    >   VERIFY_RETURN_TYPE                                           ~35
         82        FE_FREE                                                      $13
         83      > RETURN                                                       ~35
   29    84*       JMP                                                          ->11
         85    >   FE_FREE                                                      $13
   58    86      > RETURN                                                       <false>
   59    87*       VERIFY_RETURN_TYPE                                           
         88*     > RETURN                                                       null

End of function canaccept

Class X: [no user functions]
Class Y: [no user functions]
Class Z: [no user functions]
Class U: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
130.05 ms | 494 KiB | 14 Q