3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Fake\BuiltIn\Functions; /** * Executes the given statements using fake built-in functions. * * @param callable $statements Statements to execute. * @return mixed Whatever $statements returns. * @throws \ReflectionException */ function fake(callable $statements) { $function = new \ReflectionFunction($statements); $start_line = $function->getStartLine(); $end_line = $function->getEndLine(); $function_source = implode('', array_slice(file($function->getFileName()), $start_line - 1, $end_line - $start_line + 1)); if (preg_match('/(?<={).*(?=})/s', $function_source, $matches)) { $function_body = $matches[0]; $namespace = __NAMESPACE__; return eval(" namespace $namespace; $function_body "); } throw new \RuntimeException('Failed to execute statements.'); } function strlen($string) { return 'fake result'; } namespace My\Name\Space; use function Fake\BuiltIn\Functions\fake; class MyClass { /** * @throws \ReflectionException */ public function testMethodWithFakeStrlen() { fake(function () { echo strlen('foo'), PHP_EOL; }); } public function testMethodWithoutFakeStrlen() { echo strlen('foo'), PHP_EOL; } } $myObject = new MyClass(); try { $myObject->testMethodWithFakeStrlen(); $myObject->testMethodWithoutFakeStrlen(); } catch (\ReflectionException $e) { // ... }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 8
Branch analysis from position: 8
2 jumps found. (Code = 107) Position 1 = 9, Position 2 = -2
Branch analysis from position: 9
filename:       /in/LriLW
function name:  (null)
number of ops:  10
compiled vars:  !0 = $myObject, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   NEW                                              $2      'My%5CName%5CSpace%5CMyClass'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $2
   64     3        INIT_METHOD_CALL                                         !0, 'testMethodWithFakeStrlen'
          4        DO_FCALL                                      0          
   65     5        INIT_METHOD_CALL                                         !0, 'testMethodWithoutFakeStrlen'
          6        DO_FCALL                                      0          
          7      > JMP                                                      ->9
   66     8  E > > CATCH                                       last         'ReflectionException'
   68     9    > > RETURN                                                   1

Function fake%5Cbuiltin%5Cfunctions%5Cfake:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 45
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/LriLW
function name:  Fake\BuiltIn\Functions\fake
number of ops:  50
compiled vars:  !0 = $statements, !1 = $function, !2 = $start_line, !3 = $end_line, !4 = $function_source, !5 = $matches, !6 = $function_body, !7 = $namespace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
   14     1        NEW                                              $8      'ReflectionFunction'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !1, $8
   16     5        INIT_METHOD_CALL                                         !1, 'getStartLine'
          6        DO_FCALL                                      0  $11     
          7        ASSIGN                                                   !2, $11
   17     8        INIT_METHOD_CALL                                         !1, 'getEndLine'
          9        DO_FCALL                                      0  $13     
         10        ASSIGN                                                   !3, $13
   18    11        INIT_NS_FCALL_BY_NAME                                    'Fake%5CBuiltIn%5CFunctions%5Cimplode'
         12        SEND_VAL_EX                                              ''
   19    13        INIT_NS_FCALL_BY_NAME                                    'Fake%5CBuiltIn%5CFunctions%5Carray_slice'
         14        INIT_NS_FCALL_BY_NAME                                    'Fake%5CBuiltIn%5CFunctions%5Cfile'
         15        INIT_METHOD_CALL                                         !1, 'getFileName'
         16        DO_FCALL                                      0  $15     
         17        SEND_VAR_NO_REF_EX                                       $15
         18        DO_FCALL                                      0  $16     
         19        SEND_VAR_NO_REF_EX                                       $16
         20        SUB                                              ~17     !2, 1
         21        SEND_VAL_EX                                              ~17
         22        SUB                                              ~18     !3, !2
         23        ADD                                              ~19     ~18, 1
         24        SEND_VAL_EX                                              ~19
         25        DO_FCALL                                      0  $20     
         26        SEND_VAR_NO_REF_EX                                       $20
         27        DO_FCALL                                      0  $21     
   18    28        ASSIGN                                                   !4, $21
   21    29        INIT_NS_FCALL_BY_NAME                                    'Fake%5CBuiltIn%5CFunctions%5Cpreg_match'
         30        SEND_VAL_EX                                              '%2F%28%3F%3C%3D%7B%29.%2A%28%3F%3D%7D%29%2Fs'
         31        SEND_VAR_EX                                              !4
         32        SEND_VAR_EX                                              !5
         33        DO_FCALL                                      0  $23     
         34      > JMPZ                                                     $23, ->45
   22    35    >   FETCH_DIM_R                                      ~24     !5, 0
         36        ASSIGN                                                   !6, ~24
   23    37        ASSIGN                                                   !7, 'Fake%5CBuiltIn%5CFunctions'
   25    38        ROPE_INIT                                     5  ~28     '%0A++++++namespace+'
   26    39        ROPE_ADD                                      1  ~28     ~28, !7
         40        ROPE_ADD                                      2  ~28     ~28, '%3B%0A++++++'
   27    41        ROPE_ADD                                      3  ~28     ~28, !6
         42        ROPE_END                                      4  ~27     ~28, '%0A++++'
         43        INCLUDE_OR_EVAL                                  $31     ~27, EVAL
         44      > RETURN                                                   $31
   31    45    >   NEW                                              $32     'RuntimeException'
         46        SEND_VAL_EX                                              'Failed+to+execute+statements.'
         47        DO_FCALL                                      0          
         48      > THROW                                         0          $32
   32    49*     > RETURN                                                   null

End of function fake%5Cbuiltin%5Cfunctions%5Cfake

Function fake%5Cbuiltin%5Cfunctions%5Cstrlen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  Fake\BuiltIn\Functions\strlen
number of ops:  3
compiled vars:  !0 = $string
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   RECV                                             !0      
   36     1      > RETURN                                                   'fake+result'
   37     2*     > RETURN                                                   null

End of function fake%5Cbuiltin%5Cfunctions%5Cstrlen

Function %00my%5Cname%5Cspace%5C%7Bclosure%7D%2Fin%2FLriLW%3A50%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  My\Name\Space\{closure}
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   51     0  E >   INIT_NS_FCALL_BY_NAME                                    'My%5CName%5CSpace%5Cstrlen'
          1        SEND_VAL_EX                                              'foo'
          2        DO_FCALL                                      0  $0      
          3        ECHO                                                     $0
          4        FETCH_CONSTANT                                   ~1      'My%5CName%5CSpace%5CPHP_EOL'
          5        ECHO                                                     ~1
   52     6      > RETURN                                                   null

End of function %00my%5Cname%5Cspace%5C%7Bclosure%7D%2Fin%2FLriLW%3A50%240

Class My\Name\Space\MyClass:
Function testmethodwithfakestrlen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  testMethodWithFakeStrlen
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   50     0  E >   INIT_FCALL                                               'fake%5Cbuiltin%5Cfunctions%5Cfake'
          1        DECLARE_LAMBDA_FUNCTION                                  '%00my%5Cname%5Cspace%5C%7Bclosure%7D%2Fin%2FLriLW%3A50%240'
   52     2        SEND_VAL                                                 ~0
          3        DO_FCALL                                      0          
   53     4      > RETURN                                                   null

End of function testmethodwithfakestrlen

Function testmethodwithoutfakestrlen:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/LriLW
function name:  testMethodWithoutFakeStrlen
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   INIT_NS_FCALL_BY_NAME                                    'My%5CName%5CSpace%5Cstrlen'
          1        SEND_VAL_EX                                              'foo'
          2        DO_FCALL                                      0  $0      
          3        ECHO                                                     $0
          4        FETCH_CONSTANT                                   ~1      'My%5CName%5CSpace%5CPHP_EOL'
          5        ECHO                                                     ~1
   58     6      > RETURN                                                   null

End of function testmethodwithoutfakestrlen

End of class My\Name\Space\MyClass.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
132.51 ms | 1411 KiB | 24 Q