3v4l.org

run code in 300+ PHP versions simultaneously
<?php // namespace AppBundle\Helper; /** * @author Bouchez Guillaume <guillaume.bouchez@infolegale.fr> */ class ShortIdGenerator { /* * Version du générateur */ const GENERATOR_VERSION = '1'; /** * Identifiant de version, prefix */ const GENERATOR_VERSION_PREFIX = 'a'; /** * @return string */ public static function generate() { $microtime = microtime(true); $numericId = intval(implode('', explode('.', $microtime))); $ret = self::numToAlpha($numericId, self::GENERATOR_VERSION_PREFIX); var_dump($numericId, $ret); return $ret; } /** * @see http://kvz.io/blog/2009/06/10/create-short-ids-with-php-like-youtube-or-tinyurl/ * @param mixed $in int, bigint input to translate * @param mixed $pad_up Number or boolean pads the result up to a specified length * @param string $pass_key Supplying a password makes it harder to calculate the original ID */ protected static function numToAlpha( $in, $prefix = '', $pad_up = false, $pass_key = null ) { $out = ''; $index = 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $base = strlen($index); if ($pass_key !== null) { for ($n = 0; $n < strlen($index); $n++) { $i[] = substr($index, $n, 1); } $pass_hash = hash('sha256', $pass_key); $pass_hash = (strlen($pass_hash) < strlen($index) ? hash('sha512', $pass_key) : $pass_hash); for ($n = 0; $n < strlen($index); $n++) { $p[] = substr($pass_hash, $n, 1); } array_multisort($p, SORT_DESC, $i); $index = implode($i); } if (is_numeric($pad_up)) { $pad_up--; if ($pad_up > 0) { $in += pow($base, $pad_up); } } for ($t = ($in != 0 ? floor(log($in, $base)) : 0); $t >= 0; $t--) { $bcp = bcpow($base, $t); $a = floor($in / $bcp) % $base; $out = $out . substr($index, $a, 1); $in = $in - ($a * $bcp); } return $prefix.$out; } } ShortIdGenerator::generate();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nYubi
function name:  (null)
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   85     0  E >   INIT_STATIC_METHOD_CALL                                  'ShortIdGenerator', 'generate'
          1        DO_FCALL                                      0          
          2      > RETURN                                                   1

Class ShortIdGenerator:
Function generate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/nYubi
function name:  generate
number of ops:  25
compiled vars:  !0 = $microtime, !1 = $numericId, !2 = $ret
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   25     0  E >   INIT_FCALL                                               'microtime'
          1        SEND_VAL                                                 <true>
          2        DO_ICALL                                         $3      
          3        ASSIGN                                                   !0, $3
   26     4        INIT_FCALL                                               'implode'
          5        SEND_VAL                                                 ''
          6        INIT_FCALL                                               'explode'
          7        SEND_VAL                                                 '.'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                         $5      
         10        SEND_VAR                                                 $5
         11        DO_ICALL                                         $6      
         12        CAST                                          4  ~7      $6
         13        ASSIGN                                                   !1, ~7
   28    14        INIT_STATIC_METHOD_CALL                                  'numToAlpha'
         15        SEND_VAR_EX                                              !1
         16        SEND_VAL_EX                                              'a'
         17        DO_FCALL                                      0  $9      
         18        ASSIGN                                                   !2, $9
   30    19        INIT_FCALL                                               'var_dump'
         20        SEND_VAR                                                 !1
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                                 
   31    23      > RETURN                                                   !2
   32    24*     > RETURN                                                   null

End of function generate

Function numtoalpha:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 62
Branch analysis from position: 10
1 jumps found. (Code = 42) Position 1 = 20
Branch analysis from position: 20
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 12
Branch analysis from position: 23
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 38
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 42
Branch analysis from position: 53
2 jumps found. (Code = 43) Position 1 = 66, Position 2 = 74
Branch analysis from position: 66
2 jumps found. (Code = 43) Position 1 = 69, Position 2 = 74
Branch analysis from position: 69
2 jumps found. (Code = 43) Position 1 = 76, Position 2 = 85
Branch analysis from position: 76
1 jumps found. (Code = 42) Position 1 = 86
Branch analysis from position: 86
1 jumps found. (Code = 42) Position 1 = 110
Branch analysis from position: 110
2 jumps found. (Code = 44) Position 1 = 112, Position 2 = 88
Branch analysis from position: 112
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 88
2 jumps found. (Code = 44) Position 1 = 112, Position 2 = 88
Branch analysis from position: 112
Branch analysis from position: 88
Branch analysis from position: 85
1 jumps found. (Code = 42) Position 1 = 110
Branch analysis from position: 110
Branch analysis from position: 74
Branch analysis from position: 74
Branch analysis from position: 42
2 jumps found. (Code = 44) Position 1 = 53, Position 2 = 42
Branch analysis from position: 53
Branch analysis from position: 42
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 23, Position 2 = 12
Branch analysis from position: 23
Branch analysis from position: 12
Branch analysis from position: 62
filename:       /in/nYubi
function name:  numToAlpha
number of ops:  115
compiled vars:  !0 = $in, !1 = $prefix, !2 = $pad_up, !3 = $pass_key, !4 = $out, !5 = $index, !6 = $base, !7 = $n, !8 = $i, !9 = $pass_hash, !10 = $p, !11 = $t, !12 = $bcp, !13 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
          2        RECV_INIT                                        !2      <false>
          3        RECV_INIT                                        !3      null
   46     4        ASSIGN                                                   !4, ''
   47     5        ASSIGN                                                   !5, 'abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
   48     6        STRLEN                                           ~16     !5
          7        ASSIGN                                                   !6, ~16
   50     8        TYPE_CHECK                                  1020          !3
          9      > JMPZ                                                     ~18, ->62
   51    10    >   ASSIGN                                                   !7, 0
         11      > JMP                                                      ->20
   52    12    >   INIT_FCALL                                               'substr'
         13        SEND_VAR                                                 !5
         14        SEND_VAR                                                 !7
         15        SEND_VAL                                                 1
         16        DO_ICALL                                         $21     
         17        ASSIGN_DIM                                               !8
         18        OP_DATA                                                  $21
   51    19        PRE_INC                                                  !7
         20    >   STRLEN                                           ~23     !5
         21        IS_SMALLER                                               !7, ~23
         22      > JMPNZ                                                    ~24, ->12
   55    23    >   INIT_FCALL                                               'hash'
         24        SEND_VAL                                                 'sha256'
         25        SEND_VAR                                                 !3
         26        DO_ICALL                                         $25     
         27        ASSIGN                                                   !9, $25
   56    28        STRLEN                                           ~27     !9
         29        STRLEN                                           ~28     !5
         30        IS_SMALLER                                               ~27, ~28
         31      > JMPZ                                                     ~29, ->38
         32    >   INIT_FCALL                                               'hash'
         33        SEND_VAL                                                 'sha512'
         34        SEND_VAR                                                 !3
         35        DO_ICALL                                         $30     
         36        QM_ASSIGN                                        ~31     $30
         37      > JMP                                                      ->39
         38    >   QM_ASSIGN                                        ~31     !9
         39    >   ASSIGN                                                   !9, ~31
   58    40        ASSIGN                                                   !7, 0
         41      > JMP                                                      ->50
   59    42    >   INIT_FCALL                                               'substr'
         43        SEND_VAR                                                 !9
         44        SEND_VAR                                                 !7
         45        SEND_VAL                                                 1
         46        DO_ICALL                                         $35     
         47        ASSIGN_DIM                                               !10
         48        OP_DATA                                                  $35
   58    49        PRE_INC                                                  !7
         50    >   STRLEN                                           ~37     !5
         51        IS_SMALLER                                               !7, ~37
         52      > JMPNZ                                                    ~38, ->42
   62    53    >   INIT_FCALL                                               'array_multisort'
         54        SEND_REF                                                 !10
         55        SEND_VAL                                                 3
         56        SEND_REF                                                 !8
         57        DO_ICALL                                                 
   63    58        INIT_FCALL                                               'implode'
         59        SEND_VAR                                                 !8
         60        DO_ICALL                                         $40     
         61        ASSIGN                                                   !5, $40
   66    62    >   INIT_FCALL                                               'is_numeric'
         63        SEND_VAR                                                 !2
         64        DO_ICALL                                         $42     
         65      > JMPZ                                                     $42, ->74
   67    66    >   PRE_DEC                                                  !2
   69    67        IS_SMALLER                                               0, !2
         68      > JMPZ                                                     ~44, ->74
   70    69    >   INIT_FCALL                                               'pow'
         70        SEND_VAR                                                 !6
         71        SEND_VAR                                                 !2
         72        DO_ICALL                                         $45     
         73        ASSIGN_OP                                     1          !0, $45
   74    74    >   IS_NOT_EQUAL                                             !0, 0
         75      > JMPZ                                                     ~47, ->85
         76    >   INIT_FCALL                                               'floor'
         77        INIT_FCALL                                               'log'
         78        SEND_VAR                                                 !0
         79        SEND_VAR                                                 !6
         80        DO_ICALL                                         $48     
         81        SEND_VAR                                                 $48
         82        DO_ICALL                                         $49     
         83        QM_ASSIGN                                        ~50     $49
         84      > JMP                                                      ->86
         85    >   QM_ASSIGN                                        ~50     0
         86    >   ASSIGN                                                   !11, ~50
         87      > JMP                                                      ->110
   75    88    >   INIT_FCALL_BY_NAME                                       'bcpow'
         89        SEND_VAR_EX                                              !6
         90        SEND_VAR_EX                                              !11
         91        DO_FCALL                                      0  $52     
         92        ASSIGN                                                   !12, $52
   76    93        INIT_FCALL                                               'floor'
         94        DIV                                              ~54     !0, !12
         95        SEND_VAL                                                 ~54
         96        DO_ICALL                                         $55     
         97        MOD                                              ~56     $55, !6
         98        ASSIGN                                                   !13, ~56
   77    99        INIT_FCALL                                               'substr'
        100        SEND_VAR                                                 !5
        101        SEND_VAR                                                 !13
        102        SEND_VAL                                                 1
        103        DO_ICALL                                         $58     
        104        CONCAT                                           ~59     !4, $58
        105        ASSIGN                                                   !4, ~59
   78   106        MUL                                              ~61     !13, !12
        107        SUB                                              ~62     !0, ~61
        108        ASSIGN                                                   !0, ~62
   74   109        PRE_DEC                                                  !11
        110    >   IS_SMALLER_OR_EQUAL                                      0, !11
        111      > JMPNZ                                                    ~65, ->88
   81   112    >   CONCAT                                           ~66     !1, !4
        113      > RETURN                                                   ~66
   82   114*     > RETURN                                                   null

End of function numtoalpha

End of class ShortIdGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
163.88 ms | 1408 KiB | 35 Q