3v4l.org

run code in 500+ PHP versions simultaneously
<?php class User { public function __construct( private Base $base, ) { } public function run() { $return = $this->base->f(42); var_dump($return); } } readonly class Base { public function __construct( public int $i, ) { } public function f(int $i): string { echo $i; return 'hoge'; } } class ExpectationException extends \Exception {} readonly class Mock extends Base { public function __construct( private ExpectationStorage $storage = new ExpectationStorage, ) { } public function f(...$args): string { if ($args !== $this->storage->getArgs(__FUNCTION__)) { throw new ExpectationException; } if ($this->storage->hasReturns(__FUNCTION__)) { return $this->storage->getReturns(__FUNCTION__); } } public function expects(): ExpectationBuilder { return new ExpectationBuilder($this->storage); } } class ExpectationBuilder { public function __construct( private ExpectationStorage $storage, ) { } public function __call(string $function_name, $args) { $this->storage->setArgs($function_name, $args); return new FunctionExpectationBuilder($function_name, $this->storage); } } class FunctionExpectationBuilder { public function __construct( private string $function_name, private ExpectationStorage $storage, ) { } public function andReturns($value) { $this->storage->setReturns($this->function_name, $value); return $this; } } class ExpectationStorage { private array $args_expectations = []; private array $returns_expectations = []; public function setArgs(string $function_name, $args) { $this->args_expectations[$function_name] = $args; } public function getArgs(string $function_name) { return $this->args_expectations[$function_name]; } public function setReturns(string $function_name, $value) { $this->returns_expectations[$function_name] = $value; } public function getReturns(string $function_name) { return $this->returns_expectations[$function_name]; } public function hasReturns(string $function_name) { return isset($this->returns_expectations[$function_name]); } } $mock = new Mock(); $mock->expects()->f(42)->andReturns('nya-n'); (new User($mock))->run(); $mock = new Mock(); $mock->expects()->f(128)->andReturns('nya-n'); (new User($mock))->run();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  (null)
number of ops:  33
compiled vars:  !0 = $mock
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  101     0  E >   NEW                                                  $1      'Mock'
          1        DO_FCALL                                          0          
          2        ASSIGN                                                       !0, $1
  102     3        INIT_METHOD_CALL                                             !0, 'expects'
          4        DO_FCALL                                          0  $4      
          5        INIT_METHOD_CALL                                             $4, 'f'
          6        SEND_VAL_EX                                                  42
          7        DO_FCALL                                          0  $5      
          8        INIT_METHOD_CALL                                             $5, 'andReturns'
          9        SEND_VAL_EX                                                  'nya-n'
         10        DO_FCALL                                          0          
  104    11        NEW                                                  $7      'User'
         12        SEND_VAR_EX                                                  !0
         13        DO_FCALL                                          0          
         14        INIT_METHOD_CALL                                             $7, 'run'
         15        DO_FCALL                                          0          
  107    16        NEW                                                  $10     'Mock'
         17        DO_FCALL                                          0          
         18        ASSIGN                                                       !0, $10
  108    19        INIT_METHOD_CALL                                             !0, 'expects'
         20        DO_FCALL                                          0  $13     
         21        INIT_METHOD_CALL                                             $13, 'f'
         22        SEND_VAL_EX                                                  128
         23        DO_FCALL                                          0  $14     
         24        INIT_METHOD_CALL                                             $14, 'andReturns'
         25        SEND_VAL_EX                                                  'nya-n'
         26        DO_FCALL                                          0          
  110    27        NEW                                                  $16     'User'
         28        SEND_VAR_EX                                                  !0
         29        DO_FCALL                                          0          
         30        INIT_METHOD_CALL                                             $16, 'run'
         31        DO_FCALL                                          0          
         32      > RETURN                                                       1

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

End of function __construct

Function run:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  run
number of ops:  9
compiled vars:  !0 = $return
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   FETCH_OBJ_R                                          ~1      'base'
          1        INIT_METHOD_CALL                                             ~1, 'f'
          2        SEND_VAL_EX                                                  42
          3        DO_FCALL                                          0  $2      
          4        ASSIGN                                                       !0, $2
   11     5        INIT_FCALL                                                   'var_dump'
          6        SEND_VAR                                                     !0
          7        DO_ICALL                                                     
   12     8      > RETURN                                                       null

End of function run

End of class User.

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

End of function __construct

Function f:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  f
number of ops:  5
compiled vars:  !0 = $i
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
   23     1        ECHO                                                         !0
   24     2      > RETURN                                                       'hoge'
   25     3*       VERIFY_RETURN_TYPE                                           
          4*     > RETURN                                                       null

End of function f

End of class Base.

Class ExpectationException: [no user functions]
Class Mock:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  __construct
number of ops:  4
compiled vars:  !0 = $storage
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   32     0  E >   RECV_INIT                                            !0      <const ast>
          1        ASSIGN_OBJ                                                   'storage'
          2        OP_DATA                                                      !0
   34     3      > RETURN                                                       null

End of function __construct

Function f:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 21
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  f
number of ops:  23
compiled vars:  !0 = $args
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   36     0  E >   RECV_VARIADIC                                        !0      
   37     1        FETCH_OBJ_R                                          ~1      'storage'
          2        INIT_METHOD_CALL                                             ~1, 'getArgs'
          3        SEND_VAL_EX                                                  'f'
          4        DO_FCALL                                          0  $2      
          5        IS_NOT_IDENTICAL                                             !0, $2
          6      > JMPZ                                                         ~3, ->10
   38     7    >   NEW                                                  $4      'ExpectationException'
          8        DO_FCALL                                          0          
          9      > THROW                                             0          $4
   40    10    >   FETCH_OBJ_R                                          ~6      'storage'
         11        INIT_METHOD_CALL                                             ~6, 'hasReturns'
         12        SEND_VAL_EX                                                  'f'
         13        DO_FCALL                                          0  $7      
         14      > JMPZ                                                         $7, ->21
   41    15    >   FETCH_OBJ_R                                          ~8      'storage'
         16        INIT_METHOD_CALL                                             ~8, 'getReturns'
         17        SEND_VAL_EX                                                  'f'
         18        DO_FCALL                                          0  $9      
         19        VERIFY_RETURN_TYPE                                           $9
         20      > RETURN                                                       $9
   43    21    >   VERIFY_RETURN_TYPE                                           
         22      > RETURN                                                       null

End of function f

Function expects:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  expects
number of ops:  9
compiled vars:  none
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   46     0  E >   NEW                                                  $0      'ExpectationBuilder'
          1        CHECK_FUNC_ARG                                               
          2        FETCH_OBJ_FUNC_ARG                                   $1      'storage'
          3        SEND_FUNC_ARG                                                $1
          4        DO_FCALL                                          0          
          5        VERIFY_RETURN_TYPE                                           $0
          6      > RETURN                                                       $0
   47     7*       VERIFY_RETURN_TYPE                                           
          8*     > RETURN                                                       null

End of function expects

End of class Mock.

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

End of function __construct

Function __call:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  __call
number of ops:  15
compiled vars:  !0 = $function_name, !1 = $args
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   57     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   58     2        FETCH_OBJ_R                                          ~2      'storage'
          3        INIT_METHOD_CALL                                             ~2, 'setArgs'
          4        SEND_VAR_EX                                                  !0
          5        SEND_VAR_EX                                                  !1
          6        DO_FCALL                                          0          
   59     7        NEW                                                  $4      'FunctionExpectationBuilder'
          8        SEND_VAR_EX                                                  !0
          9        CHECK_FUNC_ARG                                               
         10        FETCH_OBJ_FUNC_ARG                                   $5      'storage'
         11        SEND_FUNC_ARG                                                $5
         12        DO_FCALL                                          0          
         13      > RETURN                                                       $4
   60    14*     > RETURN                                                       null

End of function __call

End of class ExpectationBuilder.

Class FunctionExpectationBuilder:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  __construct
number of ops:  7
compiled vars:  !0 = $function_name, !1 = $storage
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   66     0  E >   RECV                                                 !0      
   67     1        RECV                                                 !1      
   66     2        ASSIGN_OBJ                                                   'function_name'
          3        OP_DATA                                                      !0
   67     4        ASSIGN_OBJ                                                   'storage'
          5        OP_DATA                                                      !1
   69     6      > RETURN                                                       null

End of function __construct

Function andreturns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  andReturns
number of ops:  11
compiled vars:  !0 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   71     0  E >   RECV                                                 !0      
   72     1        FETCH_OBJ_R                                          ~1      'storage'
          2        INIT_METHOD_CALL                                             ~1, 'setReturns'
          3        CHECK_FUNC_ARG                                               
          4        FETCH_OBJ_FUNC_ARG                                   $2      'function_name'
          5        SEND_FUNC_ARG                                                $2
          6        SEND_VAR_EX                                                  !0
          7        DO_FCALL                                          0          
   73     8        FETCH_THIS                                           ~4      
          9      > RETURN                                                       ~4
   74    10*     > RETURN                                                       null

End of function andreturns

End of class FunctionExpectationBuilder.

Class ExpectationStorage:
Function setargs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  setArgs
number of ops:  6
compiled vars:  !0 = $function_name, !1 = $args
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   83     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   84     2        FETCH_OBJ_W                                          $2      'args_expectations'
          3        ASSIGN_DIM                                                   $2, !0
          4        OP_DATA                                                      !1
   85     5      > RETURN                                                       null

End of function setargs

Function getargs:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  getArgs
number of ops:  5
compiled vars:  !0 = $function_name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   86     0  E >   RECV                                                 !0      
   87     1        FETCH_OBJ_R                                          ~1      'args_expectations'
          2        FETCH_DIM_R                                          ~2      ~1, !0
          3      > RETURN                                                       ~2
   88     4*     > RETURN                                                       null

End of function getargs

Function setreturns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  setReturns
number of ops:  6
compiled vars:  !0 = $function_name, !1 = $value
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   90     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   91     2        FETCH_OBJ_W                                          $2      'returns_expectations'
          3        ASSIGN_DIM                                                   $2, !0
          4        OP_DATA                                                      !1
   92     5      > RETURN                                                       null

End of function setreturns

Function getreturns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  getReturns
number of ops:  5
compiled vars:  !0 = $function_name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   93     0  E >   RECV                                                 !0      
   94     1        FETCH_OBJ_R                                          ~1      'returns_expectations'
          2        FETCH_DIM_R                                          ~2      ~1, !0
          3      > RETURN                                                       ~2
   95     4*     > RETURN                                                       null

End of function getreturns

Function hasreturns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/bivtj
function name:  hasReturns
number of ops:  5
compiled vars:  !0 = $function_name
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   96     0  E >   RECV                                                 !0      
   97     1        FETCH_OBJ_IS                                         ~1      'returns_expectations'
          2        ISSET_ISEMPTY_DIM_OBJ                             0  ~2      ~1, !0
          3      > RETURN                                                       ~2
   98     4*     > RETURN                                                       null

End of function hasreturns

End of class ExpectationStorage.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
161.32 ms | 1557 KiB | 14 Q