3v4l.org

run code in 300+ PHP versions simultaneously
<?php declare(strict_types=1); namespace fema\enums; abstract class Enum { protected $name, $ordinal; /** * @return string the name of the enum */ public function name() : string { return $this->name; } /** * @return int a number representing this enum. Starts at 0. */ public function ordinal() : int { return $this->ordinal; } /** * Alias of name() * @return string the name of the enum */ public function __toString() : string { return $this->name(); } /** * @return Enum[] all the enums that are defined in the class */ public abstract static function getAll() : array; /** * Return a specific enum given its name * @param string $name the name of the enum * @return Enum the enum that corresponds to the passed name * @throws EnumNotFoundException if an enum with the passed name is not found */ public static function fromName(string $name) : Enum { foreach (static::getAll() as $item) { if ($item->name() === $name) { return $item; } } throw new EnumNotFoundException("The enum " . static::class . " with name '$name' wasn't found!"); } /** * Return a specific enum given its ordinal * @param int $ordinal the ordinal of the enum * @return Enum the enum that corresponds to the passed ordinal * @throws EnumNotFoundException if an enum with the passed ordinal is not found */ public static function fromOrdinal(int $ordinal) : Enum { foreach (static::getAll() as $item) { if ($item->ordinal() === $ordinal) { return $item; } } throw new EnumNotFoundException("The enum " . static::class . " with ordinal '$ordinal' wasn't found!"); } } class DocEnum extends Enum { private static $enums = null; private function __construct() { } /** * @inheritDoc */ public static function getAll() : array { if (static::$enums === null) { $last = new \ReflectionClass(static::class); /** @var \ReflectionClass[] $classes */ $classes = [$last]; while ($last->getParentClass()->getName() !== self::class) { //Put on top of array array_unshift($classes, $last = new \ReflectionClass($last->getParentClass()->getName())); } $enums = []; $ordinal = 0; foreach ($classes as $class) { $doc = $class->getDocComment(); $matches = []; if (preg_match_all('/^\\s*\\*\\s*@method\\s+static\\s+([A-Z]+)\\(\\)\\s*$/m', $doc, $matches) > 0) { foreach ($matches[1] as $name) { $enum = new DocEnum(); $enum->name = $name; $enum->ordinal = $ordinal++; $enums[] = $enum; } } } static::$enums = $enums; } return static::$enums; } }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R3EXj
function name:  (null)
number of ops:  3
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    7     0  E >   DECLARE_CLASS                                            'fema%5Cenums%5Cenum'
   70     1        DECLARE_CLASS                                            'fema%5Cenums%5Cdocenum', 'fema%5Cenums%5Cenum'
  109     2      > RETURN                                                   1

Class fema\enums\Enum:
Function name:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R3EXj
function name:  name
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   FETCH_OBJ_R                                      ~0      'name'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   16     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function name

Function ordinal:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R3EXj
function name:  ordinal
number of ops:  5
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   22     0  E >   FETCH_OBJ_R                                      ~0      'ordinal'
          1        VERIFY_RETURN_TYPE                                       ~0
          2      > RETURN                                                   ~0
   23     3*       VERIFY_RETURN_TYPE                                       
          4*     > RETURN                                                   null

End of function ordinal

Function __tostring:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R3EXj
function name:  __toString
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   INIT_METHOD_CALL                                         'name'
          1        DO_FCALL                                      0  $0      
          2        VERIFY_RETURN_TYPE                                       $0
          3      > RETURN                                                   $0
   31     4*       VERIFY_RETURN_TYPE                                       
          5*     > RETURN                                                   null

End of function __tostring

Function getall:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R3EXj
function name:  getAll
number of ops:  2
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   36     0  E >   VERIFY_RETURN_TYPE                                       
          1      > RETURN                                                   null

End of function getall

Function fromname:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
filename:       /in/R3EXj
function name:  fromName
number of ops:  26
compiled vars:  !0 = $name, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
   46     1        INIT_STATIC_METHOD_CALL                                  'getAll'
          2        DO_FCALL                                      0  $2      
          3      > FE_RESET_R                                       $3      $2, ->13
          4    > > FE_FETCH_R                                               $3, !1, ->13
   47     5    >   INIT_METHOD_CALL                                         !1, 'name'
          6        DO_FCALL                                      0  $4      
          7        IS_IDENTICAL                                             !0, $4
          8      > JMPZ                                                     ~5, ->12
   48     9    >   VERIFY_RETURN_TYPE                                       !1
         10        FE_FREE                                                  $3
         11      > RETURN                                                   !1
   46    12    > > JMP                                                      ->4
         13    >   FE_FREE                                                  $3
   51    14        NEW                                              $6      'fema%5Cenums%5CEnumNotFoundException'
         15        FETCH_CLASS_NAME                                 ~7      
         16        CONCAT                                           ~8      'The+enum+', ~7
         17        ROPE_INIT                                     3  ~10     '+with+name+%27'
         18        ROPE_ADD                                      1  ~10     ~10, !0
         19        ROPE_END                                      2  ~9      ~10, '%27+wasn%27t+found%21'
         20        CONCAT                                           ~12     ~8, ~9
         21        SEND_VAL_EX                                              ~12
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $6
   52    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function fromname

Function fromordinal:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 13
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 13
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 12
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 13
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 13
filename:       /in/R3EXj
function name:  fromOrdinal
number of ops:  26
compiled vars:  !0 = $ordinal, !1 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   60     0  E >   RECV                                             !0      
   61     1        INIT_STATIC_METHOD_CALL                                  'getAll'
          2        DO_FCALL                                      0  $2      
          3      > FE_RESET_R                                       $3      $2, ->13
          4    > > FE_FETCH_R                                               $3, !1, ->13
   62     5    >   INIT_METHOD_CALL                                         !1, 'ordinal'
          6        DO_FCALL                                      0  $4      
          7        IS_IDENTICAL                                             !0, $4
          8      > JMPZ                                                     ~5, ->12
   63     9    >   VERIFY_RETURN_TYPE                                       !1
         10        FE_FREE                                                  $3
         11      > RETURN                                                   !1
   61    12    > > JMP                                                      ->4
         13    >   FE_FREE                                                  $3
   66    14        NEW                                              $6      'fema%5Cenums%5CEnumNotFoundException'
         15        FETCH_CLASS_NAME                                 ~7      
         16        CONCAT                                           ~8      'The+enum+', ~7
         17        ROPE_INIT                                     3  ~10     '+with+ordinal+%27'
         18        ROPE_ADD                                      1  ~10     ~10, !0
         19        ROPE_END                                      2  ~9      ~10, '%27+wasn%27t+found%21'
         20        CONCAT                                           ~12     ~8, ~9
         21        SEND_VAL_EX                                              ~12
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $6
   67    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function fromordinal

End of class fema\enums\Enum.

Class fema\enums\DocEnum:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/R3EXj
function name:  __construct
number of ops:  1
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   76     0  E > > RETURN                                                   null

End of function __construct

Function getall:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 63
Branch analysis from position: 3
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 11
Branch analysis from position: 29
2 jumps found. (Code = 77) Position 1 = 32, Position 2 = 60
Branch analysis from position: 32
2 jumps found. (Code = 78) Position 1 = 33, Position 2 = 60
Branch analysis from position: 33
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 59
Branch analysis from position: 44
2 jumps found. (Code = 77) Position 1 = 46, Position 2 = 58
Branch analysis from position: 46
2 jumps found. (Code = 78) Position 1 = 47, Position 2 = 58
Branch analysis from position: 47
1 jumps found. (Code = 42) Position 1 = 46
Branch analysis from position: 46
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
Branch analysis from position: 58
Branch analysis from position: 59
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
Branch analysis from position: 11
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 11
Branch analysis from position: 29
Branch analysis from position: 11
Branch analysis from position: 63
filename:       /in/R3EXj
function name:  getAll
number of ops:  68
compiled vars:  !0 = $last, !1 = $classes, !2 = $enums, !3 = $ordinal, !4 = $class, !5 = $doc, !6 = $matches, !7 = $name, !8 = $enum
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   82     0  E >   FETCH_STATIC_PROP_R          unknown             ~9      'enums'
          1        TYPE_CHECK                                    2          ~9
          2      > JMPZ                                                     ~10, ->63
   83     3    >   NEW                                              $11     'ReflectionClass'
          4        FETCH_CLASS_NAME                                 ~12     
          5        SEND_VAL_EX                                              ~12
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !0, $11
   85     8        INIT_ARRAY                                       ~15     !0
          9        ASSIGN                                                   !1, ~15
   86    10      > JMP                                                      ->23
   88    11    >   INIT_NS_FCALL_BY_NAME                                    'fema%5Cenums%5Carray_unshift'
         12        SEND_VAR_EX                                              !1
         13        NEW                                              $17     'ReflectionClass'
         14        INIT_METHOD_CALL                                         !0, 'getParentClass'
         15        DO_FCALL                                      0  $18     
         16        INIT_METHOD_CALL                                         $18, 'getName'
         17        DO_FCALL                                      0  $19     
         18        SEND_VAR_NO_REF_EX                                       $19
         19        DO_FCALL                                      0          
         20        ASSIGN                                           ~21     !0, $17
         21        SEND_VAL_EX                                              ~21
         22        DO_FCALL                                      0          
   86    23    >   INIT_METHOD_CALL                                         !0, 'getParentClass'
         24        DO_FCALL                                      0  $23     
         25        INIT_METHOD_CALL                                         $23, 'getName'
         26        DO_FCALL                                      0  $24     
         27        IS_NOT_IDENTICAL                                         $24, 'fema%5Cenums%5CDocEnum'
         28      > JMPNZ                                                    ~25, ->11
   91    29    >   ASSIGN                                                   !2, <array>
   92    30        ASSIGN                                                   !3, 0
   93    31      > FE_RESET_R                                       $28     !1, ->60
         32    > > FE_FETCH_R                                               $28, !4, ->60
   94    33    >   INIT_METHOD_CALL                                         !4, 'getDocComment'
         34        DO_FCALL                                      0  $29     
         35        ASSIGN                                                   !5, $29
   95    36        ASSIGN                                                   !6, <array>
   96    37        INIT_NS_FCALL_BY_NAME                                    'fema%5Cenums%5Cpreg_match_all'
         38        SEND_VAL_EX                                              '%2F%5E%5Cs%2A%5C%2A%5Cs%2A%40method%5Cs%2Bstatic%5Cs%2B%28%5BA-Z%5D%2B%29%5C%28%5C%29%5Cs%2A%24%2Fm'
         39        SEND_VAR_EX                                              !5
         40        SEND_VAR_EX                                              !6
         41        DO_FCALL                                      0  $32     
         42        IS_SMALLER                                               0, $32
         43      > JMPZ                                                     ~33, ->59
   97    44    >   FETCH_DIM_R                                      ~34     !6, 1
         45      > FE_RESET_R                                       $35     ~34, ->58
         46    > > FE_FETCH_R                                               $35, !7, ->58
   98    47    >   NEW                                              $36     'fema%5Cenums%5CDocEnum'
         48        DO_FCALL                                      0          
         49        ASSIGN                                                   !8, $36
   99    50        ASSIGN_OBJ                                               !8, 'name'
         51        OP_DATA                                                  !7
  100    52        POST_INC                                         ~41     !3
         53        ASSIGN_OBJ                                               !8, 'ordinal'
         54        OP_DATA                                                  ~41
  101    55        ASSIGN_DIM                                               !2
         56        OP_DATA                                                  !8
   97    57      > JMP                                                      ->46
         58    >   FE_FREE                                                  $35
   93    59    > > JMP                                                      ->32
         60    >   FE_FREE                                                  $28
  105    61        ASSIGN_STATIC_PROP                                       'enums'
         62        OP_DATA                                                  !2
  107    63    >   FETCH_STATIC_PROP_R          unknown             ~44     'enums'
         64        VERIFY_RETURN_TYPE                                       ~44
         65      > RETURN                                                   ~44
  108    66*       VERIFY_RETURN_TYPE                                       
         67*     > RETURN                                                   null

End of function getall

End of class fema\enums\DocEnum.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
162.53 ms | 1412 KiB | 17 Q