3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace JoshDiFabio; interface RomanNumeralGeneratorInterface { /** * @param int $number * @return string * @throws \InvalidArgumentException Argument not an integer or out of range * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public function generate($number); } class RomanNumeral { const ONE = 'I'; const FIVE = 'V'; const TEN = 'X'; const FIFTY = 'L'; const ONE_HUNDRED = 'C'; const FIVE_HUNDRED = 'D'; const ONE_THOUSAND = 'M'; } class RomanNumeralGenerator implements RomanNumeralGeneratorInterface { /** * @var array * @author Josh Di Fabio <joshdifabio@hotmail.com> */ private $_integerNumeralMap = array( 1000 => RomanNumeral::ONE_THOUSAND, 500 => RomanNumeral::FIVE_HUNDRED, 100 => RomanNumeral::ONE_HUNDRED, 50 => RomanNumeral::FIFTY, 10 => RomanNumeral::TEN, 5 => RomanNumeral::FIVE, 1 => RomanNumeral::ONE, ); /** * @param int $number * @return string * @throws \InvalidArgumentException Argument not an integer or out of range * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public function generate($number) { if (!is_integer($number)) { throw new \InvalidArgumentException('Provided argument is not an integer.'); } if (1 > $number || 3999 < $number) { throw new \InvalidArgumentException( 'The provided number is not within the allowed range of 1 to 3999.'); } $numerals = array(); while (0 < $number) { $_prevInteger = null; foreach ($this->_integerNumeralMap as $_integer => $_numeral) { if (!is_null($_prevInteger) && $number >= 4 * $_integer) { $numerals[] = $_numeral; $numerals[] = $this->_integerNumeralMap[$_prevInteger]; $number -= 4 * $_integer; break; } if ($number >= $_integer) { $numerals[] = $_numeral; $number -= $_integer; break; } $_prevInteger = $_integer; } } return implode('', $numerals); } } $generator = new RomanNumeralGenerator; echo $generator->generate(3999);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YkLXv
function name:  (null)
number of ops:  9
compiled vars:  !0 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   DECLARE_CLASS                                            'joshdifabio%5Cromannumeralgenerator'
   84     1        NEW                                              $1      'JoshDiFabio%5CRomanNumeralGenerator'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   85     4        INIT_METHOD_CALL                                         !0, 'generate'
          5        SEND_VAL_EX                                              3999
          6        DO_FCALL                                      0  $4      
          7        ECHO                                                     $4
          8      > RETURN                                                   1

Class JoshDiFabio\RomanNumeralGeneratorInterface:
Function generate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/YkLXv
function name:  generate
number of ops:  2
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1      > RETURN                                                   null

End of function generate

End of class JoshDiFabio\RomanNumeralGeneratorInterface.

Class JoshDiFabio\RomanNumeral: [no user functions]
Class JoshDiFabio\RomanNumeralGenerator:
Function generate:
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
2 jumps found. (Code = 47) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 19
Branch analysis from position: 15
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 53
Branch analysis from position: 53
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 21
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 24, Position 2 = 52
Branch analysis from position: 24
2 jumps found. (Code = 78) Position 1 = 25, Position 2 = 52
Branch analysis from position: 25
2 jumps found. (Code = 46) Position 1 = 31, Position 2 = 34
Branch analysis from position: 31
2 jumps found. (Code = 43) Position 1 = 35, Position 2 = 44
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
2 jumps found. (Code = 44) Position 1 = 55, Position 2 = 21
Branch analysis from position: 55
Branch analysis from position: 21
Branch analysis from position: 44
2 jumps found. (Code = 43) Position 1 = 46, Position 2 = 50
Branch analysis from position: 46
1 jumps found. (Code = 42) Position 1 = 52
Branch analysis from position: 52
Branch analysis from position: 50
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 34
Branch analysis from position: 52
Branch analysis from position: 52
Branch analysis from position: 14
filename:       /in/YkLXv
function name:  generate
number of ops:  61
compiled vars:  !0 = $number, !1 = $numerals, !2 = $_prevInteger, !3 = $_numeral, !4 = $_integer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   RECV                                             !0      
   50     1        INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cis_integer'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $5      
          4        BOOL_NOT                                         ~6      $5
          5      > JMPZ                                                     ~6, ->10
   51     6    >   NEW                                              $7      'InvalidArgumentException'
          7        SEND_VAL_EX                                              'Provided+argument+is+not+an+integer.'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $7
   53    10    >   IS_SMALLER                                       ~9      !0, 1
         11      > JMPNZ_EX                                         ~9      ~9, ->14
         12    >   IS_SMALLER                                       ~10     3999, !0
         13        BOOL                                             ~9      ~10
         14    > > JMPZ                                                     ~9, ->19
   54    15    >   NEW                                              $11     'InvalidArgumentException'
   55    16        SEND_VAL_EX                                              'The+provided+number+is+not+within+the+allowed+range+of+1+to+3999.'
         17        DO_FCALL                                      0          
         18      > THROW                                         0          $11
   58    19    >   ASSIGN                                                   !1, <array>
   60    20      > JMP                                                      ->53
   61    21    >   ASSIGN                                                   !2, null
   62    22        FETCH_OBJ_R                                      ~15     '_integerNumeralMap'
         23      > FE_RESET_R                                       $16     ~15, ->52
         24    > > FE_FETCH_R                                       ~17     $16, !3, ->52
         25    >   ASSIGN                                                   !4, ~17
   63    26        INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cis_null'
         27        SEND_VAR_EX                                              !2
         28        DO_FCALL                                      0  $19     
         29        BOOL_NOT                                         ~20     $19
         30      > JMPZ_EX                                          ~20     ~20, ->34
         31    >   MUL                                              ~21     !4, 4
         32        IS_SMALLER_OR_EQUAL                              ~22     ~21, !0
         33        BOOL                                             ~20     ~22
         34    > > JMPZ                                                     ~20, ->44
   64    35    >   ASSIGN_DIM                                               !1
         36        OP_DATA                                                  !3
   65    37        FETCH_OBJ_R                                      ~25     '_integerNumeralMap'
         38        FETCH_DIM_R                                      ~26     ~25, !2
         39        ASSIGN_DIM                                               !1
         40        OP_DATA                                                  ~26
   66    41        MUL                                              ~27     !4, 4
         42        ASSIGN_OP                                     2          !0, ~27
   67    43      > JMP                                                      ->52
   70    44    >   IS_SMALLER_OR_EQUAL                                      !4, !0
         45      > JMPZ                                                     ~29, ->50
   71    46    >   ASSIGN_DIM                                               !1
         47        OP_DATA                                                  !3
   72    48        ASSIGN_OP                                     2          !0, !4
   73    49      > JMP                                                      ->52
   76    50    >   ASSIGN                                                   !2, !4
   62    51      > JMP                                                      ->24
         52    >   FE_FREE                                                  $16
   60    53    >   IS_SMALLER                                               0, !0
         54      > JMPNZ                                                    ~33, ->21
   80    55    >   INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cimplode'
         56        SEND_VAL_EX                                              ''
         57        SEND_VAR_EX                                              !1
         58        DO_FCALL                                      0  $34     
         59      > RETURN                                                   $34
   81    60*     > RETURN                                                   null

End of function generate

End of class JoshDiFabio\RomanNumeralGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.54 ms | 1407 KiB | 18 Q