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; } for ($_lesserNumIdx = 6; $_numeralIdx < $_lesserNumIdx; $_lesserNumIdx--) { $_lesserNumeralData = $this->_numerals[$_lesserNumIdx]; if ($number >= $_numeralData['integer'] - $_lesserNumeralData['integer']) { $numerals[] = $_lesserNumeralData['symbol']; $numerals[] = $_numeralData['symbol']; $number -= ($_numeralData['integer'] - $_lesserNumeralData['integer']); break(2); } } } } 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/kN5tC
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'
  111     1        NEW                                              $1      'JoshDiFabio%5CRomanNumeralGenerator'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
  112     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/kN5tC
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 = 60
Branch analysis from position: 60
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 21
Branch analysis from position: 62
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 125) Position 1 = 23, Position 2 = 59
Branch analysis from position: 23
2 jumps found. (Code = 126) Position 1 = 24, Position 2 = 59
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 = 59
Branch analysis from position: 59
2 jumps found. (Code = 44) Position 1 = 62, Position 2 = 21
Branch analysis from position: 62
Branch analysis from position: 21
Branch analysis from position: 34
1 jumps found. (Code = 42) Position 1 = 56
Branch analysis from position: 56
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 36
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 36
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 55
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 55
2 jumps found. (Code = 44) Position 1 = 58, Position 2 = 36
Branch analysis from position: 58
Branch analysis from position: 36
Branch analysis from position: 59
Branch analysis from position: 59
Branch analysis from position: 14
filename:       /in/kN5tC
function name:  generate
number of ops:  68
compiled vars:  !0 = $number, !1 = $numerals, !2 = $_numeralData, !3 = $_numeralIdx, !4 = $_lesserNumIdx, !5 = $_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  $6      
          4        BOOL_NOT                                         ~7      $6
          5      > JMPZ                                                     ~7, ->10
   72     6    >   NEW                                              $8      'InvalidArgumentException'
          7        SEND_VAL_EX                                              'Provided+argument+is+not+an+integer.'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $8
   74    10    >   IS_SMALLER                                       ~10     !0, 1
         11      > JMPNZ_EX                                         ~10     ~10, ->14
         12    >   IS_SMALLER                                       ~11     3999, !0
         13        BOOL                                             ~10     ~11
         14    > > JMPZ                                                     ~10, ->19
   75    15    >   NEW                                              $12     '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          $12
   79    19    >   ASSIGN                                                   !1, <array>
   81    20      > JMP                                                      ->60
   83    21    >   FETCH_OBJ_W                                      $15     '_numerals'
         22      > FE_RESET_RW                                      $16     $15, ->59
         23    > > FE_FETCH_RW                                      ~17     $16, !2, ->59
         24    >   ASSIGN                                                   !3, ~17
   89    25        FETCH_DIM_R                                      ~19     !2, 'integer'
         26        IS_SMALLER_OR_EQUAL                                      ~19, !0
         27      > JMPZ                                                     ~20, ->34
   90    28    >   FETCH_DIM_R                                      ~22     !2, 'symbol'
         29        ASSIGN_DIM                                               !1
         30        OP_DATA                                                  ~22
   91    31        FETCH_DIM_R                                      ~23     !2, 'integer'
         32        ASSIGN_OP                                     2          !0, ~23
   92    33      > JMP                                                      ->59
   95    34    >   ASSIGN                                                   !4, 6
         35      > JMP                                                      ->56
   96    36    >   FETCH_OBJ_R                                      ~26     '_numerals'
         37        FETCH_DIM_R                                      ~27     ~26, !4
         38        ASSIGN                                                   !5, ~27
   97    39        FETCH_DIM_R                                      ~29     !2, 'integer'
         40        FETCH_DIM_R                                      ~30     !5, 'integer'
         41        SUB                                              ~31     ~29, ~30
         42        IS_SMALLER_OR_EQUAL                                      ~31, !0
         43      > JMPZ                                                     ~32, ->55
   98    44    >   FETCH_DIM_R                                      ~34     !5, 'symbol'
         45        ASSIGN_DIM                                               !1
         46        OP_DATA                                                  ~34
   99    47        FETCH_DIM_R                                      ~36     !2, 'symbol'
         48        ASSIGN_DIM                                               !1
         49        OP_DATA                                                  ~36
  100    50        FETCH_DIM_R                                      ~37     !2, 'integer'
         51        FETCH_DIM_R                                      ~38     !5, 'integer'
         52        SUB                                              ~39     ~37, ~38
         53        ASSIGN_OP                                     2          !0, ~39
  101    54      > JMP                                                      ->59
   95    55    >   PRE_DEC                                                  !4
         56    >   IS_SMALLER                                               !3, !4
         57      > JMPNZ                                                    ~42, ->36
   83    58    > > JMP                                                      ->23
         59    >   FE_FREE                                                  $16
   81    60    >   IS_SMALLER                                               0, !0
         61      > JMPNZ                                                    ~43, ->21
  107    62    >   INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cimplode'
         63        SEND_VAL_EX                                              ''
         64        SEND_VAR_EX                                              !1
         65        DO_FCALL                                      0  $44     
         66      > RETURN                                                   $44
  108    67*     > RETURN                                                   null

End of function generate

End of class JoshDiFabio\RomanNumeralGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.77 ms | 1407 KiB | 16 Q