3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Validator { const GREATER = [self::class, 'greater']; const IS_TYPE = [self::class, 'isType']; public static function all(...$rules) { foreach ($rules as $def) { $rule = array_shift($def); if ($rule(...$def)) { continue; } return false; } return true; } public static function greater($value, $compare) : bool { return $value > $compare; } public static function isType($value, $type) : bool { return gettype($value) === $type; } } class Model { const RULES = [ 'foo' => [ 'integer', // shorthand for [Validator::IS_TYPE, 'integer'] [Validator::GREATER, 10] ] ]; protected $foo; public function set($name, $value) { if ($this->isValid($name, $value)) { $this->$name = $value; return; } throw new Exception("{$name} cannot be {$value}"); } function isValid($name, $value) : bool { $rules = []; foreach (self::RULES[$name] as $rule) { // convenience: turn strings into typehints if (is_string($rule)) { $rules[] = [Validator::IS_TYPE, $value]; continue; } // ... other convenience parsing ... // for fully defined rules, just add value arg at proper spot array_splice($rule, 1, 0, $value); $rules[] = $rule; } return Validator::ALL($rules); } } $m = new Model; $m->set('foo', 15); var_dump($m); $m->set('foo', 7);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iRMov
function name:  (null)
number of ops:  15
compiled vars:  !0 = $m
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   66     0  E >   NEW                                              $1      'Model'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   68     3        INIT_METHOD_CALL                                         !0, 'set'
          4        SEND_VAL_EX                                              'foo'
          5        SEND_VAL_EX                                              15
          6        DO_FCALL                                      0          
   69     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !0
          9        DO_ICALL                                                 
   71    10        INIT_METHOD_CALL                                         !0, 'set'
         11        SEND_VAL_EX                                              'foo'
         12        SEND_VAL_EX                                              7
         13        DO_FCALL                                      0          
         14      > RETURN                                                   1

Class Validator:
Function all:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 2, Position 2 = 16
Branch analysis from position: 2
2 jumps found. (Code = 78) Position 1 = 3, Position 2 = 16
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 13
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/iRMov
function name:  all
number of ops:  19
compiled vars:  !0 = $rules, !1 = $def, !2 = $rule
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV_VARIADIC                                    !0      
    9     1      > FE_RESET_R                                       $3      !0, ->16
          2    > > FE_FETCH_R                                               $3, !1, ->16
   10     3    >   INIT_FCALL                                               'array_shift'
          4        SEND_REF                                                 !1
          5        DO_ICALL                                         $4      
          6        ASSIGN                                                   !2, $4
   11     7        INIT_DYNAMIC_CALL                                        !2
          8        SEND_UNPACK                                              !1
          9        CHECK_UNDEF_ARGS                                         
         10        DO_FCALL                                      1  $6      
         11      > JMPZ                                                     $6, ->13
         12    > > JMP                                                      ->2
   12    13    >   FE_FREE                                                  $3
         14      > RETURN                                                   <false>
    9    15*       JMP                                                      ->2
         16    >   FE_FREE                                                  $3
   14    17      > RETURN                                                   <true>
   15    18*     > RETURN                                                   null

End of function all

Function greater:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iRMov
function name:  greater
number of ops:  7
compiled vars:  !0 = $value, !1 = $compare
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   18     2        IS_SMALLER                                       ~2      !1, !0
          3        VERIFY_RETURN_TYPE                                       ~2
          4      > RETURN                                                   ~2
   19     5*       VERIFY_RETURN_TYPE                                       
          6*     > RETURN                                                   null

End of function greater

Function istype:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/iRMov
function name:  isType
number of ops:  8
compiled vars:  !0 = $value, !1 = $type
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   22     2        GET_TYPE                                         ~2      !0
          3        IS_IDENTICAL                                     ~3      !1, ~2
          4        VERIFY_RETURN_TYPE                                       ~3
          5      > RETURN                                                   ~3
   23     6*       VERIFY_RETURN_TYPE                                       
          7*     > RETURN                                                   null

End of function istype

End of class Validator.

Class Model:
Function set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/iRMov
function name:  set
number of ops:  18
compiled vars:  !0 = $name, !1 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   38     2        INIT_METHOD_CALL                                         'isValid'
          3        SEND_VAR_EX                                              !0
          4        SEND_VAR_EX                                              !1
          5        DO_FCALL                                      0  $2      
          6      > JMPZ                                                     $2, ->10
   39     7    >   ASSIGN_OBJ                                               !0
          8        OP_DATA                                                  !1
   40     9      > RETURN                                                   null
   42    10    >   NEW                                              $4      'Exception'
         11        ROPE_INIT                                     3  ~6      !0
         12        ROPE_ADD                                      1  ~6      ~6, '+cannot+be+'
         13        ROPE_END                                      2  ~5      ~6, !1
         14        SEND_VAL_EX                                              ~5
         15        DO_FCALL                                      0          
         16      > THROW                                         0          $4
   43    17*     > RETURN                                                   null

End of function set

Function isvalid:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 22
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 22
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 13
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/iRMov
function name:  isValid
number of ops:  30
compiled vars:  !0 = $name, !1 = $value, !2 = $rules, !3 = $rule
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   46     2        ASSIGN                                                   !2, <array>
   47     3        FETCH_DIM_R                                      ~5      <array>, !0
          4      > FE_RESET_R                                       $6      ~5, ->22
          5    > > FE_FETCH_R                                               $6, !3, ->22
   50     6    >   TYPE_CHECK                                   64          !3
          7      > JMPZ                                                     ~7, ->13
   51     8    >   INIT_ARRAY                                       ~9      <array>
          9        ADD_ARRAY_ELEMENT                                ~9      !1
         10        ASSIGN_DIM                                               !2
         11        OP_DATA                                                  ~9
   52    12      > JMP                                                      ->5
   58    13    >   INIT_FCALL                                               'array_splice'
         14        SEND_REF                                                 !3
         15        SEND_VAL                                                 1
         16        SEND_VAL                                                 0
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                                 
   59    19        ASSIGN_DIM                                               !2
         20        OP_DATA                                                  !3
   47    21      > JMP                                                      ->5
         22    >   FE_FREE                                                  $6
   62    23        INIT_STATIC_METHOD_CALL                                  'Validator', 'ALL'
         24        SEND_VAR                                                 !2
         25        DO_FCALL                                      0  $12     
         26        VERIFY_RETURN_TYPE                                       $12
         27      > RETURN                                                   $12
   63    28*       VERIFY_RETURN_TYPE                                       
         29*     > RETURN                                                   null

End of function isvalid

End of class Model.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
177.37 ms | 1404 KiB | 19 Q