3v4l.org

run code in 300+ PHP versions simultaneously
<?php define('TYPEHINT_PCRE', '/^Argument (\d)+ passed to (?:(\w+)::)?(\w+)\(\) must be an instance of (\w+), (\w+) given/'); class Typehint { private static $Typehints = array( 'boolean' => 'is_bool', 'int' => 'is_int', 'integer' => 'is_int', 'float' => 'is_float', 'string' => 'is_string', 'resrouce' => 'is_resource' ); private function __Constrct() {} public static function initializeHandler() { set_error_handler('Typehint::handleTypehint', E_ALL); return TRUE; } private static function getTypehintedArgument($ThBackTrace, $ThFunction, $ThArgIndex, &$ThArgValue) { foreach ($ThBackTrace as $ThTrace) { // Match the function; Note we could do more defensive error checking. if (isset($ThTrace['function']) && $ThTrace['function'] == $ThFunction) { $ThArgValue = $ThTrace['args'][$ThArgIndex - 1]; return TRUE; } } return FALSE; } public static function handleTypehint($ErrLevel, $ErrMessage, $ErrFile, $ErrLine) { if ($ErrLevel == E_RECOVERABLE_ERROR) { if (preg_match(TYPEHINT_PCRE, $ErrMessage, $ErrMatches)) { list($ErrMatch, $ThArgIndex, $ThClass, $ThFunction, $ThHint, $ThType) = $ErrMatches; if (isset(self::$Typehints[$ThHint])) { $ThBacktrace = debug_backtrace(); $ThArgValue = NULL; if (self::getTypehintedArgument($ThBacktrace, $ThFunction, $ThArgIndex, $ThArgValue)) { if (call_user_func(self::$Typehints[$ThHint], $ThArgValue)) { return TRUE; } } } } } return false; } } Typehint::initializeHandler(); function test(string $number) { echo $number; } test("5"); test(5);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/19UcH
function name:  (null)
number of ops:  13
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   INIT_FCALL                                               'define'
          1        SEND_VAL                                                 'TYPEHINT_PCRE'
          2        SEND_VAL                                                 '%2F%5EArgument+%28%5Cd%29%2B+passed+to+%28%3F%3A%28%5Cw%2B%29%3A%3A%29%3F%28%5Cw%2B%29%5C%28%5C%29+must+be+an+instance+of+%28%5Cw%2B%29%2C+%28%5Cw%2B%29+given%2F'
          3        DO_ICALL                                                 
   80     4        INIT_STATIC_METHOD_CALL                                  'Typehint', 'initializeHandler'
          5        DO_FCALL                                      0          
   87     6        INIT_FCALL                                               'test'
          7        SEND_VAL                                                 '5'
          8        DO_FCALL                                      0          
   88     9        INIT_FCALL                                               'test'
         10        SEND_VAL                                                 5
         11        DO_FCALL                                      0          
         12      > RETURN                                                   1

Function test:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/19UcH
function name:  test
number of ops:  3
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
   84     1        ECHO                                                     !0
   85     2      > RETURN                                                   null

End of function test

Class Typehint:
Function __constrct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/19UcH
function name:  __Constrct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E > > RETURN                                                   null

End of function __constrct

Function initializehandler:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/19UcH
function name:  initializeHandler
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   INIT_FCALL                                               'set_error_handler'
          1        SEND_VAL                                                 'Typehint%3A%3AhandleTypehint'
          2        SEND_VAL                                                 32767
          3        DO_ICALL                                                 
   24     4      > RETURN                                                   <true>
   25     5*     > RETURN                                                   null

End of function initializehandler

Function gettypehintedargument:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 19
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 19
Branch analysis from position: 6
2 jumps found. (Code = 46) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 18
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 11
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 19
filename:       /in/19UcH
function name:  getTypehintedArgument
number of ops:  22
compiled vars:  !0 = $ThBackTrace, !1 = $ThFunction, !2 = $ThArgIndex, !3 = $ThArgValue, !4 = $ThTrace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   30     4      > FE_RESET_R                                       $5      !0, ->19
          5    > > FE_FETCH_R                                               $5, !4, ->19
   34     6    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~6      !4, 'function'
          7      > JMPZ_EX                                          ~6      ~6, ->11
          8    >   FETCH_DIM_R                                      ~7      !4, 'function'
          9        IS_EQUAL                                         ~8      !1, ~7
         10        BOOL                                             ~6      ~8
         11    > > JMPZ                                                     ~6, ->18
   37    12    >   SUB                                              ~10     !2, 1
         13        FETCH_DIM_R                                      ~9      !4, 'args'
         14        FETCH_DIM_R                                      ~11     ~9, ~10
         15        ASSIGN                                                   !3, ~11
   39    16        FE_FREE                                                  $5
         17      > RETURN                                                   <true>
   30    18    > > JMP                                                      ->5
         19    >   FE_FREE                                                  $5
   43    20      > RETURN                                                   <false>
   44    21*     > RETURN                                                   null

End of function gettypehintedargument

Function handletypehint:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 48
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 48
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 30, Position 2 = 48
Branch analysis from position: 30
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 48
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 48
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
Branch analysis from position: 48
Branch analysis from position: 48
Branch analysis from position: 48
filename:       /in/19UcH
function name:  handleTypehint
number of ops:  50
compiled vars:  !0 = $ErrLevel, !1 = $ErrMessage, !2 = $ErrFile, !3 = $ErrLine, !4 = $ErrMatches, !5 = $ErrMatch, !6 = $ThArgIndex, !7 = $ThClass, !8 = $ThFunction, !9 = $ThHint, !10 = $ThType, !11 = $ThBacktrace, !12 = $ThArgValue
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   46     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   49     4        IS_EQUAL                                                 !0, 4096
          5      > JMPZ                                                     ~13, ->48
   52     6    >   INIT_FCALL                                               'preg_match'
          7        FETCH_CONSTANT                                   ~14     'TYPEHINT_PCRE'
          8        SEND_VAL                                                 ~14
          9        SEND_VAR                                                 !1
         10        SEND_REF                                                 !4
         11        DO_ICALL                                         $15     
         12      > JMPZ                                                     $15, ->48
   55    13    >   QM_ASSIGN                                        ~16     !4
         14        FETCH_LIST_R                                     $17     ~16, 0
         15        ASSIGN                                                   !5, $17
         16        FETCH_LIST_R                                     $19     ~16, 1
         17        ASSIGN                                                   !6, $19
         18        FETCH_LIST_R                                     $21     ~16, 2
         19        ASSIGN                                                   !7, $21
         20        FETCH_LIST_R                                     $23     ~16, 3
         21        ASSIGN                                                   !8, $23
         22        FETCH_LIST_R                                     $25     ~16, 4
         23        ASSIGN                                                   !9, $25
         24        FETCH_LIST_R                                     $27     ~16, 5
         25        ASSIGN                                                   !10, $27
         26        FREE                                                     ~16
   57    27        FETCH_STATIC_PROP_IS                             ~29     'Typehints'
         28        ISSET_ISEMPTY_DIM_OBJ                         0          ~29, !9
         29      > JMPZ                                                     ~30, ->48
   60    30    >   INIT_FCALL                                               'debug_backtrace'
         31        DO_ICALL                                         $31     
         32        ASSIGN                                                   !11, $31
   61    33        ASSIGN                                                   !12, null
   63    34        INIT_STATIC_METHOD_CALL                                  'getTypehintedArgument'
         35        SEND_VAR                                                 !11
         36        SEND_VAR                                                 !8
         37        SEND_VAR                                                 !6
         38        SEND_REF                                                 !12
         39        DO_FCALL                                      0  $34     
         40      > JMPZ                                                     $34, ->48
   66    41    >   FETCH_STATIC_PROP_R          unknown             ~35     'Typehints'
         42        FETCH_DIM_R                                      ~36     ~35, !9
         43        INIT_USER_CALL                                1          'call_user_func', ~36
         44        SEND_USER                                                !12
         45        DO_FCALL                                      0  $37     
         46      > JMPZ                                                     $37, ->48
   69    47    > > RETURN                                                   <true>
   76    48    > > RETURN                                                   <false>
   77    49*     > RETURN                                                   null

End of function handletypehint

End of class Typehint.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
170.33 ms | 1406 KiB | 23 Q