3v4l.org

run code in 300+ PHP versions simultaneously
<?php // just make this example code not pitch a bunch of errors. :P trait ItsAllFakeAnyway { public function __call($func, $args) { printf("%s::%s(%s)\n", __CLASS__, $func, json_encode($args)); } } /* Whisper TLDR: - Whisper is a time-series database format, like RRD but better. - Each Whisper file contains one or more Archives representing a series of points at varying precisions */ interface WhisperInterface { /*...*/ } class Whisper implements WhisperInterface { use ItsAllFakeAnyway; public function generateArchives() { while($header = $this->readBinFile()) { yield $this->createArchive($header); } } protected function createArchive($header) { return new Archive($header); } } interface ArchiveInterface { /*...*/ } class Archive implements ArchiveInterface { use ItsAllFakeAnyway; public function generatePoints() { while($point = $this->readBinFile()) { yield $point; } } } /* The intention of the WhisperAggregator class is to ingest metrics from many Whisper files and aggregate them into a single file. While you *can* configure the thing that initially ingests the metrics to do this *for* you, I did not do that initially and I have to backfill the aggregations as below. */ class WhisperAggregator { use ItsAllFakeAnyway; public function processWhisper(WhisperInterface $w) { foreach($w->generateArchives() as $a) { $this->processArchive($a); } } protected function processArchive(ArchiveInterface $a) { foreach($w->generatePoints() as $p) { $this->addPointToAggregatedArchive($a); } } } // eg: $a = new WhisperAggregator(); foreach(['a.wsp', 'b.wsp'] as $file) { $a->processWhisper(new Whisper($file)); } $a->writeTo('agg.wsp'); /* This works just fine until I have a case where I need to modify the point data in order to backfill aggregations that were entirely missing at certain points in time, eg a "count" aggregation. So to accomplish this I want to simply change each point value to 1. I've accomplished this as below, but I'm curious if there is an applicable design pattern for something like this that avoids the below extensions. I was considering a Decorator pattern, but I don't know how best to apply a Decorator while also maintaining the interface contract short of writing a passthrough method for every interface method, which would be far more work than extending the class. */ class WhisperShim extends Whisper { protected function createArchive($header) { return new ArchiveShim($header); } } class ArchiveShim extends Archive { public function generatePoints() { foreach(parent::generatePoints() as $point) { $point['value'] = 1; yield $point; } } } // eg: $a = new WhisperAggregator(); foreach(['a.wsp', 'b.wsp'] as $file) { $a->processWhisper(new WhisperShim($file)); } $a->writeTo('agg.wsp');
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 15
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 15
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 15
2 jumps found. (Code = 77) Position 1 = 25, Position 2 = 33
Branch analysis from position: 25
2 jumps found. (Code = 78) Position 1 = 26, Position 2 = 33
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 25
Branch analysis from position: 25
Branch analysis from position: 33
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 33
Branch analysis from position: 15
filename:       /in/FsKSO
function name:  (null)
number of ops:  38
compiled vars:  !0 = $a, !1 = $file
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   DECLARE_CLASS                                            'whisper'
   32     1        DECLARE_CLASS                                            'archive'
   45     2        DECLARE_CLASS                                            'whisperaggregator'
   62     3        NEW                                              $2      'WhisperAggregator'
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $2
   63     6      > FE_RESET_R                                       $5      <array>, ->15
          7    > > FE_FETCH_R                                               $5, !1, ->15
   64     8    >   INIT_METHOD_CALL                                         !0, 'processWhisper'
          9        NEW                                              $6      'Whisper'
         10        SEND_VAR_EX                                              !1
         11        DO_FCALL                                      0          
         12        SEND_VAR_NO_REF_EX                                       $6
         13        DO_FCALL                                      0          
   63    14      > JMP                                                      ->7
         15    >   FE_FREE                                                  $5
   66    16        INIT_METHOD_CALL                                         !0, 'writeTo'
         17        SEND_VAL_EX                                              'agg.wsp'
         18        DO_FCALL                                      0          
   73    19        DECLARE_CLASS                                            'whispershim', 'whisper'
   79    20        DECLARE_CLASS                                            'archiveshim', 'archive'
   89    21        NEW                                              $10     'WhisperAggregator'
         22        DO_FCALL                                      0          
         23        ASSIGN                                                   !0, $10
   90    24      > FE_RESET_R                                       $13     <array>, ->33
         25    > > FE_FETCH_R                                               $13, !1, ->33
   91    26    >   INIT_METHOD_CALL                                         !0, 'processWhisper'
         27        NEW                                              $14     'WhisperShim'
         28        SEND_VAR_EX                                              !1
         29        DO_FCALL                                      0          
         30        SEND_VAR_NO_REF_EX                                       $14
         31        DO_FCALL                                      0          
   90    32      > JMP                                                      ->25
         33    >   FE_FREE                                                  $13
   93    34        INIT_METHOD_CALL                                         !0, 'writeTo'
         35        SEND_VAL_EX                                              'agg.wsp'
         36        DO_FCALL                                      0          
         37      > RETURN                                                   1

Class ItsAllFakeAnyway:
Function __call:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FsKSO
function name:  __call
number of ops:  13
compiled vars:  !0 = $func, !1 = $args
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        INIT_FCALL                                               'printf'
          3        SEND_VAL                                                 '%25s%3A%3A%25s%28%25s%29%0A'
          4        FETCH_CLASS_NAME                                 ~2      
          5        SEND_VAL                                                 ~2
          6        SEND_VAR                                                 !0
          7        INIT_FCALL                                               'json_encode'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $3      
         10        SEND_VAR                                                 $3
         11        DO_ICALL                                                 
    7    12      > RETURN                                                   null

End of function __call

End of class ItsAllFakeAnyway.

Class WhisperInterface: [no user functions]
Class Whisper:
Function generatearchives:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 2
Branch analysis from position: 10
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 44) Position 1 = 10, Position 2 = 2
Branch analysis from position: 10
Branch analysis from position: 2
filename:       /in/FsKSO
function name:  generateArchives
number of ops:  11
compiled vars:  !0 = $header
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   GENERATOR_CREATE                                         
   21     1      > JMP                                                      ->6
   22     2    >   INIT_METHOD_CALL                                         'createArchive'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0  $1      
          5        YIELD                                                    $1
   21     6    >   INIT_METHOD_CALL                                         'readBinFile'
          7        DO_FCALL                                      0  $3      
          8        ASSIGN                                           ~4      !0, $3
          9      > JMPNZ                                                    ~4, ->2
   24    10    > > GENERATOR_RETURN                                         

End of function generatearchives

Function createarchive:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FsKSO
function name:  createArchive
number of ops:  6
compiled vars:  !0 = $header
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   RECV                                             !0      
   27     1        NEW                                              $1      'Archive'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   28     5*     > RETURN                                                   null

End of function createarchive

End of class Whisper.

Class ArchiveInterface: [no user functions]
Class Archive:
Function generatepoints:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 2
Branch analysis from position: 7
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 44) Position 1 = 7, Position 2 = 2
Branch analysis from position: 7
Branch analysis from position: 2
filename:       /in/FsKSO
function name:  generatePoints
number of ops:  8
compiled vars:  !0 = $point
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   35     0  E >   GENERATOR_CREATE                                         
   36     1      > JMP                                                      ->3
   37     2    >   YIELD                                                    !0
   36     3    >   INIT_METHOD_CALL                                         'readBinFile'
          4        DO_FCALL                                      0  $2      
          5        ASSIGN                                           ~3      !0, $2
          6      > JMPNZ                                                    ~3, ->2
   39     7    > > GENERATOR_RETURN                                         

End of function generatepoints

End of class Archive.

Class WhisperAggregator:
Function processwhisper:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/FsKSO
function name:  processWhisper
number of ops:  11
compiled vars:  !0 = $w, !1 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   49     1        INIT_METHOD_CALL                                         !0, 'generateArchives'
          2        DO_FCALL                                      0  $2      
          3      > FE_RESET_R                                       $3      $2, ->9
          4    > > FE_FETCH_R                                               $3, !1, ->9
   50     5    >   INIT_METHOD_CALL                                         'processArchive'
          6        SEND_VAR_EX                                              !1
          7        DO_FCALL                                      0          
   49     8      > JMP                                                      ->4
          9    >   FE_FREE                                                  $3
   52    10      > RETURN                                                   null

End of function processwhisper

Function processarchive:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 9
filename:       /in/FsKSO
function name:  processArchive
number of ops:  11
compiled vars:  !0 = $a, !1 = $w, !2 = $p
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   54     0  E >   RECV                                             !0      
   55     1        INIT_METHOD_CALL                                         !1, 'generatePoints'
          2        DO_FCALL                                      0  $3      
          3      > FE_RESET_R                                       $4      $3, ->9
          4    > > FE_FETCH_R                                               $4, !2, ->9
   56     5    >   INIT_METHOD_CALL                                         'addPointToAggregatedArchive'
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   55     8      > JMP                                                      ->4
          9    >   FE_FREE                                                  $4
   58    10      > RETURN                                                   null

End of function processarchive

End of class WhisperAggregator.

Class WhisperShim:
Function createarchive:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/FsKSO
function name:  createArchive
number of ops:  6
compiled vars:  !0 = $header
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   74     0  E >   RECV                                             !0      
   75     1        NEW                                              $1      'ArchiveShim'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4      > RETURN                                                   $1
   76     5*     > RETURN                                                   null

End of function createarchive

End of class WhisperShim.

Class ArchiveShim:
Function generatepoints:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 9
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 9
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 9
1 jumps found. (Code = 161) Position 1 = -2
Branch analysis from position: 9
filename:       /in/FsKSO
function name:  generatePoints
number of ops:  11
compiled vars:  !0 = $point
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   80     0  E >   GENERATOR_CREATE                                         
   81     1        INIT_STATIC_METHOD_CALL                                  'generatePoints'
          2        DO_FCALL                                      0  $1      
          3      > FE_RESET_R                                       $2      $1, ->9
          4    > > FE_FETCH_R                                               $2, !0, ->9
   82     5    >   ASSIGN_DIM                                               !0, 'value'
          6        OP_DATA                                                  1
   83     7        YIELD                                                    !0
   81     8      > JMP                                                      ->4
          9    >   FE_FREE                                                  $2
   85    10      > GENERATOR_RETURN                                         

End of function generatepoints

End of class ArchiveShim.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
205.1 ms | 1408 KiB | 17 Q