3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Matrix { private int $rows; private int $columns; private array $values; public function __construct(int $rows, int $columns) { $this->rows = $rows; $this->columns = $columns; } public function rows(): int { return $this->rows; } public function columns() : int { return $this->columns; } } class MatrixFactory { public function create(int $rows, int $columns) : Matrix{ return new Matrix($rows, $columns); } } interface MatrixCommand { public function execute(Matrix $matrix): Response; public function title() :string; } class TransposeMatrixCommand implements MatrixCommand { public const TITLE = 'Pomnóż macierze wejściowe'; public function title() : string { return self::TITLE; } public function execute(Matrix $matrix):Response { return new Response(); } } class MultiplicationMatrixCommand implements MatrixCommand { public const TITLE = 'Pomnóż macierze wejściowe'; public function title() : string { return self::TITLE; } public function execute(Matrix $matrix):Response { return new Response(); } } class Response { } class RegistryMatrixCommand { private array $registry; public function __construct(iterable $commands) { $this->addCommands($commands); } public function addCommand(string $command) { if(false === class_exists($command)) { throw new Exception('Class not exists'); } $this->registry[$command] = new $command; } public function addCommands(iterable $commands) { foreach($commands as $command) { $this->addCommand($command); } } public function get(string $action) : MatrixCommand { if(false === array_key_exists($action, $this->registry)) { throw new Exception('Not found command!'); } return $this->registry[$action]; } public function list() : iterable { return $this->registry; } } class MenuRenderer { private array $commands; public function render(array $commands) : string { $menu = ''; $counter = 1; foreach($commands as $command) { $menu .= sprintf('%d - %s', $counter, $command->title()); $counter++; } return $menu; } } class MatrixAction { private RegistryMatrixCommand $registry; private MenuRenderer $menuRenderer; public function __construct(RegistryMatrixCommand $registry, MenuRenderer $menuRenderer) { $this->registry = $registry; $this->menuRenderer = $menuRenderer; } public function displayActions(): string { return $this->menuRenderer->render($this->registry->list()); } public function execute(Request $request) : Response { $matrix = (new MatrixFactory())->create( $request->rows(), $request->columns() ); $command = $this->registry->get($request->action()); $response = $command->execute($matrix); return $response; } } class Request { private string $action; private int $rows; private int $columns; public function __construct(string $action, int $rows, int $columns) { $this->action = $action; $this->rows = $rows; $this->columns = $columns; } public function action() :string { return $this->action; } public function rows() : int { return $this->rows; } public function columns(): int { return $this->columns; } } $actions = new ArrayIterator([ MultiplicationMatrixCommand::class, TransposeMatrixCommand::class ]); $registryMatrix = new RegistryMatrixCommand($actions); $menuRenderer = new MenuRenderer(); $matrixAction = new MatrixAction($registryMatrix, $menuRenderer); $request = new Request('TransposeMatrixCommand', 3, 4); echo $matrixAction->displayActions(); // var_dump($matrixAction->execute($request));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  (null)
number of ops:  28
compiled vars:  !0 = $actions, !1 = $registryMatrix, !2 = $menuRenderer, !3 = $matrixAction, !4 = $request
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   33     0  E >   DECLARE_CLASS                                            'transposematrixcommand'
   44     1        DECLARE_CLASS                                            'multiplicationmatrixcommand'
  160     2        NEW                                              $5      'ArrayIterator'
  161     3        SEND_VAL_EX                                              <array>
          4        DO_FCALL                                      0          
  160     5        ASSIGN                                                   !0, $5
  165     6        NEW                                              $8      'RegistryMatrixCommand'
          7        SEND_VAR_EX                                              !0
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !1, $8
  166    10        NEW                                              $11     'MenuRenderer'
         11        DO_FCALL                                      0          
         12        ASSIGN                                                   !2, $11
  168    13        NEW                                              $14     'MatrixAction'
         14        SEND_VAR_EX                                              !1
         15        SEND_VAR_EX                                              !2
         16        DO_FCALL                                      0          
         17        ASSIGN                                                   !3, $14
  169    18        NEW                                              $17     'Request'
         19        SEND_VAL_EX                                              'TransposeMatrixCommand'
         20        SEND_VAL_EX                                              3
         21        SEND_VAL_EX                                              4
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !4, $17
  170    24        INIT_METHOD_CALL                                         !3, 'displayActions'
         25        DO_FCALL                                      0  $20     
         26        ECHO                                                     $20
  171    27      > RETURN                                                   1

Class Matrix:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  __construct
number of ops:  7
compiled vars:  !0 = $rows, !1 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    9     2        ASSIGN_OBJ                                               'rows'
          3        OP_DATA                                                  !0
   10     4        ASSIGN_OBJ                                               'columns'
          5        OP_DATA                                                  !1
   11     6      > RETURN                                                   null

End of function __construct

Function rows:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  rows
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   FETCH_OBJ_R                                      ~0      'rows'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   15     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function rows

Function columns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  columns
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   18     0  E >   FETCH_OBJ_R                                      ~0      'columns'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   19     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function columns

End of class Matrix.

Class MatrixFactory:
Function create:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  create
number of ops:  10
compiled vars:  !0 = $rows, !1 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        NEW                                              $2      'Matrix'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
          6        VERIFY_RETURN_TYPE                                       $2
          7      > RETURN                                                   $2
   25     8*       VERIFY_RETURN_TYPE                                       
          9*     > RETURN                                                   null

End of function create

End of class MatrixFactory.

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

End of function execute

Function title:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  title
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function title

End of class MatrixCommand.

Class TransposeMatrixCommand:
Function title:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  title
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E > > RETURN                                                   'Pomn%C3%B3%C5%BC+macierze+wej%C5%9Bciowe'
   38     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function title

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

End of function execute

End of class TransposeMatrixCommand.

Class MultiplicationMatrixCommand:
Function title:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  title
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E > > RETURN                                                   'Pomn%C3%B3%C5%BC+macierze+wej%C5%9Bciowe'
   49     1*       VERIFY_RETURN_TYPE                                       
          2*     > RETURN                                                   null

End of function title

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

End of function execute

End of class MultiplicationMatrixCommand.

Class Response: [no user functions]
Class RegistryMatrixCommand:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  __construct
number of ops:  5
compiled vars:  !0 = $commands
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   65     1        INIT_METHOD_CALL                                         'addCommands'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   66     4      > RETURN                                                   null

End of function __construct

Function addcommand:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 10
Branch analysis from position: 6
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  addCommand
number of ops:  17
compiled vars:  !0 = $command
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
   69     1        INIT_FCALL                                               'class_exists'
          2        SEND_VAR                                                 !0
          3        DO_ICALL                                         $1      
          4        TYPE_CHECK                                    4          $1
          5      > JMPZ                                                     ~2, ->10
   70     6    >   NEW                                              $3      'Exception'
          7        SEND_VAL_EX                                              'Class+not+exists'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $3
   73    10    >   FETCH_CLASS                                   0  $7      !0
         11        NEW                                              $8      $7
         12        DO_FCALL                                      0          
         13        FETCH_OBJ_W                                      $5      'registry'
         14        ASSIGN_DIM                                               $5, !0
         15        OP_DATA                                                  $8
   74    16      > RETURN                                                   null

End of function addcommand

Function addcommands:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 7
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
filename:       /in/WW7Vk
function name:  addCommands
number of ops:  9
compiled vars:  !0 = $commands, !1 = $command
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E >   RECV                                             !0      
   77     1      > FE_RESET_R                                       $2      !0, ->7
          2    > > FE_FETCH_R                                               $2, !1, ->7
   78     3    >   INIT_METHOD_CALL                                         'addCommand'
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0          
   77     6      > JMP                                                      ->2
          7    >   FE_FREE                                                  $2
   80     8      > RETURN                                                   null

End of function addcommands

Function get:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  get
number of ops:  15
compiled vars:  !0 = $action
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   RECV                                             !0      
   83     1        FETCH_OBJ_R                                      ~1      'registry'
          2        ARRAY_KEY_EXISTS                                 ~2      !0, ~1
          3        TYPE_CHECK                                    4          ~2
          4      > JMPZ                                                     ~3, ->9
   84     5    >   NEW                                              $4      'Exception'
          6        SEND_VAL_EX                                              'Not+found+command%21'
          7        DO_FCALL                                      0          
          8      > THROW                                         0          $4
   87     9    >   FETCH_OBJ_R                                      ~6      'registry'
         10        FETCH_DIM_R                                      ~7      ~6, !0
         11        VERIFY_RETURN_TYPE                                       ~7
         12      > RETURN                                                   ~7
   88    13*       VERIFY_RETURN_TYPE                                       
         14*     > RETURN                                                   null

End of function get

Function list:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  list
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   91     0  E >   FETCH_OBJ_R                                      ~0      'registry'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   92     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function list

End of class RegistryMatrixCommand.

Class MenuRenderer:
Function render:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 15
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 15
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 15
filename:       /in/WW7Vk
function name:  render
number of ops:  20
compiled vars:  !0 = $commands, !1 = $menu, !2 = $counter, !3 = $command
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   RECV                                             !0      
   99     1        ASSIGN                                                   !1, ''
  100     2        ASSIGN                                                   !2, 1
  102     3      > FE_RESET_R                                       $6      !0, ->15
          4    > > FE_FETCH_R                                               $6, !3, ->15
  103     5    >   INIT_FCALL                                               'sprintf'
          6        SEND_VAL                                                 '%25d+-+%25s'
          7        SEND_VAR                                                 !2
          8        INIT_METHOD_CALL                                         !3, 'title'
          9        DO_FCALL                                      0  $7      
         10        SEND_VAR                                                 $7
         11        DO_ICALL                                         $8      
         12        ASSIGN_OP                                     8          !1, $8
  104    13        PRE_INC                                                  !2
  102    14      > JMP                                                      ->4
         15    >   FE_FREE                                                  $6
  107    16        VERIFY_RETURN_TYPE                                       !1
         17      > RETURN                                                   !1
  108    18*       VERIFY_RETURN_TYPE                                       
         19*     > RETURN                                                   null

End of function render

End of class MenuRenderer.

Class MatrixAction:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  __construct
number of ops:  7
compiled vars:  !0 = $registry, !1 = $menuRenderer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  115     0  E >   RECV                                             !0      
          1        RECV                                             !1      
  116     2        ASSIGN_OBJ                                               'registry'
          3        OP_DATA                                                  !0
  117     4        ASSIGN_OBJ                                               'menuRenderer'
          5        OP_DATA                                                  !1
  118     6      > RETURN                                                   null

End of function __construct

Function displayactions:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  displayActions
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  121     0  E >   FETCH_OBJ_R                                      ~0      'menuRenderer'
          1        INIT_METHOD_CALL                                         ~0, 'render'
          2        FETCH_OBJ_R                                      ~1      'registry'
          3        INIT_METHOD_CALL                                         ~1, 'list'
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR_NO_REF_EX                                       $2
          6        DO_FCALL                                      0  $3      
          7        VERIFY_RETURN_TYPE                                       $3
          8      > RETURN                                                   $3
  122     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function displayactions

Function execute:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  execute
number of ops:  27
compiled vars:  !0 = $request, !1 = $matrix, !2 = $command, !3 = $response
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  124     0  E >   RECV                                             !0      
  125     1        NEW                                              $4      'MatrixFactory'
          2        DO_FCALL                                      0          
          3        INIT_METHOD_CALL                                         $4, 'create'
  126     4        INIT_METHOD_CALL                                         !0, 'rows'
          5        DO_FCALL                                      0  $6      
          6        SEND_VAR_NO_REF_EX                                       $6
  127     7        INIT_METHOD_CALL                                         !0, 'columns'
          8        DO_FCALL                                      0  $7      
          9        SEND_VAR_NO_REF_EX                                       $7
         10        DO_FCALL                                      0  $8      
  125    11        ASSIGN                                                   !1, $8
  130    12        FETCH_OBJ_R                                      ~10     'registry'
         13        INIT_METHOD_CALL                                         ~10, 'get'
         14        INIT_METHOD_CALL                                         !0, 'action'
         15        DO_FCALL                                      0  $11     
         16        SEND_VAR_NO_REF_EX                                       $11
         17        DO_FCALL                                      0  $12     
         18        ASSIGN                                                   !2, $12
  131    19        INIT_METHOD_CALL                                         !2, 'execute'
         20        SEND_VAR_EX                                              !1
         21        DO_FCALL                                      0  $14     
         22        ASSIGN                                                   !3, $14
  132    23        VERIFY_RETURN_TYPE                                       !3
         24      > RETURN                                                   !3
  133    25*       VERIFY_RETURN_TYPE                                       
         26*     > RETURN                                                   null

End of function execute

End of class MatrixAction.

Class Request:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  __construct
number of ops:  10
compiled vars:  !0 = $action, !1 = $rows, !2 = $columns
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  141     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
  142     3        ASSIGN_OBJ                                               'action'
          4        OP_DATA                                                  !0
  143     5        ASSIGN_OBJ                                               'rows'
          6        OP_DATA                                                  !1
  144     7        ASSIGN_OBJ                                               'columns'
          8        OP_DATA                                                  !2
  145     9      > RETURN                                                   null

End of function __construct

Function action:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  action
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  148     0  E >   FETCH_OBJ_R                                      ~0      'action'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  149     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function action

Function rows:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  rows
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  152     0  E >   FETCH_OBJ_R                                      ~0      'rows'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  153     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function rows

Function columns:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WW7Vk
function name:  columns
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  156     0  E >   FETCH_OBJ_R                                      ~0      'columns'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
  157     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function columns

End of class Request.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
169.84 ms | 1424 KiB | 17 Q