3v4l.org

run code in 300+ PHP versions simultaneously
<?php /********************** OBJECTS **********************/ interface OldInterface {} class OldA implements OldInterface {} class OldB implements OldInterface {} interface NewInterface {} class NewA implements NewInterface {} class NewB implements NewInterface {} /********************** CONVERTERS **********************/ interface ConverterInterface { public function supports(OldInterface $old): bool; public function convert(OldInterface $old): NewInterface; } class AConverter implements ConverterInterface { public function supports(OldInterface $old): bool { return $old instanceof OldA; } public function convert(OldInterface $old): NewInterface { return new NewA(); } } class BConverter implements ConverterInterface { public function supports(OldInterface $old): bool { return $old instanceof OldB; } public function convert(OldInterface $old): NewInterface { return new NewB(); } } /********************** CONVERTER DISCOVERER **********************/ interface ConverterDiscovererInterface { public function discoverConverterFor(OldInterface $old): ?ConverterInterface; } class ConverterDiscoverer implements ConverterDiscovererInterface { /** * @param ConverterInterface[] $converters */ public function __construct(private array $converters) {} public function discoverConverterFor(OldInterface $old): ?ConverterInterface { foreach ($this->converters as $converter) { if ($converter->supports($old)) { return $converter; } } return null; } } /********************** MASS CONVERTER **********************/ class MassConverter { public function __construct(private ConverterDiscovererInterface $converterDiscoverer) {} /** * @param OldInterface[] $oldObjects * @return NewInterface[] */ public function massConvert(array $oldObjects): array { $newObjects = []; foreach ($oldObjects as $oldObject) { $converter = $this->converterDiscoverer->discoverConverterFor($oldObject); if ($converter !== null) { $newObjects[] = $converter->convert($oldObject); } } return $newObjects; } } /********************** SAMPLE **********************/ $converterDiscoverer = new ConverterDiscoverer([new AConverter(), new BConverter()]); $massConverter = new MassConverter($converterDiscoverer); var_dump($massConverter->massConvert([new OldA(), new OldB()]));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  (null)
number of ops:  34
compiled vars:  !0 = $converterDiscoverer, !1 = $massConverter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   DECLARE_CLASS                                            'olda'
    7     1        DECLARE_CLASS                                            'oldb'
   10     2        DECLARE_CLASS                                            'newa'
   11     3        DECLARE_CLASS                                            'newb'
   21     4        DECLARE_CLASS                                            'aconverter'
   34     5        DECLARE_CLASS                                            'bconverter'
   54     6        DECLARE_CLASS                                            'converterdiscoverer'
  100     7        NEW                                              $2      'ConverterDiscoverer'
          8        NEW                                              $3      'AConverter'
          9        DO_FCALL                                      0          
         10        INIT_ARRAY                                       ~5      $3
         11        NEW                                              $6      'BConverter'
         12        DO_FCALL                                      0          
         13        ADD_ARRAY_ELEMENT                                ~5      $6
         14        SEND_VAL_EX                                              ~5
         15        DO_FCALL                                      0          
         16        ASSIGN                                                   !0, $2
  101    17        NEW                                              $10     'MassConverter'
         18        SEND_VAR_EX                                              !0
         19        DO_FCALL                                      0          
         20        ASSIGN                                                   !1, $10
  103    21        INIT_FCALL                                               'var_dump'
         22        INIT_METHOD_CALL                                         !1, 'massConvert'
         23        NEW                                              $13     'OldA'
         24        DO_FCALL                                      0          
         25        INIT_ARRAY                                       ~15     $13
         26        NEW                                              $16     'OldB'
         27        DO_FCALL                                      0          
         28        ADD_ARRAY_ELEMENT                                ~15     $16
         29        SEND_VAL_EX                                              ~15
         30        DO_FCALL                                      0  $18     
         31        SEND_VAR                                                 $18
         32        DO_ICALL                                                 
         33      > RETURN                                                   1

Class OldInterface: [no user functions]
Class OldA: [no user functions]
Class OldB: [no user functions]
Class NewInterface: [no user functions]
Class NewA: [no user functions]
Class NewB: [no user functions]
Class ConverterInterface:
Function supports:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  supports
number of ops:  3
compiled vars:  !0 = $old
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        VERIFY_RETURN_TYPE                                       
          2      > RETURN                                                   null

End of function supports

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

End of function convert

End of class ConverterInterface.

Class AConverter:
Function supports:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  supports
number of ops:  6
compiled vars:  !0 = $old
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
   25     1        INSTANCEOF                                       ~1      !0, 'OldA'
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   26     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function supports

Function convert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  convert
number of ops:  7
compiled vars:  !0 = $old
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   28     0  E >   RECV                                             !0      
   30     1        NEW                                              $1      'NewA'
          2        DO_FCALL                                      0          
          3        VERIFY_RETURN_TYPE                                       $1
          4      > RETURN                                                   $1
   31     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function convert

End of class AConverter.

Class BConverter:
Function supports:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  supports
number of ops:  6
compiled vars:  !0 = $old
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   RECV                                             !0      
   38     1        INSTANCEOF                                       ~1      !0, 'OldB'
          2        VERIFY_RETURN_TYPE                                       ~1
          3      > RETURN                                                   ~1
   39     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function supports

Function convert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  convert
number of ops:  7
compiled vars:  !0 = $old
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   RECV                                             !0      
   43     1        NEW                                              $1      'NewB'
          2        DO_FCALL                                      0          
          3        VERIFY_RETURN_TYPE                                       $1
          4      > RETURN                                                   $1
   44     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function convert

End of class BConverter.

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

End of function discoverconverterfor

End of class ConverterDiscovererInterface.

Class ConverterDiscoverer:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  __construct
number of ops:  4
compiled vars:  !0 = $converters
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   59     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'converters'
          2        OP_DATA                                                  !0
          3      > RETURN                                                   null

End of function __construct

Function discoverconverterfor:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 12
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 12
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 11
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/6KOo4
function name:  discoverConverterFor
number of ops:  16
compiled vars:  !0 = $old, !1 = $converter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   61     0  E >   RECV                                             !0      
   63     1        FETCH_OBJ_R                                      ~2      'converters'
          2      > FE_RESET_R                                       $3      ~2, ->12
          3    > > FE_FETCH_R                                               $3, !1, ->12
   64     4    >   INIT_METHOD_CALL                                         !1, 'supports'
          5        SEND_VAR_EX                                              !0
          6        DO_FCALL                                      0  $4      
          7      > JMPZ                                                     $4, ->11
   65     8    >   VERIFY_RETURN_TYPE                                       !1
          9        FE_FREE                                                  $3
         10      > RETURN                                                   !1
   63    11    > > JMP                                                      ->3
         12    >   FE_FREE                                                  $3
   69    13      > RETURN                                                   null
   70    14*       VERIFY_RETURN_TYPE                                       
         15*     > RETURN                                                   null

End of function discoverconverterfor

End of class ConverterDiscoverer.

Class MassConverter:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/6KOo4
function name:  __construct
number of ops:  4
compiled vars:  !0 = $converterDiscoverer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   77     0  E >   RECV                                             !0      
          1        ASSIGN_OBJ                                               'converterDiscoverer'
          2        OP_DATA                                                  !0
          3      > RETURN                                                   null

End of function __construct

Function massconvert:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 17
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 17
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 16
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 16
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 17
filename:       /in/6KOo4
function name:  massConvert
number of ops:  22
compiled vars:  !0 = $oldObjects, !1 = $newObjects, !2 = $oldObject, !3 = $converter
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   83     0  E >   RECV                                             !0      
   85     1        ASSIGN                                                   !1, <array>
   87     2      > FE_RESET_R                                       $5      !0, ->17
          3    > > FE_FETCH_R                                               $5, !2, ->17
   88     4    >   FETCH_OBJ_R                                      ~6      'converterDiscoverer'
          5        INIT_METHOD_CALL                                         ~6, 'discoverConverterFor'
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $7      
          8        ASSIGN                                                   !3, $7
   89     9        TYPE_CHECK                                  1020          !3
         10      > JMPZ                                                     ~9, ->16
   90    11    >   INIT_METHOD_CALL                                         !3, 'convert'
         12        SEND_VAR_EX                                              !2
         13        DO_FCALL                                      0  $11     
         14        ASSIGN_DIM                                               !1
         15        OP_DATA                                                  $11
   87    16    > > JMP                                                      ->3
         17    >   FE_FREE                                                  $5
   94    18        VERIFY_RETURN_TYPE                                       !1
         19      > RETURN                                                   !1
   95    20*       VERIFY_RETURN_TYPE                                       
         21*     > RETURN                                                   null

End of function massconvert

End of class MassConverter.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.01 ms | 1015 KiB | 14 Q