3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Try it out */ $user = (new Api)->user(1); echo "Got user ".$user->id; function phpdoc_params(ReflectionMethod $method) : array { // Retrieve the full PhpDoc comment block $doc = $method->getDocComment(); // Trim each line from space and star chars $lines = array_map(function($line){ return trim($line, " *"); }, explode("\n", $doc)); // Retain lines that start with an @ $lines = array_filter($lines, function($line){ return strpos($line, "@") === 0; }); $args = []; // Push each value in the corresponding @param array foreach($lines as $line){ list($param, $value) = explode(' ', $line, 2); $args[$param][] = $value; } return $args; } class Api { /** * @param int $id * @return User * @throws ReflectionException * @endpoint users */ public function user(int $id) : User { return $this->request($this->endpoint(__METHOD__), $id); } /** * @param string $method * @return string * @throws ReflectionException */ protected function endpoint(string $method) : string { $reflection = new \ReflectionMethod($method); $params = phpdoc_params($reflection); return $params['@endpoint'][0] ?? null; } /** * @param string $endpoint * @param $param * @return User */ protected function request(string $endpoint, $param) { switch($endpoint){ case 'users': return new User($param); default: throw new InvalidArgumentException("Invalid endpoint"); } } } /** * Class User */ class User { public $id; public function __construct($id) { $this->id = $id; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UuIop
function name:  (null)
number of ops:  10
compiled vars:  !0 = $user
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   NEW                                              $1      'Api'
          1        DO_FCALL                                      0          
          2        INIT_METHOD_CALL                                         $1, 'user'
          3        SEND_VAL_EX                                              1
          4        DO_FCALL                                      0  $3      
          5        ASSIGN                                                   !0, $3
    7     6        FETCH_OBJ_R                                      ~5      !0, 'id'
          7        CONCAT                                           ~6      'Got+user+', ~5
          8        ECHO                                                     ~6
   84     9      > RETURN                                                   1

Function phpdoc_params:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 22, Position 2 = 37
Branch analysis from position: 22
2 jumps found. (Code = 78) Position 1 = 23, Position 2 = 37
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 22
Branch analysis from position: 22
Branch analysis from position: 37
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
filename:       /in/UuIop
function name:  phpdoc_params
number of ops:  42
compiled vars:  !0 = $method, !1 = $doc, !2 = $lines, !3 = $args, !4 = $line, !5 = $param, !6 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    9     0  E >   RECV                                             !0      
   12     1        INIT_METHOD_CALL                                         !0, 'getDocComment'
          2        DO_FCALL                                      0  $7      
          3        ASSIGN                                                   !1, $7
   15     4        INIT_FCALL                                               'array_map'
          5        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FUuIop%3A15%240'
   17     6        SEND_VAL                                                 ~9
          7        INIT_FCALL                                               'explode'
          8        SEND_VAL                                                 '%0A'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $10     
         11        SEND_VAR                                                 $10
         12        DO_ICALL                                         $11     
   15    13        ASSIGN                                                   !2, $11
   20    14        INIT_FCALL                                               'array_filter'
         15        SEND_VAR                                                 !2
         16        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FUuIop%3A20%241'
   22    17        SEND_VAL                                                 ~13
         18        DO_ICALL                                         $14     
   20    19        ASSIGN                                                   !2, $14
   24    20        ASSIGN                                                   !3, <array>
   27    21      > FE_RESET_R                                       $17     !2, ->37
         22    > > FE_FETCH_R                                               $17, !4, ->37
   28    23    >   INIT_FCALL                                               'explode'
         24        SEND_VAL                                                 '+'
         25        SEND_VAR                                                 !4
         26        SEND_VAL                                                 2
         27        DO_ICALL                                         $18     
         28        FETCH_LIST_R                                     $19     $18, 0
         29        ASSIGN                                                   !5, $19
         30        FETCH_LIST_R                                     $21     $18, 1
         31        ASSIGN                                                   !6, $21
         32        FREE                                                     $18
   29    33        FETCH_DIM_W                                      $23     !3, !5
         34        ASSIGN_DIM                                               $23
         35        OP_DATA                                                  !6
   27    36      > JMP                                                      ->22
         37    >   FE_FREE                                                  $17
   32    38        VERIFY_RETURN_TYPE                                       !3
         39      > RETURN                                                   !3
   33    40*       VERIFY_RETURN_TYPE                                       
         41*     > RETURN                                                   null

End of function phpdoc_params

Function %00%7Bclosure%7D%2Fin%2FUuIop%3A15%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UuIop
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $line
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
   16     1        INIT_FCALL                                               'trim'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '+%2A'
          4        DO_ICALL                                         $1      
          5      > RETURN                                                   $1
   17     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FUuIop%3A15%240

Function %00%7Bclosure%7D%2Fin%2FUuIop%3A20%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UuIop
function name:  {closure}
number of ops:  8
compiled vars:  !0 = $line
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   RECV                                             !0      
   21     1        INIT_FCALL                                               'strpos'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 '%40'
          4        DO_ICALL                                         $1      
          5        IS_IDENTICAL                                     ~2      $1, 0
          6      > RETURN                                                   ~2
   22     7*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FUuIop%3A20%241

Class Api:
Function user:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UuIop
function name:  user
number of ops:  12
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   43     0  E >   RECV                                             !0      
   44     1        INIT_METHOD_CALL                                         'request'
          2        INIT_METHOD_CALL                                         'endpoint'
          3        SEND_VAL_EX                                              'Api%3A%3Auser'
          4        DO_FCALL                                      0  $1      
          5        SEND_VAR_NO_REF_EX                                       $1
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0  $2      
          8        VERIFY_RETURN_TYPE                                       $2
          9      > RETURN                                                   $2
   45    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function user

Function endpoint:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UuIop
function name:  endpoint
number of ops:  17
compiled vars:  !0 = $method, !1 = $reflection, !2 = $params
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
   53     1        NEW                                              $3      'ReflectionMethod'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $3
   54     5        INIT_FCALL                                               'phpdoc_params'
          6        SEND_VAR                                                 !1
          7        DO_FCALL                                      0  $6      
          8        ASSIGN                                                   !2, $6
   55     9        FETCH_DIM_IS                                     ~8      !2, '%40endpoint'
         10        FETCH_DIM_IS                                     ~9      ~8, 0
         11        COALESCE                                         ~10     ~9
         12        QM_ASSIGN                                        ~10     null
         13        VERIFY_RETURN_TYPE                                       ~10
         14      > RETURN                                                   ~10
   56    15*       VERIFY_RETURN_TYPE                                       
         16*     > RETURN                                                   null

End of function endpoint

Function request:
Finding entry points
Branch analysis from position: 0
3 jumps found. (Code = 188) Position 1 = 6, Position 2 = 10, Position 3 = 3
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 6
filename:       /in/UuIop
function name:  request
number of ops:  15
compiled vars:  !0 = $endpoint, !1 = $param
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   65     2      > SWITCH_STRING                                            !0, [ 'users':->6, ], ->10
   66     3    >   IS_EQUAL                                                 !0, 'users'
          4      > JMPNZ                                                    ~2, ->6
          5    > > JMP                                                      ->10
   67     6    >   NEW                                              $3      'User'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9      > RETURN                                                   $3
   69    10    >   NEW                                              $5      'InvalidArgumentException'
         11        SEND_VAL_EX                                              'Invalid+endpoint'
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $5
   71    14*     > RETURN                                                   null

End of function request

End of class Api.

Class User:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/UuIop
function name:  __construct
number of ops:  4
compiled vars:  !0 = $id
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   RECV                                             !0      
   82     1        ASSIGN_OBJ                                               'id'
          2        OP_DATA                                                  !0
   83     3      > RETURN                                                   null

End of function __construct

End of class User.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
171.73 ms | 1411 KiB | 24 Q