3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); class StackOverflowCopyPaste { /** @var int $v4MaskBits */ private $v4MaskBits; /** @var int $v6MaskBits */ private $v6MaskBits; /** * @param int $v4MaskBits * @param int $v6MaskBits */ public function __construct(int $v4MaskBits = 24, int $v6MaskBits = 48) { $this->v4MaskBits = $v4MaskBits; $this->v6MaskBits = $v6MaskBits; } /** * Return the given subnet for an IP and the configured mask bits * * Determine if the IP is an IPv4 or IPv6 address, then pass to the correct * method for handling that specific type. * * @param string $ip * @return string */ public function getSubnet(string $ip): string { if (\preg_match('/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/', $ip)) { return $this->getIPv4Subnet( $ip, (int) ($this->v4MaskBits ?? 32) ); } return $this->getIPv6Subnet( $ip, (int) ($this->v6MaskBits ?? 128) ); } /** * Return the given subnet for an IPv4 address and mask bits * * @param string $ip * @param int $maskBits * @return string */ public function getIPv4Subnet(string $ip, int $maskBits = 32): string { $binary = \inet_pton($ip); for ($i = 32; $i > $maskBits; $i -= 8) { $j = \intdiv($i, 8) - 1; $k = (int) \min(8, $i - $maskBits); $mask = (0xff - ((1 << $k) - 1)); $int = \unpack('C', $binary[$j]); $binary[$j] = \pack('C', $int[1] & $mask); } return \inet_ntop($binary).'/'.$maskBits; } /** * Return the given subnet for an IPv6 address and mask bits * * @param string $ip * @param int $maskBits * @return string */ public function getIPv6Subnet(string $ip, int $maskBits = 48): string { $binary = \inet_pton($ip); for ($i = 128; $i > $maskBits; $i -= 8) { $j = \intdiv($i, 8) - 1; $k = (int) \min(8, $i - $maskBits); $mask = (0xff - ((1 << $k) - 1)); $int = \unpack('C', $binary[$j]); $binary[$j] = \pack('C', $int[1] & $mask); } return \inet_ntop($binary).'/'.$maskBits; } } $mask = new StackOverflowCopyPaste(24, 48); var_dump($mask->getSubnet('12.34.56.78')); var_dump($mask->getSubnet('2001:0db8:85a3:0000:0000:8a2e:0370:7334'));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i2ufm
function name:  (null)
number of ops:  18
compiled vars:  !0 = $mask
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   85     0  E >   NEW                                                  $1      'StackOverflowCopyPaste'
          1        SEND_VAL_EX                                                  24
          2        SEND_VAL_EX                                                  48
          3        DO_FCALL                                          0          
          4        ASSIGN                                                       !0, $1
   87     5        INIT_FCALL                                                   'var_dump'
          6        INIT_METHOD_CALL                                             !0, 'getSubnet'
          7        SEND_VAL_EX                                                  '12.34.56.78'
          8        DO_FCALL                                          0  $4      
          9        SEND_VAR                                                     $4
         10        DO_ICALL                                                     
   88    11        INIT_FCALL                                                   'var_dump'
         12        INIT_METHOD_CALL                                             !0, 'getSubnet'
         13        SEND_VAL_EX                                                  '2001%3A0db8%3A85a3%3A0000%3A0000%3A8a2e%3A0370%3A7334'
         14        DO_FCALL                                          0  $6      
         15        SEND_VAR                                                     $6
         16        DO_ICALL                                                     
         17      > RETURN                                                       1

Class StackOverflowCopyPaste:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i2ufm
function name:  __construct
number of ops:  7
compiled vars:  !0 = $v4MaskBits, !1 = $v6MaskBits
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   15     0  E >   RECV_INIT                                            !0      24
          1        RECV_INIT                                            !1      48
   17     2        ASSIGN_OBJ                                                   'v4MaskBits'
          3        OP_DATA                                                      !0
   18     4        ASSIGN_OBJ                                                   'v6MaskBits'
          5        OP_DATA                                                      !1
   19     6      > RETURN                                                       null

End of function __construct

Function getsubnet:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/i2ufm
function name:  getSubnet
number of ops:  25
compiled vars:  !0 = $ip
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   30     0  E >   RECV                                                 !0      
   32     1        FRAMELESS_ICALL_2                preg_match          ~1      '%2F%5E%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%5C.%5B0-9%5D%7B1%2C3%7D%24%2F', !0
          2      > JMPZ                                                         ~1, ->13
   33     3    >   INIT_METHOD_CALL                                             'getIPv4Subnet'
   34     4        SEND_VAR_EX                                                  !0
   35     5        FETCH_OBJ_IS                                         ~2      'v4MaskBits'
          6        COALESCE                                             ~3      ~2
          7        QM_ASSIGN                                            ~3      32
          8        CAST                                              4  ~4      ~3
          9        SEND_VAL_EX                                                  ~4
   33    10        DO_FCALL                                          0  $5      
   35    11        VERIFY_RETURN_TYPE                                           $5
         12      > RETURN                                                       $5
   38    13    >   INIT_METHOD_CALL                                             'getIPv6Subnet'
   39    14        SEND_VAR_EX                                                  !0
   40    15        FETCH_OBJ_IS                                         ~6      'v6MaskBits'
         16        COALESCE                                             ~7      ~6
         17        QM_ASSIGN                                            ~7      128
         18        CAST                                              4  ~8      ~7
         19        SEND_VAL_EX                                                  ~8
   38    20        DO_FCALL                                          0  $9      
   40    21        VERIFY_RETURN_TYPE                                           $9
         22      > RETURN                                                       $9
   42    23*       VERIFY_RETURN_TYPE                                           
         24*     > RETURN                                                       null

End of function getsubnet

Function getipv4subnet:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 8
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 8
Branch analysis from position: 39
Branch analysis from position: 8
filename:       /in/i2ufm
function name:  getIPv4Subnet
number of ops:  48
compiled vars:  !0 = $ip, !1 = $maskBits, !2 = $binary, !3 = $i, !4 = $j, !5 = $k, !6 = $mask, !7 = $int
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      32
   53     2        INIT_FCALL                                                   'inet_pton'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                             $8      
          5        ASSIGN                                                       !2, $8
   54     6        ASSIGN                                                       !3, 32
          7      > JMP                                                          ->37
   55     8    >   INIT_FCALL                                                   'intdiv'
          9        SEND_VAR                                                     !3
         10        SEND_VAL                                                     8
         11        DO_ICALL                                             $11     
         12        SUB                                                  ~12     $11, 1
         13        ASSIGN                                                       !4, ~12
   56    14        SUB                                                  ~14     !3, !1
         15        FRAMELESS_ICALL_2                min                 ~15     8, ~14
         16        CAST                                              4  ~16     ~15
         17        ASSIGN                                                       !5, ~16
   57    18        SL                                                   ~18     1, !5
         19        SUB                                                  ~19     ~18, 1
         20        SUB                                                  ~20     255, ~19
         21        ASSIGN                                                       !6, ~20
   58    22        INIT_FCALL                                                   'unpack'
         23        SEND_VAL                                                     'C'
         24        FETCH_DIM_R                                          ~22     !2, !4
         25        SEND_VAL                                                     ~22
         26        DO_ICALL                                             $23     
         27        ASSIGN                                                       !7, $23
   59    28        INIT_FCALL                                                   'pack'
         29        SEND_VAL                                                     'C'
         30        FETCH_DIM_R                                          ~26     !7, 1
         31        BW_AND                                               ~27     !6, ~26
         32        SEND_VAL                                                     ~27
         33        DO_ICALL                                             $28     
         34        ASSIGN_DIM                                                   !2, !4
         35        OP_DATA                                                      $28
   54    36        ASSIGN_OP                                         2          !3, 8
         37    >   IS_SMALLER                                                   !1, !3
         38      > JMPNZ                                                        ~30, ->8
   61    39    >   INIT_FCALL                                                   'inet_ntop'
         40        SEND_VAR                                                     !2
         41        DO_ICALL                                             $31     
         42        CONCAT                                               ~32     $31, '%2F'
         43        CONCAT                                               ~33     ~32, !1
         44        VERIFY_RETURN_TYPE                                           ~33
         45      > RETURN                                                       ~33
   62    46*       VERIFY_RETURN_TYPE                                           
         47*     > RETURN                                                       null

End of function getipv4subnet

Function getipv6subnet:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 8
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 8
Branch analysis from position: 39
Branch analysis from position: 8
filename:       /in/i2ufm
function name:  getIPv6Subnet
number of ops:  48
compiled vars:  !0 = $ip, !1 = $maskBits, !2 = $binary, !3 = $i, !4 = $j, !5 = $k, !6 = $mask, !7 = $int
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   71     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      48
   73     2        INIT_FCALL                                                   'inet_pton'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                             $8      
          5        ASSIGN                                                       !2, $8
   74     6        ASSIGN                                                       !3, 128
          7      > JMP                                                          ->37
   75     8    >   INIT_FCALL                                                   'intdiv'
          9        SEND_VAR                                                     !3
         10        SEND_VAL                                                     8
         11        DO_ICALL                                             $11     
         12        SUB                                                  ~12     $11, 1
         13        ASSIGN                                                       !4, ~12
   76    14        SUB                                                  ~14     !3, !1
         15        FRAMELESS_ICALL_2                min                 ~15     8, ~14
         16        CAST                                              4  ~16     ~15
         17        ASSIGN                                                       !5, ~16
   77    18        SL                                                   ~18     1, !5
         19        SUB                                                  ~19     ~18, 1
         20        SUB                                                  ~20     255, ~19
         21        ASSIGN                                                       !6, ~20
   78    22        INIT_FCALL                                                   'unpack'
         23        SEND_VAL                                                     'C'
         24        FETCH_DIM_R                                          ~22     !2, !4
         25        SEND_VAL                                                     ~22
         26        DO_ICALL                                             $23     
         27        ASSIGN                                                       !7, $23
   79    28        INIT_FCALL                                                   'pack'
         29        SEND_VAL                                                     'C'
         30        FETCH_DIM_R                                          ~26     !7, 1
         31        BW_AND                                               ~27     !6, ~26
         32        SEND_VAL                                                     ~27
         33        DO_ICALL                                             $28     
         34        ASSIGN_DIM                                                   !2, !4
         35        OP_DATA                                                      $28
   74    36        ASSIGN_OP                                         2          !3, 8
         37    >   IS_SMALLER                                                   !1, !3
         38      > JMPNZ                                                        ~30, ->8
   81    39    >   INIT_FCALL                                                   'inet_ntop'
         40        SEND_VAR                                                     !2
         41        DO_ICALL                                             $31     
         42        CONCAT                                               ~32     $31, '%2F'
         43        CONCAT                                               ~33     ~32, !1
         44        VERIFY_RETURN_TYPE                                           ~33
         45      > RETURN                                                       ~33
   82    46*       VERIFY_RETURN_TYPE                                           
         47*     > RETURN                                                       null

End of function getipv6subnet

End of class StackOverflowCopyPaste.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
143.24 ms | 2590 KiB | 19 Q