3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface ModifierInterface { public function apply(array $data, string $key): array; } class ArrayHelper { public static function mergeReverse(...$args): array { return self::applyModifiers(self::performMergeReverse(...$args)); } public static function merge(...$args): array { return self::applyModifiers(self::performMerge(...$args)); } private static function performMergeReverse(...$args): array { $res = array_pop($args) ?: []; while (!empty($args)) { foreach (array_pop($args) as $k => $v) { if (is_int($k)) { if (array_key_exists($k, $res) && $res[$k] !== $v) { $res[] = $v; } else { $res[$k] = $v; } } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) { $res[$k] = self::performMergeReverse($v, $res[$k]); } elseif (!isset($res[$k])) { $res[$k] = $v; } } } return $res; } private static function performMerge(...$args): array { $res = array_shift($args) ?: []; while (!empty($args)) { foreach (array_shift($args) as $k => $v) { if (is_int($k)) { if (array_key_exists($k, $res) && $res[$k] !== $v) { $res[] = $v; } else { $res[$k] = $v; } } elseif (is_array($v) && isset($res[$k]) && is_array($res[$k])) { $res[$k] = self::performMerge($res[$k], $v); } else { $res[$k] = $v; } } } return $res; } public static function applyModifiers(array $data): array { $modifiers = []; foreach ($data as $k => $v) { if ($v instanceof ModifierInterface) { $modifiers[$k] = $v; unset($data[$k]); } elseif (is_array($v)) { $data[$k] = self::applyModifiers($v); } } foreach ($modifiers as $key => $modifier) { $data = $modifier->apply($data, $key); } return $data; } } $package = [ 'ApplicationStartup::class' => [ ['WebAppInfoCollector::class', 'collect'], ], 'ApplicationShutdown::class' => [ ['WebAppInfoCollector::class', 'collect'], ], 'BeforeRequest::class' => [ ['Debugger::class', 'startup'], ['WebAppInfoCollector::class', 'collect'], ], ]; $app = [ 'ApplicationStartup::class' => [ ['AppStartup::class', 'start'], ], 'ApplicationShutdown::class' => [ ['AppShutdown::class', 'shutdown'], ['WebAppInfoCollector::class', 'collectMainApp'], ], ]; $result = ArrayHelper::merge($package, $app); var_export($result); echo "\n\n"; $resultPop = ArrayHelper::mergeReverse($package, $app); var_export($resultPop);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daMvc
function name:  (null)
number of ops:  20
compiled vars:  !0 = $package, !1 = $app, !2 = $result, !3 = $resultPop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   86     0  E >   ASSIGN                                                   !0, <array>
   99     1        ASSIGN                                                   !1, <array>
  108     2        INIT_STATIC_METHOD_CALL                                  'ArrayHelper', 'merge'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $6      
          6        ASSIGN                                                   !2, $6
  109     7        INIT_FCALL                                               'var_export'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                                 
  110    10        ECHO                                                     '%0A%0A'
  111    11        INIT_STATIC_METHOD_CALL                                  'ArrayHelper', 'mergeReverse'
         12        SEND_VAR                                                 !0
         13        SEND_VAR                                                 !1
         14        DO_FCALL                                      0  $9      
         15        ASSIGN                                                   !3, $9
  112    16        INIT_FCALL                                               'var_export'
         17        SEND_VAR                                                 !3
         18        DO_ICALL                                                 
         19      > RETURN                                                   1

Class ModifierInterface:
Function apply:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daMvc
function name:  apply
number of ops:  4
compiled vars:  !0 = $data, !1 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        VERIFY_RETURN_TYPE                                       
          3      > RETURN                                                   null

End of function apply

End of class ModifierInterface.

Class ArrayHelper:
Function mergereverse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daMvc
function name:  mergeReverse
number of ops:  12
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV_VARIADIC                                    !0      
   14     1        INIT_STATIC_METHOD_CALL                                  'applyModifiers'
          2        INIT_STATIC_METHOD_CALL                                  'performMergeReverse'
          3        SEND_UNPACK                                              !0
          4        CHECK_UNDEF_ARGS                                         
          5        DO_FCALL                                      1  $1      
          6        SEND_VAR_NO_REF_EX                                       $1
          7        DO_FCALL                                      0  $2      
          8        VERIFY_RETURN_TYPE                                       $2
          9      > RETURN                                                   $2
   15    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function mergereverse

Function merge:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/daMvc
function name:  merge
number of ops:  12
compiled vars:  !0 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV_VARIADIC                                    !0      
   19     1        INIT_STATIC_METHOD_CALL                                  'applyModifiers'
          2        INIT_STATIC_METHOD_CALL                                  'performMerge'
          3        SEND_UNPACK                                              !0
          4        CHECK_UNDEF_ARGS                                         
          5        DO_FCALL                                      1  $1      
          6        SEND_VAR_NO_REF_EX                                       $1
          7        DO_FCALL                                      0  $2      
          8        VERIFY_RETURN_TYPE                                       $2
          9      > RETURN                                                   $2
   20    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function merge

Function performmergereverse:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 8
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 51
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 51
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 28
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 21
Branch analysis from position: 28
2 jumps found. (Code = 46) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 36
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 45
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 50
Branch analysis from position: 50
Branch analysis from position: 45
2 jumps found. (Code = 43) Position 1 = 48, Position 2 = 50
Branch analysis from position: 48
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 50
Branch analysis from position: 36
Branch analysis from position: 32
Branch analysis from position: 51
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 8
Branch analysis from position: 55
Branch analysis from position: 8
Branch analysis from position: 51
filename:       /in/daMvc
function name:  performMergeReverse
number of ops:  59
compiled vars:  !0 = $args, !1 = $res, !2 = $v, !3 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   RECV_VARIADIC                                    !0      
   24     1        INIT_FCALL                                               'array_pop'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $4      
          4        JMP_SET                                          ~5      $4, ->6
          5        QM_ASSIGN                                        ~5      <array>
          6        ASSIGN                                                   !1, ~5
   25     7      > JMP                                                      ->52
   26     8    >   INIT_FCALL                                               'array_pop'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                         $7      
         11      > FE_RESET_R                                       $8      $7, ->51
         12    > > FE_FETCH_R                                       ~9      $8, !2, ->51
         13    >   ASSIGN                                                   !3, ~9
   27    14        TYPE_CHECK                                   16          !3
         15      > JMPZ                                                     ~11, ->28
   28    16    >   ARRAY_KEY_EXISTS                                 ~12     !3, !1
         17      > JMPZ_EX                                          ~12     ~12, ->21
         18    >   FETCH_DIM_R                                      ~13     !1, !3
         19        IS_NOT_IDENTICAL                                 ~14     !2, ~13
         20        BOOL                                             ~12     ~14
         21    > > JMPZ                                                     ~12, ->25
   29    22    >   ASSIGN_DIM                                               !1
         23        OP_DATA                                                  !2
   28    24      > JMP                                                      ->27
   31    25    >   ASSIGN_DIM                                               !1, !3
         26        OP_DATA                                                  !2
   27    27    > > JMP                                                      ->50
   33    28    >   TYPE_CHECK                                  128  ~17     !2
         29      > JMPZ_EX                                          ~17     ~17, ->32
         30    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~18     !1, !3
         31        BOOL                                             ~17     ~18
         32    > > JMPZ_EX                                          ~17     ~17, ->36
         33    >   FETCH_DIM_R                                      ~19     !1, !3
         34        TYPE_CHECK                                  128  ~20     ~19
         35        BOOL                                             ~17     ~20
         36    > > JMPZ                                                     ~17, ->45
   34    37    >   INIT_STATIC_METHOD_CALL                                  'performMergeReverse'
         38        SEND_VAR                                                 !2
         39        FETCH_DIM_R                                      ~22     !1, !3
         40        SEND_VAL                                                 ~22
         41        DO_FCALL                                      0  $23     
         42        ASSIGN_DIM                                               !1, !3
         43        OP_DATA                                                  $23
   33    44      > JMP                                                      ->50
   35    45    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~24     !1, !3
         46        BOOL_NOT                                         ~25     ~24
         47      > JMPZ                                                     ~25, ->50
   36    48    >   ASSIGN_DIM                                               !1, !3
         49        OP_DATA                                                  !2
   26    50    > > JMP                                                      ->12
         51    >   FE_FREE                                                  $8
   25    52    >   ISSET_ISEMPTY_CV                                 ~27     !0
         53        BOOL_NOT                                         ~28     ~27
         54      > JMPNZ                                                    ~28, ->8
   41    55    >   VERIFY_RETURN_TYPE                                       !1
         56      > RETURN                                                   !1
   42    57*       VERIFY_RETURN_TYPE                                       
         58*     > RETURN                                                   null

End of function performmergereverse

Function performmerge:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 49
Branch analysis from position: 49
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 8
Branch analysis from position: 52
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 48
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 48
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 28
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 43) Position 1 = 22, Position 2 = 25
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 21
Branch analysis from position: 28
2 jumps found. (Code = 46) Position 1 = 30, Position 2 = 32
Branch analysis from position: 30
2 jumps found. (Code = 46) Position 1 = 33, Position 2 = 36
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 37, Position 2 = 45
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 47
Branch analysis from position: 47
Branch analysis from position: 45
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 36
Branch analysis from position: 32
Branch analysis from position: 48
2 jumps found. (Code = 44) Position 1 = 52, Position 2 = 8
Branch analysis from position: 52
Branch analysis from position: 8
Branch analysis from position: 48
filename:       /in/daMvc
function name:  performMerge
number of ops:  56
compiled vars:  !0 = $args, !1 = $res, !2 = $v, !3 = $k
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   44     0  E >   RECV_VARIADIC                                    !0      
   46     1        INIT_FCALL                                               'array_shift'
          2        SEND_REF                                                 !0
          3        DO_ICALL                                         $4      
          4        JMP_SET                                          ~5      $4, ->6
          5        QM_ASSIGN                                        ~5      <array>
          6        ASSIGN                                                   !1, ~5
   47     7      > JMP                                                      ->49
   48     8    >   INIT_FCALL                                               'array_shift'
          9        SEND_REF                                                 !0
         10        DO_ICALL                                         $7      
         11      > FE_RESET_R                                       $8      $7, ->48
         12    > > FE_FETCH_R                                       ~9      $8, !2, ->48
         13    >   ASSIGN                                                   !3, ~9
   49    14        TYPE_CHECK                                   16          !3
         15      > JMPZ                                                     ~11, ->28
   50    16    >   ARRAY_KEY_EXISTS                                 ~12     !3, !1
         17      > JMPZ_EX                                          ~12     ~12, ->21
         18    >   FETCH_DIM_R                                      ~13     !1, !3
         19        IS_NOT_IDENTICAL                                 ~14     !2, ~13
         20        BOOL                                             ~12     ~14
         21    > > JMPZ                                                     ~12, ->25
   51    22    >   ASSIGN_DIM                                               !1
         23        OP_DATA                                                  !2
   50    24      > JMP                                                      ->27
   53    25    >   ASSIGN_DIM                                               !1, !3
         26        OP_DATA                                                  !2
   49    27    > > JMP                                                      ->47
   55    28    >   TYPE_CHECK                                  128  ~17     !2
         29      > JMPZ_EX                                          ~17     ~17, ->32
         30    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~18     !1, !3
         31        BOOL                                             ~17     ~18
         32    > > JMPZ_EX                                          ~17     ~17, ->36
         33    >   FETCH_DIM_R                                      ~19     !1, !3
         34        TYPE_CHECK                                  128  ~20     ~19
         35        BOOL                                             ~17     ~20
         36    > > JMPZ                                                     ~17, ->45
   56    37    >   INIT_STATIC_METHOD_CALL                                  'performMerge'
         38        FETCH_DIM_R                                      ~22     !1, !3
         39        SEND_VAL                                                 ~22
         40        SEND_VAR                                                 !2
         41        DO_FCALL                                      0  $23     
         42        ASSIGN_DIM                                               !1, !3
         43        OP_DATA                                                  $23
   55    44      > JMP                                                      ->47
   58    45    >   ASSIGN_DIM                                               !1, !3
         46        OP_DATA                                                  !2
   48    47    > > JMP                                                      ->12
         48    >   FE_FREE                                                  $8
   47    49    >   ISSET_ISEMPTY_CV                                 ~25     !0
         50        BOOL_NOT                                         ~26     ~25
         51      > JMPNZ                                                    ~26, ->8
   63    52    >   VERIFY_RETURN_TYPE                                       !1
         53      > RETURN                                                   !1
   64    54*       VERIFY_RETURN_TYPE                                       
         55*     > RETURN                                                   null

End of function performmerge

Function applymodifiers:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 19
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 19
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 18
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 18
Branch analysis from position: 19
2 jumps found. (Code = 77) Position 1 = 21, Position 2 = 29
Branch analysis from position: 21
2 jumps found. (Code = 78) Position 1 = 22, Position 2 = 29
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 21
Branch analysis from position: 21
Branch analysis from position: 29
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 29
Branch analysis from position: 19
filename:       /in/daMvc
function name:  applyModifiers
number of ops:  34
compiled vars:  !0 = $data, !1 = $modifiers, !2 = $v, !3 = $k, !4 = $modifier, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   RECV                                             !0      
   68     1        ASSIGN                                                   !1, <array>
   69     2      > FE_RESET_R                                       $7      !0, ->19
          3    > > FE_FETCH_R                                       ~8      $7, !2, ->19
          4    >   ASSIGN                                                   !3, ~8
   70     5        INSTANCEOF                                               !2, 'ModifierInterface'
          6      > JMPZ                                                     ~10, ->11
   71     7    >   ASSIGN_DIM                                               !1, !3
          8        OP_DATA                                                  !2
   72     9        UNSET_DIM                                                !0, !3
   70    10      > JMP                                                      ->18
   73    11    >   TYPE_CHECK                                  128          !2
         12      > JMPZ                                                     ~12, ->18
   74    13    >   INIT_STATIC_METHOD_CALL                                  'applyModifiers'
         14        SEND_VAR                                                 !2
         15        DO_FCALL                                      0  $14     
         16        ASSIGN_DIM                                               !0, !3
         17        OP_DATA                                                  $14
   69    18    > > JMP                                                      ->3
         19    >   FE_FREE                                                  $7
   77    20      > FE_RESET_R                                       $15     !1, ->29
         21    > > FE_FETCH_R                                       ~16     $15, !4, ->29
         22    >   ASSIGN                                                   !5, ~16
   78    23        INIT_METHOD_CALL                                         !4, 'apply'
         24        SEND_VAR_EX                                              !0
         25        SEND_VAR_EX                                              !5
         26        DO_FCALL                                      0  $18     
         27        ASSIGN                                                   !0, $18
   77    28      > JMP                                                      ->21
         29    >   FE_FREE                                                  $15
   80    30        VERIFY_RETURN_TYPE                                       !0
         31      > RETURN                                                   !0
   81    32*       VERIFY_RETURN_TYPE                                       
         33*     > RETURN                                                   null

End of function applymodifiers

End of class ArrayHelper.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.9 ms | 1021 KiB | 16 Q