3v4l.org

run code in 300+ PHP versions simultaneously
<?php function parse_tokens($code) { $ns = ''; $tokens = token_get_all($code); /* Assume token 0 = '<?php' and token 1 = whitespace */ for ($i = 2, $c = count($tokens); $i < $c; ++$i) { if (!is_array($tokens[$i])) { continue; } switch ($tokens[$i][0]) { case T_CLASS: list($class, $i) = $this->parse_class($tokens, $i); if ($class) { $test = $this->instantiate_test($loader, $ns . $class); if ($test) { $this->runner->run_test_case($test); } } break; case T_NAMESPACE: //list($ns, $i) = $this->parse_namespace($tokens, $i, $ns); break; } } } function parse_class($tokens, $i) { /* $i = 'class' and $i+1 = whitespace */ $i += 2; while (!is_array($tokens[$i]) || T_STRING !== $tokens[$i][0]) { ++$i; } $class = $tokens[$i][1]; print "$class\n"; if (0 === stripos($class, 'test')) { return [$class, $i]; } return [false, $i]; } $file = <<<'FILE' class TestTextBefore {} <?php /* class TestComment {} */ class Test {} <<<STRING class TestString {} STRING; class test2 { public function test1() {} public function test2() {} public function test3() {} } class NotATest {} class // valid tokens between the 'class' keyword and the test name /* should be handled correctly */ Test3 {} ?> class TestTestAfter {} FILE; parse_tokens($file);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/MStua
function name:  (null)
number of ops:  5
compiled vars:  !0 = $file
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   ASSIGN                                                   !0, 'class+TestTextBefore+%7B%7D%0A%0A%3C%3Fphp%0A%0A%2F%2A%0Aclass+TestComment+%7B%7D%0A%2A%2F%0A%0Aclass+Test+%7B%7D%0A%0A%3C%3C%3CSTRING%0Aclass+TestString+%7B%7D%0ASTRING%3B%0A%0Aclass+test2+%7B%0A++++public+function+test1%28%29+%7B%7D%0A++++public+function+test2%28%29+%7B%7D%0A++++public+function+test3%28%29+%7B%7D%0A%7D%0A%0Aclass+NotATest+%7B%7D%0A%0Aclass+%2F%2F+valid+tokens+between+the+%27class%27+keyword+and+the+test+name%0A++++++%2F%2A+should+be+handled+correctly+%2A%2F%0ATest3+%7B%7D%0A%0A%3F%3E%0A%0Aclass+TestTestAfter+%7B%7D'
   76     1        INIT_FCALL                                               'parse_tokens'
          2        SEND_VAR                                                 !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   1

Function parse_tokens:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 10
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 15
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 10
Branch analysis from position: 52
Branch analysis from position: 10
Branch analysis from position: 15
2 jumps found. (Code = 44) Position 1 = 19, Position 2 = 22
Branch analysis from position: 19
2 jumps found. (Code = 44) Position 1 = 21, Position 2 = 47
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 10
Branch analysis from position: 52
Branch analysis from position: 10
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 46
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 41, Position 2 = 46
Branch analysis from position: 41
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
Branch analysis from position: 46
Branch analysis from position: 46
filename:       /in/MStua
function name:  parse_tokens
number of ops:  53
compiled vars:  !0 = $code, !1 = $ns, !2 = $tokens, !3 = $i, !4 = $c, !5 = $class, !6 = $test, !7 = $loader
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        ASSIGN                                                   !1, ''
    6     2        INIT_FCALL                                               'token_get_all'
          3        SEND_VAR                                                 !0
          4        DO_ICALL                                         $9      
          5        ASSIGN                                                   !2, $9
    8     6        ASSIGN                                                   !3, 2
          7        COUNT                                            ~12     !2
          8        ASSIGN                                                   !4, ~12
          9      > JMP                                                      ->50
    9    10    >   FETCH_DIM_R                                      ~14     !2, !3
         11        TYPE_CHECK                                  128  ~15     ~14
         12        BOOL_NOT                                         ~16     ~15
         13      > JMPZ                                                     ~16, ->15
   10    14    > > JMP                                                      ->49
   12    15    >   FETCH_DIM_R                                      ~17     !2, !3
         16        FETCH_DIM_R                                      ~18     ~17, 0
         17        CASE                                                     ~18, 366
         18      > JMPNZ                                                    ~19, ->22
         19    >   CASE                                                     ~18, 371
         20      > JMPNZ                                                    ~19, ->47
         21    > > JMP                                                      ->48
   14    22    >   FETCH_THIS                                       $20     
         23        INIT_METHOD_CALL                                         $20, 'parse_class'
         24        SEND_VAR_EX                                              !2
         25        SEND_VAR_EX                                              !3
         26        DO_FCALL                                      0  $21     
         27        FETCH_LIST_R                                     $22     $21, 0
         28        ASSIGN                                                   !5, $22
         29        FETCH_LIST_R                                     $24     $21, 1
         30        ASSIGN                                                   !3, $24
         31        FREE                                                     $21
   15    32      > JMPZ                                                     !5, ->46
   16    33    >   FETCH_THIS                                       $26     
         34        INIT_METHOD_CALL                                         $26, 'instantiate_test'
         35        SEND_VAR_EX                                              !7
         36        CONCAT                                           ~27     !1, !5
         37        SEND_VAL_EX                                              ~27
         38        DO_FCALL                                      0  $28     
         39        ASSIGN                                                   !6, $28
   17    40      > JMPZ                                                     !6, ->46
   18    41    >   FETCH_THIS                                       $30     
         42        FETCH_OBJ_R                                      ~31     $30, 'runner'
         43        INIT_METHOD_CALL                                         ~31, 'run_test_case'
         44        SEND_VAR_EX                                              !6
         45        DO_FCALL                                      0          
   21    46    > > JMP                                                      ->48
   25    47    > > JMP                                                      ->48
         48    >   FREE                                                     ~18
    8    49    >   PRE_INC                                                  !3
         50    >   IS_SMALLER                                               !3, !4
         51      > JMPNZ                                                    ~34, ->10
   28    52    > > RETURN                                                   null

End of function parse_tokens

Function parse_class:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
2 jumps found. (Code = 47) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
2 jumps found. (Code = 44) Position 1 = 14, Position 2 = 4
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 29
Branch analysis from position: 26
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 4
2 jumps found. (Code = 47) Position 1 = 9, Position 2 = 13
Branch analysis from position: 9
Branch analysis from position: 13
Branch analysis from position: 13
filename:       /in/MStua
function name:  parse_class
number of ops:  33
compiled vars:  !0 = $tokens, !1 = $i, !2 = $class
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   32     2        ASSIGN_OP                                     1          !1, 2
   33     3      > JMP                                                      ->5
   34     4    >   PRE_INC                                                  !1
   33     5    >   FETCH_DIM_R                                      ~5      !0, !1
          6        TYPE_CHECK                                  128  ~6      ~5
          7        BOOL_NOT                                         ~7      ~6
          8      > JMPNZ_EX                                         ~7      ~7, ->13
          9    >   FETCH_DIM_R                                      ~8      !0, !1
         10        FETCH_DIM_R                                      ~9      ~8, 0
         11        IS_NOT_IDENTICAL                                 ~10     ~9, 311
         12        BOOL                                             ~7      ~10
         13    > > JMPNZ                                                    ~7, ->4
   36    14    >   FETCH_DIM_R                                      ~11     !0, !1
         15        FETCH_DIM_R                                      ~12     ~11, 1
         16        ASSIGN                                                   !2, ~12
   37    17        NOP                                                      
         18        FAST_CONCAT                                      ~14     !2, '%0A'
         19        ECHO                                                     ~14
   38    20        INIT_FCALL                                               'stripos'
         21        SEND_VAR                                                 !2
         22        SEND_VAL                                                 'test'
         23        DO_ICALL                                         $15     
         24        IS_IDENTICAL                                             $15, 0
         25      > JMPZ                                                     ~16, ->29
   39    26    >   INIT_ARRAY                                       ~17     !2
         27        ADD_ARRAY_ELEMENT                                ~17     !1
         28      > RETURN                                                   ~17
   41    29    >   INIT_ARRAY                                       ~18     <false>
         30        ADD_ARRAY_ELEMENT                                ~18     !1
         31      > RETURN                                                   ~18
   42    32*     > RETURN                                                   null

End of function parse_class

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
142.82 ms | 1411 KiB | 18 Q