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 $_numerals = array( array( 'integer' => 1000, 'symbol' => RomanNumeral::ONE_THOUSAND, ), array( 'integer' => 500, 'symbol' => RomanNumeral::FIVE_HUNDRED, ), array( 'integer' => 100, 'symbol' => RomanNumeral::ONE_HUNDRED, ), array( 'integer' => 50, 'symbol' => RomanNumeral::FIFTY, ), array( 'integer' => 10, 'symbol' => RomanNumeral::TEN, ), array( 'integer' => 5, 'symbol' => RomanNumeral::FIVE, ), array( 'integer' => 1, 'symbol' => 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) { // loop through numerals from largest to smallest foreach ($this->_numerals as $_numeralIdx => &$_numeralData) { /* * if the input number less any already selected numerals is larger than the current * numeral, choose the current numeral and deduct its integer value from the input * number */ if ($number >= $_numeralData['integer']) { $numerals[] = $_numeralData['symbol']; $number -= $_numeralData['integer']; break; } $_lesserNumeralData = $this->_numerals[1 + $_numeralIdx]; if ($number >= $_numeralData['integer'] - $_lesserNumeralData['integer']) { $numerals[] = $_lesserNumeralData['symbol']; $number -= $_lesserNumeralData['integer']; $numerals[] = $_numeralData['symbol']; $number -= $_numeralData['integer']; break; } } } 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/EBDGF
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'
  110     1        NEW                                              $1      'JoshDiFabio%5CRomanNumeralGenerator'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
  111     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/EBDGF
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 = 56
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 21
Branch analysis from position: 58
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 125) Position 1 = 23, Position 2 = 55
Branch analysis from position: 23
2 jumps found. (Code = 126) Position 1 = 24, Position 2 = 55
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 28, Position 2 = 34
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 21
Branch analysis from position: 58
Branch analysis from position: 21
Branch analysis from position: 34
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 54
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 55
Branch analysis from position: 55
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 55
Branch analysis from position: 55
Branch analysis from position: 14
filename:       /in/EBDGF
function name:  generate
number of ops:  64
compiled vars:  !0 = $number, !1 = $numerals, !2 = $_numeralData, !3 = $_numeralIdx, !4 = $_lesserNumeralData
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   69     0  E >   RECV                                             !0      
   71     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
   72     6    >   NEW                                              $7      'InvalidArgumentException'
          7        SEND_VAL_EX                                              'Provided+argument+is+not+an+integer.'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $7
   74    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
   75    15    >   NEW                                              $11     'InvalidArgumentException'
   76    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
   79    19    >   ASSIGN                                                   !1, <array>
   81    20      > JMP                                                      ->56
   83    21    >   FETCH_OBJ_W                                      $14     '_numerals'
         22      > FE_RESET_RW                                      $15     $14, ->55
         23    > > FE_FETCH_RW                                      ~16     $15, !2, ->55
         24    >   ASSIGN                                                   !3, ~16
   89    25        FETCH_DIM_R                                      ~18     !2, 'integer'
         26        IS_SMALLER_OR_EQUAL                                      ~18, !0
         27      > JMPZ                                                     ~19, ->34
   90    28    >   FETCH_DIM_R                                      ~21     !2, 'symbol'
         29        ASSIGN_DIM                                               !1
         30        OP_DATA                                                  ~21
   91    31        FETCH_DIM_R                                      ~22     !2, 'integer'
         32        ASSIGN_OP                                     2          !0, ~22
   92    33      > JMP                                                      ->55
   95    34    >   ADD                                              ~25     1, !3
         35        FETCH_OBJ_R                                      ~24     '_numerals'
         36        FETCH_DIM_R                                      ~26     ~24, ~25
         37        ASSIGN                                                   !4, ~26
   96    38        FETCH_DIM_R                                      ~28     !2, 'integer'
         39        FETCH_DIM_R                                      ~29     !4, 'integer'
         40        SUB                                              ~30     ~28, ~29
         41        IS_SMALLER_OR_EQUAL                                      ~30, !0
         42      > JMPZ                                                     ~31, ->54
   97    43    >   FETCH_DIM_R                                      ~33     !4, 'symbol'
         44        ASSIGN_DIM                                               !1
         45        OP_DATA                                                  ~33
   98    46        FETCH_DIM_R                                      ~34     !4, 'integer'
         47        ASSIGN_OP                                     2          !0, ~34
   99    48        FETCH_DIM_R                                      ~37     !2, 'symbol'
         49        ASSIGN_DIM                                               !1
         50        OP_DATA                                                  ~37
  100    51        FETCH_DIM_R                                      ~38     !2, 'integer'
         52        ASSIGN_OP                                     2          !0, ~38
  101    53      > JMP                                                      ->55
   83    54    > > JMP                                                      ->23
         55    >   FE_FREE                                                  $15
   81    56    >   IS_SMALLER                                               0, !0
         57      > JMPNZ                                                    ~40, ->21
  106    58    >   INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cimplode'
         59        SEND_VAL_EX                                              ''
         60        SEND_VAR_EX                                              !1
         61        DO_FCALL                                      0  $41     
         62      > RETURN                                                   $41
  107    63*     > RETURN                                                   null

End of function generate

End of class JoshDiFabio\RomanNumeralGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.42 ms | 1407 KiB | 16 Q