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 FOUR = 'IV'; const FIVE = 'V'; const NINE = 'IX'; const TEN = 'X'; const FOURTY = 'XL'; const FIFTY = 'L'; const NINETY = 'XC'; const ONE_HUNDRED = 'C'; const FOUR_HUNDRED = 'CD'; const FIVE_HUNDRED = 'D'; const NINE_HUNDRED = 'CM'; const ONE_THOUSAND = 'M'; } class RomanNumeralGenerator implements RomanNumeralGeneratorInterface { /** * @var array * @author Josh Di Fabio <joshdifabio@hotmail.com> */ private $_integerNumeralMap = array( 1000 => RomanNumeral::ONE_THOUSAND, 900 => RomanNumeral::NINE_HUNDRED, 500 => RomanNumeral::FIVE_HUNDRED, 400 => RomanNumeral::FOUR_HUNDRED, 100 => RomanNumeral::ONE_HUNDRED, 90 => RomanNumeral::NINETY, 50 => RomanNumeral::FIFTY, 40 => RomanNumeral::FOURTY, 10 => RomanNumeral::TEN, 9 => RomanNumeral::NINE, 5 => RomanNumeral::FIVE, 4 => RomanNumeral::FOUR, 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) { foreach ($this->_integerNumeralMap as $_integer => $_numeral) { /* * 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 >= $_integer) { $numerals[] = $_numeral; $number -= $_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/NAb1n
function name:  (null)
number of ops:  9
compiled vars:  !0 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   32     0  E >   DECLARE_CLASS                                            'joshdifabio%5Cromannumeralgenerator'
   91     1        NEW                                              $1      'JoshDiFabio%5CRomanNumeralGenerator'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   92     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/NAb1n
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 = 33
Branch analysis from position: 33
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 21
Branch analysis from position: 35
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 21
2 jumps found. (Code = 77) Position 1 = 23, Position 2 = 32
Branch analysis from position: 23
2 jumps found. (Code = 78) Position 1 = 24, Position 2 = 32
Branch analysis from position: 24
2 jumps found. (Code = 43) Position 1 = 27, Position 2 = 31
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 44) Position 1 = 35, Position 2 = 21
Branch analysis from position: 35
Branch analysis from position: 21
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
Branch analysis from position: 32
Branch analysis from position: 32
Branch analysis from position: 14
filename:       /in/NAb1n
function name:  generate
number of ops:  41
compiled vars:  !0 = $number, !1 = $numerals, !2 = $_numeral, !3 = $_integer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   62     1        INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cis_integer'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0  $4      
          4        BOOL_NOT                                         ~5      $4
          5      > JMPZ                                                     ~5, ->10
   63     6    >   NEW                                              $6      'InvalidArgumentException'
          7        SEND_VAL_EX                                              'Provided+argument+is+not+an+integer.'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $6
   65    10    >   IS_SMALLER                                       ~8      !0, 1
         11      > JMPNZ_EX                                         ~8      ~8, ->14
         12    >   IS_SMALLER                                       ~9      3999, !0
         13        BOOL                                             ~8      ~9
         14    > > JMPZ                                                     ~8, ->19
   66    15    >   NEW                                              $10     'InvalidArgumentException'
   67    16        SEND_VAL_EX                                              'The+provided+number+is+not+within+the+allowed+range+of+1+to+3999.'
         17        DO_FCALL                                      0          
         18      > THROW                                         0          $10
   70    19    >   ASSIGN                                                   !1, <array>
   72    20      > JMP                                                      ->33
   73    21    >   FETCH_OBJ_R                                      ~13     '_integerNumeralMap'
         22      > FE_RESET_R                                       $14     ~13, ->32
         23    > > FE_FETCH_R                                       ~15     $14, !2, ->32
         24    >   ASSIGN                                                   !3, ~15
   79    25        IS_SMALLER_OR_EQUAL                                      !3, !0
         26      > JMPZ                                                     ~17, ->31
   80    27    >   ASSIGN_DIM                                               !1
         28        OP_DATA                                                  !2
   81    29        ASSIGN_OP                                     2          !0, !3
   82    30      > JMP                                                      ->32
   73    31    > > JMP                                                      ->23
         32    >   FE_FREE                                                  $14
   72    33    >   IS_SMALLER                                               0, !0
         34      > JMPNZ                                                    ~20, ->21
   87    35    >   INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Cimplode'
         36        SEND_VAL_EX                                              ''
         37        SEND_VAR_EX                                              !1
         38        DO_FCALL                                      0  $21     
         39      > RETURN                                                   $21
   88    40*     > RETURN                                                   null

End of function generate

End of class JoshDiFabio\RomanNumeralGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.93 ms | 1407 KiB | 16 Q