3v4l.org

run code in 500+ PHP versions simultaneously
<?php //source: https://dzone.com/articles/simple-example-illustrates //FIX-ME !!!!!!!!!!!!! class Parser { private $successor; public function __construct(Parser $successor) { $this->setSuccessor($successor); } public function parse(string $fileName): void { if ($this->getSuccessor() != null) { $this->getSuccessor()->parse($fileName); } else { echo "Unable to find the correct parser for the file:" . $fileName . PHP_EOL; } } protected function canHandleFile(string $fileName, string $format): bool { $spl = new SplFileInfo($fileName); return ($fileName == null) || ($spl->getExtension() == $format); } public function getSuccessor(): ?Parser { return $this->successor; } public function setSuccessor(Parser $successor): void { $this->successor = $successor; } } class TextParser extends Parser { public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "txt")) { echo("A text parser is handling the file: " . $fileName . PHP_EOL); } else { parent::parse($fileName); } } } class JsonParser extends Parser { public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "json")) { echo("A JSON parser is handling the file: " . $fileName . PHP_EOL); } else { parent::parse($fileName); } } } class CSVParser extends Parser { public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "json")) { echo("A CSV parser is handling the file: " + $fileName . PHP_EOL); } else { parent::parse($fileName); } } } class XMLParser extends Parser { public function __construct() { return; } public function parse(string $fileName): void { if ($this->canHandleFile($fileName, "json")) { echo("A XML parser is handling the file: " + $fileName . PHP_EOL); } else { parent::parse($fileName); } } } //List of file names to parse. $fileList = ["someFile.txt", "otherFile.json", "xmlFile.xml", "csvFile.csv", "csvFile.doc"]; //No successor for this handler because this is the last in chain. $xmlParser = new XmlParser(); //XmlParser is the successor of CsvParser. $csvParser = new CsvParser($xmlParser); //CsvParser is the successor of JsonParser. $jsonParser = new JsonParser($csvParser); //JsonParser is the successor of TextParser. //TextParser is the start of the chain. $textParser = new TextParser($jsonParser); //Pass the file name to the first handler in the chain. foreach ($fileList as $key => $fileName) { $textParser->parse($fileName); }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 18, Position 2 = 24
Branch analysis from position: 18
2 jumps found. (Code = 78) Position 1 = 19, Position 2 = 24
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/ukJtE
function name:  (null)
number of ops:  26
compiled vars:  !0 = $fileList, !1 = $xmlParser, !2 = $csvParser, !3 = $jsonParser, !4 = $textParser, !5 = $fileName, !6 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   73     0  E >   DECLARE_CLASS                                                'xmlparser', 'parser'
   89     1        ASSIGN                                                       !0, <array>
   92     2        NEW                                                  $8      'XmlParser'
          3        DO_FCALL                                          0          
          4        ASSIGN                                                       !1, $8
   95     5        NEW                                                  $11     'CsvParser'
          6        SEND_VAR_EX                                                  !1
          7        DO_FCALL                                          0          
          8        ASSIGN                                                       !2, $11
   98     9        NEW                                                  $14     'JsonParser'
         10        SEND_VAR_EX                                                  !2
         11        DO_FCALL                                          0          
         12        ASSIGN                                                       !3, $14
  102    13        NEW                                                  $17     'TextParser'
         14        SEND_VAR_EX                                                  !3
         15        DO_FCALL                                          0          
         16        ASSIGN                                                       !4, $17
  105    17      > FE_RESET_R                                           $20     !0, ->24
         18    > > FE_FETCH_R                                           ~21     $20, !5, ->24
         19    >   ASSIGN                                                       !6, ~21
  106    20        INIT_METHOD_CALL                                             !4, 'parse'
         21        SEND_VAR_EX                                                  !5
         22        DO_FCALL                                          0          
  105    23      > JMP                                                          ->18
         24    >   FE_FREE                                                      $20
  107    25      > RETURN                                                       1

Class Parser:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  __construct
number of ops:  5
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
   11     1        INIT_METHOD_CALL                                             'setSuccessor'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
   12     4      > RETURN                                                       null

End of function __construct

Function parse:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 11
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 14
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  parse
number of ops:  15
compiled vars:  !0 = $fileName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   14     0  E >   RECV                                                 !0      
   15     1        INIT_METHOD_CALL                                             'getSuccessor'
          2        DO_FCALL                                          0  $1      
          3        IS_NOT_EQUAL                                                 $1, null
          4      > JMPZ                                                         ~2, ->11
   16     5    >   INIT_METHOD_CALL                                             'getSuccessor'
          6        DO_FCALL                                          0  $3      
          7        INIT_METHOD_CALL                                             $3, 'parse'
          8        SEND_VAR_EX                                                  !0
          9        DO_FCALL                                          0          
   15    10      > JMP                                                          ->14
   18    11    >   CONCAT                                               ~5      'Unable+to+find+the+correct+parser+for+the+file%3A', !0
         12        CONCAT                                               ~6      ~5, '%0A'
         13        ECHO                                                         ~6
   20    14    > > RETURN                                                       null

End of function parse

Function canhandlefile:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/ukJtE
function name:  canHandleFile
number of ops:  16
compiled vars:  !0 = $fileName, !1 = $format, !2 = $spl
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   23     2        NEW                                                  $3      'SplFileInfo'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0          
          5        ASSIGN                                                       !2, $3
   24     6        IS_EQUAL                                             ~6      !0, null
          7      > JMPNZ_EX                                             ~6      ~6, ->12
          8    >   INIT_METHOD_CALL                                             !2, 'getExtension'
          9        DO_FCALL                                          0  $7      
         10        IS_EQUAL                                             ~8      !1, $7
         11        BOOL                                                 ~6      ~8
         12    >   VERIFY_RETURN_TYPE                                           ~6
         13      > RETURN                                                       ~6
   25    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null

End of function canhandlefile

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

End of function getsuccessor

Function setsuccessor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  setSuccessor
number of ops:  4
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     0  E >   RECV                                                 !0      
   32     1        ASSIGN_OBJ                                                   'successor'
          2        OP_DATA                                                      !0
   33     3      > RETURN                                                       null

End of function setsuccessor

End of class Parser.

Class TextParser:
Function parse:
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 = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  parse
number of ops:  14
compiled vars:  !0 = $fileName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   39     0  E >   RECV                                                 !0      
   40     1        INIT_METHOD_CALL                                             'canHandleFile'
          2        SEND_VAR_EX                                                  !0
          3        SEND_VAL_EX                                                  'txt'
          4        DO_FCALL                                          0  $1      
          5      > JMPZ                                                         $1, ->10
   41     6    >   CONCAT                                               ~2      'A+text+parser+is+handling+the+file%3A+', !0
          7        CONCAT                                               ~3      ~2, '%0A'
          8        ECHO                                                         ~3
   40     9      > JMP                                                          ->13
   43    10    >   INIT_STATIC_METHOD_CALL                                      'parse'
         11        SEND_VAR_EX                                                  !0
         12        DO_FCALL                                          0          
   45    13    > > RETURN                                                       null

End of function parse

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  __construct
number of ops:  5
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
   11     1        INIT_METHOD_CALL                                             'setSuccessor'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
   12     4      > RETURN                                                       null

End of function __construct

Function canhandlefile:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/ukJtE
function name:  canHandleFile
number of ops:  16
compiled vars:  !0 = $fileName, !1 = $format, !2 = $spl
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   23     2        NEW                                                  $3      'SplFileInfo'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0          
          5        ASSIGN                                                       !2, $3
   24     6        IS_EQUAL                                             ~6      !0, null
          7      > JMPNZ_EX                                             ~6      ~6, ->12
          8    >   INIT_METHOD_CALL                                             !2, 'getExtension'
          9        DO_FCALL                                          0  $7      
         10        IS_EQUAL                                             ~8      !1, $7
         11        BOOL                                                 ~6      ~8
         12    >   VERIFY_RETURN_TYPE                                           ~6
         13      > RETURN                                                       ~6
   25    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null

End of function canhandlefile

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

End of function getsuccessor

Function setsuccessor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  setSuccessor
number of ops:  4
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     0  E >   RECV                                                 !0      
   32     1        ASSIGN_OBJ                                                   'successor'
          2        OP_DATA                                                      !0
   33     3      > RETURN                                                       null

End of function setsuccessor

End of class TextParser.

Class JsonParser:
Function parse:
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 = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  parse
number of ops:  14
compiled vars:  !0 = $fileName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   51     0  E >   RECV                                                 !0      
   52     1        INIT_METHOD_CALL                                             'canHandleFile'
          2        SEND_VAR_EX                                                  !0
          3        SEND_VAL_EX                                                  'json'
          4        DO_FCALL                                          0  $1      
          5      > JMPZ                                                         $1, ->10
   53     6    >   CONCAT                                               ~2      'A+JSON+parser+is+handling+the+file%3A+', !0
          7        CONCAT                                               ~3      ~2, '%0A'
          8        ECHO                                                         ~3
   52     9      > JMP                                                          ->13
   55    10    >   INIT_STATIC_METHOD_CALL                                      'parse'
         11        SEND_VAR_EX                                                  !0
         12        DO_FCALL                                          0          
   57    13    > > RETURN                                                       null

End of function parse

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  __construct
number of ops:  5
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
   11     1        INIT_METHOD_CALL                                             'setSuccessor'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
   12     4      > RETURN                                                       null

End of function __construct

Function canhandlefile:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/ukJtE
function name:  canHandleFile
number of ops:  16
compiled vars:  !0 = $fileName, !1 = $format, !2 = $spl
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   23     2        NEW                                                  $3      'SplFileInfo'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0          
          5        ASSIGN                                                       !2, $3
   24     6        IS_EQUAL                                             ~6      !0, null
          7      > JMPNZ_EX                                             ~6      ~6, ->12
          8    >   INIT_METHOD_CALL                                             !2, 'getExtension'
          9        DO_FCALL                                          0  $7      
         10        IS_EQUAL                                             ~8      !1, $7
         11        BOOL                                                 ~6      ~8
         12    >   VERIFY_RETURN_TYPE                                           ~6
         13      > RETURN                                                       ~6
   25    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null

End of function canhandlefile

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

End of function getsuccessor

Function setsuccessor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  setSuccessor
number of ops:  4
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     0  E >   RECV                                                 !0      
   32     1        ASSIGN_OBJ                                                   'successor'
          2        OP_DATA                                                      !0
   33     3      > RETURN                                                       null

End of function setsuccessor

End of class JsonParser.

Class CSVParser:
Function parse:
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 = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  parse
number of ops:  14
compiled vars:  !0 = $fileName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   63     0  E >   RECV                                                 !0      
   64     1        INIT_METHOD_CALL                                             'canHandleFile'
          2        SEND_VAR_EX                                                  !0
          3        SEND_VAL_EX                                                  'json'
          4        DO_FCALL                                          0  $1      
          5      > JMPZ                                                         $1, ->10
   65     6    >   ADD                                                  ~2      'A+CSV+parser+is+handling+the+file%3A+', !0
          7        CONCAT                                               ~3      ~2, '%0A'
          8        ECHO                                                         ~3
   64     9      > JMP                                                          ->13
   67    10    >   INIT_STATIC_METHOD_CALL                                      'parse'
         11        SEND_VAR_EX                                                  !0
         12        DO_FCALL                                          0          
   69    13    > > RETURN                                                       null

End of function parse

Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  __construct
number of ops:  5
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   10     0  E >   RECV                                                 !0      
   11     1        INIT_METHOD_CALL                                             'setSuccessor'
          2        SEND_VAR_EX                                                  !0
          3        DO_FCALL                                          0          
   12     4      > RETURN                                                       null

End of function __construct

Function canhandlefile:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 8, Position 2 = 12
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
filename:       /in/ukJtE
function name:  canHandleFile
number of ops:  16
compiled vars:  !0 = $fileName, !1 = $format, !2 = $spl
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   22     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
   23     2        NEW                                                  $3      'SplFileInfo'
          3        SEND_VAR_EX                                                  !0
          4        DO_FCALL                                          0          
          5        ASSIGN                                                       !2, $3
   24     6        IS_EQUAL                                             ~6      !0, null
          7      > JMPNZ_EX                                             ~6      ~6, ->12
          8    >   INIT_METHOD_CALL                                             !2, 'getExtension'
          9        DO_FCALL                                          0  $7      
         10        IS_EQUAL                                             ~8      !1, $7
         11        BOOL                                                 ~6      ~8
         12    >   VERIFY_RETURN_TYPE                                           ~6
         13      > RETURN                                                       ~6
   25    14*       VERIFY_RETURN_TYPE                                           
         15*     > RETURN                                                       null

End of function canhandlefile

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

End of function getsuccessor

Function setsuccessor:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  setSuccessor
number of ops:  4
compiled vars:  !0 = $successor
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   31     0  E >   RECV                                                 !0      
   32     1        ASSIGN_OBJ                                                   'successor'
          2        OP_DATA                                                      !0
   33     3      > RETURN                                                       null

End of function setsuccessor

End of class CSVParser.

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

End of function __construct

Function parse:
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 = 42) Position 1 = 13
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ukJtE
function name:  parse
number of ops:  14
compiled vars:  !0 = $fileName
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   79     0  E >   RECV                                                 !0      
   80     1        INIT_METHOD_CALL                                             'canHandleFile'
          2        SEND_VAR_EX                                                  !0
          3        SEND_VAL_EX                                                  'json'
          4        DO_FCALL                                          0  $1      
          5      > JMPZ                                                         $1, ->10
   81     6    >   ADD                                                  ~2      'A+XML+parser+is+handling+the+file%3A+', !0
          7        CONCAT                                               ~3      ~2, '%0A'
          8        ECHO                                                         ~3
   80     9      > JMP                                                          ->13
   83    10    >   INIT_STATIC_METHOD_CALL                                      'parse'
         11        SEND_VAR_EX                                                  !0
         12        DO_FCALL                                          0          
   85    13    > > RETURN                                                       null

End of function parse

End of class XMLParser.

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
173.51 ms | 2168 KiB | 13 Q