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'; /** * @return array * @author Josh Di Fabio <joshdifabio@hotmail.com> */ public static function getIntegerToNumeralMap() { return array( 1 => RomanNumeral::ONE, 4 => RomanNumeral::FOUR, 5 => RomanNumeral::FIVE, 9 => RomanNumeral::NINE, 10 => RomanNumeral::TEN, 40 => RomanNumeral::FOURTY, 50 => RomanNumeral::FIFTY, 90 => RomanNumeral::NINETY, 100 => RomanNumeral::ONE_HUNDRED, 400 => RomanNumeral::FOUR_HUNDRED, 500 => RomanNumeral::FIVE_HUNDRED, 900 => RomanNumeral::NINE_HUNDRED, 1000 => RomanNumeral::ONE_THOUSAND, ); } } class RomanNumeralGenerator implements 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) { 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.'); } $numeralString = ''; $integerToNumeralMap = RomanNumeral::getIntegerToNumeralMap(); krsort($integerToNumeralMap); while (0 < $number) { foreach ($integerToNumeralMap as $_integer => $_numeral) { /* * if the input number less any already selected numerals is larger than the current * numeral, select the current numeral and deduct its integer value from the input * number */ if ($number >= $_integer) { $numeralString .= $_numeral; $number -= $_integer; break; } } } return $numeralString; } } $generator = new RomanNumeralGenerator; echo $generator->generate(0);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dH0UH
function name:  (null)
number of ops:  9
compiled vars:  !0 = $generator
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   55     0  E >   DECLARE_CLASS                                            'joshdifabio%5Cromannumeralgenerator'
   96     1        NEW                                              $1      'JoshDiFabio%5CRomanNumeralGenerator'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $1
   97     4        INIT_METHOD_CALL                                         !0, 'generate'
          5        SEND_VAL_EX                                              0
          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/dH0UH
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:
Function getintegertonumeralmap:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dH0UH
function name:  getIntegerToNumeralMap
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E > > RETURN                                                   <array>
   52     1*     > RETURN                                                   null

End of function getintegertonumeralmap

End of class JoshDiFabio\RomanNumeral.

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 = 37
Branch analysis from position: 37
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 27
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 27
2 jumps found. (Code = 77) Position 1 = 28, Position 2 = 36
Branch analysis from position: 28
2 jumps found. (Code = 78) Position 1 = 29, Position 2 = 36
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 35
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 36
Branch analysis from position: 36
2 jumps found. (Code = 44) Position 1 = 39, Position 2 = 27
Branch analysis from position: 39
Branch analysis from position: 27
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 28
Branch analysis from position: 28
Branch analysis from position: 36
Branch analysis from position: 36
Branch analysis from position: 14
filename:       /in/dH0UH
function name:  generate
number of ops:  41
compiled vars:  !0 = $number, !1 = $numeralString, !2 = $integerToNumeralMap, !3 = $_numeral, !4 = $_integer
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   63     0  E >   RECV                                             !0      
   65     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
   66     6    >   NEW                                              $7      'InvalidArgumentException'
          7        SEND_VAL_EX                                              'Provided+argument+is+not+an+integer.'
          8        DO_FCALL                                      0          
          9      > THROW                                         0          $7
   68    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
   69    15    >   NEW                                              $11     'InvalidArgumentException'
   70    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
   73    19    >   ASSIGN                                                   !1, ''
   75    20        INIT_STATIC_METHOD_CALL                                  'JoshDiFabio%5CRomanNumeral', 'getIntegerToNumeralMap'
         21        DO_FCALL                                      0  $14     
         22        ASSIGN                                                   !2, $14
   76    23        INIT_NS_FCALL_BY_NAME                                    'JoshDiFabio%5Ckrsort'
         24        SEND_VAR_EX                                              !2
         25        DO_FCALL                                      0          
   77    26      > JMP                                                      ->37
   78    27    > > FE_RESET_R                                       $17     !2, ->36
         28    > > FE_FETCH_R                                       ~18     $17, !3, ->36
         29    >   ASSIGN                                                   !4, ~18
   84    30        IS_SMALLER_OR_EQUAL                                      !4, !0
         31      > JMPZ                                                     ~20, ->35
   85    32    >   ASSIGN_OP                                     8          !1, !3
   86    33        ASSIGN_OP                                     2          !0, !4
   87    34      > JMP                                                      ->36
   78    35    > > JMP                                                      ->28
         36    >   FE_FREE                                                  $17
   77    37    >   IS_SMALLER                                               0, !0
         38      > JMPNZ                                                    ~23, ->27
   92    39    > > RETURN                                                   !1
   93    40*     > RETURN                                                   null

End of function generate

End of class JoshDiFabio\RomanNumeralGenerator.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.17 ms | 1394 KiB | 16 Q