3v4l.org

run code in 500+ PHP versions simultaneously
<?php function decorate(array $decorators, callable $fn, array $arguments = array()) { foreach (array_reverse($decorators) as $decorator) { if (is_callable($decorator)) { $fn = $decorator($fn); } } return call_user_func_array($fn, $arguments); } function expects(/* $type,... */) { $types = func_get_args(); $decorator = function(callable $fn) use ($types) { $wrapper = function() use ($fn, $types) { foreach (func_get_args() as $i => $arg) { if (($type = gettype($arg)) !== $types[$i] and isset($types[$i]) and $types[$i] !== 'mixed') { throw new \InvalidArgumentException("Expected type of argument $i to be {$types[$i]}, got $type"); } } return call_user_func_array($fn, func_get_args()); }; return $wrapper; }; return $decorator; } function returns($type) { $decorator = function($fn) use ($type) { $wrapper = function() use ($fn, $type) { $returnValue = call_user_func_array($fn, func_get_args()); if (($returnType = gettype($returnValue)) !== $type) { throw new \UnexpectedValueException("Expected return type $type, got $returnType"); } return $returnValue; }; return $wrapper; }; return $decorator; } function bar() { return decorate( [ expects('integer'), returns('string') ], function($foo) { return (array) "foo"; }, func_get_args() ); } bar(0); bar("foo");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   68     0  E >   INIT_FCALL                                                   'bar'
          1        SEND_VAL                                                     0
          2        DO_FCALL                                          0          
   69     3        INIT_FCALL                                                   'bar'
          4        SEND_VAL                                                     'foo'
          5        DO_FCALL                                          0          
          6      > RETURN                                                       1

Function decorate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 17
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 17
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 16
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/Q002f
function name:  decorate
number of ops:  24
compiled vars:  !0 = $decorators, !1 = $fn, !2 = $arguments, !3 = $decorator
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        RECV_INIT                                            !2      <array>
    5     3        INIT_FCALL                                                   'array_reverse'
          4        SEND_VAR                                                     !0
          5        DO_ICALL                                             $4      
          6      > FE_RESET_R                                           $5      $4, ->17
          7    > > FE_FETCH_R                                                   $5, !3, ->17
    6     8    >   INIT_FCALL                                                   'is_callable'
          9        SEND_VAR                                                     !3
         10        DO_ICALL                                             $6      
         11      > JMPZ                                                         $6, ->16
    7    12    >   INIT_DYNAMIC_CALL                                            !3
         13        SEND_VAR_EX                                                  !1
         14        DO_FCALL                                          0  $7      
         15        ASSIGN                                                       !1, $7
    5    16    > > JMP                                                          ->7
         17    >   FE_FREE                                                      $5
   11    18        INIT_USER_CALL                                    0          'call_user_func_array', !1
         19        SEND_ARRAY                                                   !2
         20        CHECK_UNDEF_ARGS                                             
         21        DO_FCALL                                          1  $9      
         22      > RETURN                                                       $9
   12    23*     > RETURN                                                       null

End of function decorate

Function expects:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  expects
number of ops:  7
compiled vars:  !0 = $types, !1 = $decorator
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   16     0  E >   FUNC_GET_ARGS                                        ~2      
          1        ASSIGN                                                       !0, ~2
   18     2        DECLARE_LAMBDA_FUNCTION                              ~4      [0]
          3        BIND_LEXICAL                                                 ~4, !0
          4        ASSIGN                                                       !1, ~4
   32     5      > RETURN                                                       !1
   33     6*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  {closure:expects():18}
number of ops:  8
compiled vars:  !0 = $fn, !1 = $types, !2 = $wrapper
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   18     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   19     2        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          3        BIND_LEXICAL                                                 ~3, !0
          4        BIND_LEXICAL                                                 ~3, !1
          5        ASSIGN                                                       !2, ~3
   29     6      > RETURN                                                       !2
   30     7*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 30
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 30
Branch analysis from position: 5
2 jumps found. (Code = 46) Position 1 = 11, Position 2 = 13
Branch analysis from position: 11
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 29
Branch analysis from position: 18
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 17
Branch analysis from position: 13
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/Q002f
function name:  {closure:{closure:expects():18}:19}
number of ops:  38
compiled vars:  !0 = $fn, !1 = $types, !2 = $arg, !3 = $i, !4 = $type
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   19     0  E >   BIND_STATIC                                                  !0
          1        BIND_STATIC                                                  !1
   20     2        FUNC_GET_ARGS                                        ~5      
          3      > FE_RESET_R                                           $6      ~5, ->30
          4    > > FE_FETCH_R                                           ~7      $6, !2, ->30
          5    >   ASSIGN                                                       !3, ~7
   21     6        GET_TYPE                                             ~9      !2
          7        ASSIGN                                               ~10     !4, ~9
          8        FETCH_DIM_R                                          ~11     !1, !3
          9        IS_NOT_IDENTICAL                                     ~12     ~10, ~11
         10      > JMPZ_EX                                              ~12     ~12, ->13
         11    >   ISSET_ISEMPTY_DIM_OBJ                             0  ~13     !1, !3
         12        BOOL                                                 ~12     ~13
         13    > > JMPZ_EX                                              ~12     ~12, ->17
         14    >   FETCH_DIM_R                                          ~14     !1, !3
         15        IS_NOT_IDENTICAL                                     ~15     ~14, 'mixed'
         16        BOOL                                                 ~12     ~15
         17    > > JMPZ                                                         ~12, ->29
   22    18    >   NEW                                                  $16     'InvalidArgumentException'
         19        ROPE_INIT                                         6  ~19     'Expected+type+of+argument+'
         20        ROPE_ADD                                          1  ~19     ~19, !3
         21        ROPE_ADD                                          2  ~19     ~19, '+to+be+'
         22        FETCH_DIM_R                                          ~17     !1, !3
         23        ROPE_ADD                                          3  ~19     ~19, ~17
         24        ROPE_ADD                                          4  ~19     ~19, '%2C+got+'
         25        ROPE_END                                          5  ~18     ~19, !4
         26        SEND_VAL_EX                                                  ~18
         27        DO_FCALL                                          0          
         28      > THROW                                             0          $16
   20    29    > > JMP                                                          ->4
         30    >   FE_FREE                                                      $6
   26    31        INIT_USER_CALL                                    0          'call_user_func_array', !0
         32        FUNC_GET_ARGS                                        ~23     
         33        SEND_ARRAY                                                   ~23
         34        CHECK_UNDEF_ARGS                                             
         35        DO_FCALL                                          1  $24     
         36      > RETURN                                                       $24
   27    37*     > RETURN                                                       null

End of Dynamic Function 0

End of Dynamic Function 0

End of function expects

Function returns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  returns
number of ops:  6
compiled vars:  !0 = $type, !1 = $decorator
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   35     0  E >   RECV                                                 !0      
   37     1        DECLARE_LAMBDA_FUNCTION                              ~2      [0]
          2        BIND_LEXICAL                                                 ~2, !0
          3        ASSIGN                                                       !1, ~2
   51     4      > RETURN                                                       !1
   52     5*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  {closure:returns():37}
number of ops:  8
compiled vars:  !0 = $fn, !1 = $type, !2 = $wrapper
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   37     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
   38     2        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
          3        BIND_LEXICAL                                                 ~3, !0
          4        BIND_LEXICAL                                                 ~3, !1
          5        ASSIGN                                                       !2, ~3
   48     6      > RETURN                                                       !2
   49     7*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 20
Branch analysis from position: 12
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 20
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  {closure:{closure:returns():37}:38}
number of ops:  22
compiled vars:  !0 = $fn, !1 = $type, !2 = $returnValue, !3 = $returnType
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   38     0  E >   BIND_STATIC                                                  !0
          1        BIND_STATIC                                                  !1
   39     2        INIT_USER_CALL                                    0          'call_user_func_array', !0
          3        FUNC_GET_ARGS                                        ~4      
          4        SEND_ARRAY                                                   ~4
          5        CHECK_UNDEF_ARGS                                             
          6        DO_FCALL                                          1  $5      
          7        ASSIGN                                                       !2, $5
   41     8        GET_TYPE                                             ~7      !2
          9        ASSIGN                                               ~8      !3, ~7
         10        IS_NOT_IDENTICAL                                             !1, ~8
         11      > JMPZ                                                         ~9, ->20
   42    12    >   NEW                                                  $10     'UnexpectedValueException'
         13        ROPE_INIT                                         4  ~12     'Expected+return+type+'
         14        ROPE_ADD                                          1  ~12     ~12, !1
         15        ROPE_ADD                                          2  ~12     ~12, '%2C+got+'
         16        ROPE_END                                          3  ~11     ~12, !3
         17        SEND_VAL_EX                                                  ~11
         18        DO_FCALL                                          0          
         19      > THROW                                             0          $10
   45    20    > > RETURN                                                       !2
   46    21*     > RETURN                                                       null

End of Dynamic Function 0

End of Dynamic Function 0

End of function returns

Function bar:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  bar
number of ops:  17
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   56     0  E >   INIT_FCALL                                                   'decorate'
   58     1        INIT_FCALL                                                   'expects'
          2        SEND_VAL                                                     'integer'
          3        DO_FCALL                                          0  $0      
          4        INIT_ARRAY                                           ~1      $0
   59     5        INIT_FCALL                                                   'returns'
          6        SEND_VAL                                                     'string'
          7        DO_FCALL                                          0  $2      
          8        ADD_ARRAY_ELEMENT                                    ~1      $2
          9        SEND_VAL                                                     ~1
   61    10        DECLARE_LAMBDA_FUNCTION                              ~3      [0]
   63    11        SEND_VAL                                                     ~3
   64    12        FUNC_GET_ARGS                                        ~4      
         13        SEND_VAL                                                     ~4
   56    14        DO_FCALL                                          0  $5      
   64    15      > RETURN                                                       $5
   66    16*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/Q002f
function name:  {closure:bar():61}
number of ops:  4
compiled vars:  !0 = $foo
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   61     0  E >   RECV                                                 !0      
   62     1        CAST                                              7  ~1      'foo'
          2      > RETURN                                                       ~1
   63     3*     > RETURN                                                       null

End of Dynamic Function 0

End of function bar

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
176.55 ms | 2892 KiB | 20 Q