3v4l.org

run code in 300+ PHP versions simultaneously
<?php class SlightlyLessBollocksMySQLi { public function prepareWithParams($query, $params) { $tokens = $this->getQueryTokens($query); $query = ''; $args = ['']; $paramRefs = []; $i = 1; foreach ($tokens as $token) { if ($token !== '?') { $query .= $token; continue; } $paramRefs[$i] = array_shift($params); switch (true) { case is_array($paramRefs[$i]): $query .= implode(', ', array_fill(0, count($paramRefs[$i]), '?')); $tmp = $paramRefs[$i]; array_splice($paramRefs, $i, 1, $tmp); foreach ($tmp as $val) { switch (true) { case is_int($val): $args[0] .= 'i'; break; case is_float($val): $args[0] .= 'd'; break; default: $args[0] .= 's'; break; } } for ($j = $i; isset($paramRefs[$j]); $j++) { $args[] = &$paramRefs[$j]; } $i = $j; break; case is_int($paramRefs[$i]): case is_bool($paramRefs[$i]): $args[0] .= 'i'; $query .= '?'; $paramRefs[$i] = (int)$paramRefs[$i]; $args[] = &$paramRefs[$i++]; break; case is_float($paramRefs[$i]): $args[0] .= 'd'; $query .= '?'; $args[] = &$paramRefs[$i++]; break; default: $args[0] .= 's'; $query .= '?'; $paramRefs[$i] = (string)$paramRefs[$i]; $args[$i] = &$paramRefs[$i++]; break; } } var_dump($query, $args); } /** * @param string $token * @return bool */ private function isQuotedString($token) { return in_array($token[0], array('"', "'")) && $token[0] == $token[strlen($token) - 1]; } /** * "Tokenize" the query * * Actually all this does is split the query up into things that are and are not quoted strings, to avoid treating * a literal question mark as a placeholder. * * @param string $query * @return string[] */ private function getQueryTokens($query) { static $expr = '/("(?:[^"\\\\]*(?:\\\\.[^"\\\\]*)*)"|\'(?:[^\'\\\\]*(?:\\\\.[^\'\\\\]*)*)\')/'; $tokens = []; foreach (preg_split($expr, $query, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $token) { if ($this->isQuotedString($token)) { $tokens[] = $token; } else { $tokens = array_merge($tokens, preg_split('/(\?)/', $token, -1, PREG_SPLIT_DELIM_CAPTURE)); } } return $tokens; } } (new SlightlyLessBollocksMySQLi)->prepareWithParams("SELECT * FROM foo WHERE 'a' = b AND foo = ? AND bar IN(?)", [2, [1, 2, 12.345, "hello", true]]);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/tdIqv
function name:  (null)
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  102     0  E >   NEW                                              $0      'SlightlyLessBollocksMySQLi'
          1        DO_FCALL                                      0          
          2        INIT_METHOD_CALL                                         $0, 'prepareWithParams'
          3        SEND_VAL_EX                                              'SELECT+%2A+FROM+foo+WHERE+%27a%27+%3D+b+AND+foo+%3D+%3F+AND+bar+IN%28%3F%29'
          4        SEND_VAL_EX                                              <array>
          5        DO_FCALL                                      0          
          6      > RETURN                                                   1

Class SlightlyLessBollocksMySQLi:
Function preparewithparams:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 119
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 119
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 16
2 jumps found. (Code = 44) Position 1 = 24, Position 2 = 34
Branch analysis from position: 24
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 83
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 30, Position 2 = 83
Branch analysis from position: 30
2 jumps found. (Code = 44) Position 1 = 33, Position 2 = 96
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 105
Branch analysis from position: 105
1 jumps found. (Code = 42) Position 1 = 118
Branch analysis from position: 118
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 118
Branch analysis from position: 118
Branch analysis from position: 83
1 jumps found. (Code = 42) Position 1 = 118
Branch analysis from position: 118
Branch analysis from position: 83
Branch analysis from position: 34
2 jumps found. (Code = 77) Position 1 = 55, Position 2 = 71
Branch analysis from position: 55
2 jumps found. (Code = 78) Position 1 = 56, Position 2 = 71
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 61
Branch analysis from position: 58
2 jumps found. (Code = 44) Position 1 = 60, Position 2 = 64
Branch analysis from position: 60
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
Branch analysis from position: 64
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 61
1 jumps found. (Code = 42) Position 1 = 70
Branch analysis from position: 70
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
2 jumps found. (Code = 44) Position 1 = 81, Position 2 = 74
Branch analysis from position: 81
1 jumps found. (Code = 42) Position 1 = 118
Branch analysis from position: 118
Branch analysis from position: 74
2 jumps found. (Code = 44) Position 1 = 81, Position 2 = 74
Branch analysis from position: 81
Branch analysis from position: 74
Branch analysis from position: 71
Branch analysis from position: 119
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 119
filename:       /in/tdIqv
function name:  prepareWithParams
number of ops:  125
compiled vars:  !0 = $query, !1 = $params, !2 = $tokens, !3 = $args, !4 = $paramRefs, !5 = $i, !6 = $token, !7 = $tmp, !8 = $val, !9 = $j
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        INIT_METHOD_CALL                                         'getQueryTokens'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $10     
          5        ASSIGN                                                   !2, $10
    8     6        ASSIGN                                                   !0, ''
    9     7        ASSIGN                                                   !3, <array>
   10     8        ASSIGN                                                   !4, <array>
   11     9        ASSIGN                                                   !5, 1
   13    10      > FE_RESET_R                                       $16     !2, ->119
         11    > > FE_FETCH_R                                               $16, !6, ->119
   14    12    >   IS_NOT_IDENTICAL                                         !6, '%3F'
         13      > JMPZ                                                     ~17, ->16
   15    14    >   ASSIGN_OP                                     8          !0, !6
   16    15      > JMP                                                      ->11
   19    16    >   INIT_FCALL                                               'array_shift'
         17        SEND_REF                                                 !1
         18        DO_ICALL                                         $20     
         19        ASSIGN_DIM                                               !4, !5
         20        OP_DATA                                                  $20
   21    21        FETCH_DIM_R                                      ~22     !4, !5
         22        TYPE_CHECK                                  128          ~22
         23      > JMPNZ                                                    ~23, ->34
   42    24    >   FETCH_DIM_R                                      ~24     !4, !5
         25        TYPE_CHECK                                   16          ~24
         26      > JMPNZ                                                    ~25, ->83
   43    27    >   FETCH_DIM_R                                      ~26     !4, !5
         28        TYPE_CHECK                                   12          ~26
         29      > JMPNZ                                                    ~27, ->83
   50    30    >   FETCH_DIM_R                                      ~28     !4, !5
         31        TYPE_CHECK                                   32          ~28
         32      > JMPNZ                                                    ~29, ->96
         33    > > JMP                                                      ->105
   22    34    >   INIT_FCALL                                               'implode'
         35        SEND_VAL                                                 '%2C+'
         36        INIT_FCALL                                               'array_fill'
         37        SEND_VAL                                                 0
         38        FETCH_DIM_R                                      ~30     !4, !5
         39        COUNT                                            ~31     ~30
         40        SEND_VAL                                                 ~31
         41        SEND_VAL                                                 '%3F'
         42        DO_ICALL                                         $32     
         43        SEND_VAR                                                 $32
         44        DO_ICALL                                         $33     
         45        ASSIGN_OP                                     8          !0, $33
   24    46        FETCH_DIM_R                                      ~35     !4, !5
         47        ASSIGN                                                   !7, ~35
   25    48        INIT_FCALL                                               'array_splice'
         49        SEND_REF                                                 !4
         50        SEND_VAR                                                 !5
         51        SEND_VAL                                                 1
         52        SEND_VAR                                                 !7
         53        DO_ICALL                                                 
   27    54      > FE_RESET_R                                       $38     !7, ->71
         55    > > FE_FETCH_R                                               $38, !8, ->71
   29    56    >   TYPE_CHECK                                   16          !8
         57      > JMPNZ                                                    ~40, ->61
   30    58    >   TYPE_CHECK                                   32          !8
         59      > JMPNZ                                                    ~41, ->64
         60    > > JMP                                                      ->67
   29    61    >   ASSIGN_DIM_OP                .=               8          !3, 0
         62        OP_DATA                                                  'i'
         63      > JMP                                                      ->70
   30    64    >   ASSIGN_DIM_OP                .=               8          !3, 0
         65        OP_DATA                                                  'd'
         66      > JMP                                                      ->70
   31    67    >   ASSIGN_DIM_OP                .=               8          !3, 0
         68        OP_DATA                                                  's'
         69      > JMP                                                      ->70
   27    70    > > JMP                                                      ->55
         71    >   FE_FREE                                                  $38
   35    72        ASSIGN                                                   !9, !5
         73      > JMP                                                      ->79
   36    74    >   FETCH_DIM_W                                      $47     !4, !9
         75        MAKE_REF                                         $48     $47
         76        FETCH_DIM_W                                      $46     !3
         77        ASSIGN_REF                                               $46, $48
   35    78        PRE_INC                                                  !9
         79    >   ISSET_ISEMPTY_DIM_OBJ                         0          !4, !9
         80      > JMPNZ                                                    ~51, ->74
   39    81    >   ASSIGN                                                   !5, !9
   40    82      > JMP                                                      ->118
   44    83    >   ASSIGN_DIM_OP                .=               8          !3, 0
         84        OP_DATA                                                  'i'
   45    85        ASSIGN_OP                                     8          !0, '%3F'
   46    86        FETCH_DIM_R                                      ~56     !4, !5
         87        CAST                                          4  ~57     ~56
         88        ASSIGN_DIM                                               !4, !5
         89        OP_DATA                                                  ~57
   47    90        POST_INC                                         ~59     !5
         91        FETCH_DIM_W                                      $60     !4, ~59
         92        MAKE_REF                                         $61     $60
         93        FETCH_DIM_W                                      $58     !3
         94        ASSIGN_REF                                               $58, $61
   48    95      > JMP                                                      ->118
   51    96    >   ASSIGN_DIM_OP                .=               8          !3, 0
         97        OP_DATA                                                  'd'
   52    98        ASSIGN_OP                                     8          !0, '%3F'
   53    99        POST_INC                                         ~66     !5
        100        FETCH_DIM_W                                      $67     !4, ~66
        101        MAKE_REF                                         $68     $67
        102        FETCH_DIM_W                                      $65     !3
        103        ASSIGN_REF                                               $65, $68
   54   104      > JMP                                                      ->118
   57   105    >   ASSIGN_DIM_OP                .=               8          !3, 0
        106        OP_DATA                                                  's'
   58   107        ASSIGN_OP                                     8          !0, '%3F'
   59   108        FETCH_DIM_R                                      ~73     !4, !5
        109        CAST                                          6  ~74     ~73
        110        ASSIGN_DIM                                               !4, !5
        111        OP_DATA                                                  ~74
   60   112        POST_INC                                         ~76     !5
        113        FETCH_DIM_W                                      $77     !4, ~76
        114        MAKE_REF                                         $78     $77
        115        FETCH_DIM_W                                      $75     !3, !5
        116        ASSIGN_REF                                               $75, $78
   61   117      > JMP                                                      ->118
   13   118    > > JMP                                                      ->11
        119    >   FE_FREE                                                  $16
   65   120        INIT_FCALL                                               'var_dump'
        121        SEND_VAR                                                 !0
        122        SEND_VAR                                                 !3
        123        DO_ICALL                                                 
   66   124      > RETURN                                                   null

End of function preparewithparams

Function isquotedstring:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 4, Position 2 = 10
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
filename:       /in/tdIqv
function name:  isQuotedString
number of ops:  12
compiled vars:  !0 = $token
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
   73     1        FETCH_DIM_R                                      ~1      !0, 0
          2        IN_ARRAY                                         ~2      ~1, <array>
          3      > JMPZ_EX                                          ~2      ~2, ->10
          4    >   FETCH_DIM_R                                      ~3      !0, 0
          5        STRLEN                                           ~4      !0
          6        SUB                                              ~5      ~4, 1
          7        FETCH_DIM_R                                      ~6      !0, ~5
          8        IS_EQUAL                                         ~7      ~3, ~6
          9        BOOL                                             ~2      ~7
         10    > > RETURN                                                   ~2
   74    11*     > RETURN                                                   null

End of function isquotedstring

Function getquerytokens:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 30
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 30
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 29
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 30
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 30
filename:       /in/tdIqv
function name:  getQueryTokens
number of ops:  33
compiled vars:  !0 = $query, !1 = $expr, !2 = $tokens, !3 = $token
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   RECV                                             !0      
   86     1        BIND_STATIC                                              !1
   87     2        ASSIGN                                                   !2, <array>
   89     3        INIT_FCALL                                               'preg_split'
          4        SEND_VAR                                                 !1
          5        SEND_VAR                                                 !0
          6        SEND_VAL                                                 -1
          7        SEND_VAL                                                 3
          8        DO_ICALL                                         $5      
          9      > FE_RESET_R                                       $6      $5, ->30
         10    > > FE_FETCH_R                                               $6, !3, ->30
   90    11    >   INIT_METHOD_CALL                                         'isQuotedString'
         12        SEND_VAR                                                 !3
         13        DO_FCALL                                      0  $7      
         14      > JMPZ                                                     $7, ->18
   91    15    >   ASSIGN_DIM                                               !2
         16        OP_DATA                                                  !3
         17      > JMP                                                      ->29
   93    18    >   INIT_FCALL                                               'array_merge'
         19        SEND_VAR                                                 !2
         20        INIT_FCALL                                               'preg_split'
         21        SEND_VAL                                                 '%2F%28%5C%3F%29%2F'
         22        SEND_VAR                                                 !3
         23        SEND_VAL                                                 -1
         24        SEND_VAL                                                 2
         25        DO_ICALL                                         $9      
         26        SEND_VAR                                                 $9
         27        DO_ICALL                                         $10     
         28        ASSIGN                                                   !2, $10
   89    29    > > JMP                                                      ->10
         30    >   FE_FREE                                                  $6
   97    31      > RETURN                                                   !2
   98    32*     > RETURN                                                   null

End of function getquerytokens

End of class SlightlyLessBollocksMySQLi.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
159.05 ms | 1404 KiB | 27 Q