3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * TODO: Fix shitty doc. * * Provides type assertions for public object properties. All public properties are affected by this library. The * following assertions are provided: * * - Read-only property: the property can be accessed by everything, but only the object itself can modify it. This is * the default behavior af all public properties, unless configured otherwise. * - Guarded property: the property can be set by everything, but the value must pass a custom check for type/format, * provided by the object. * * In both cases the checks only apply for external callers (outside the object). Public properties can't be unset * (they can still be set to null, however). The trait also protects against dynamic properties being set on the object. */ trait PropAssert { // TODO: Move out all method bodies to another class, so code is loaded only if uses (i.e. with assertions enabled)? protected $__propAssert = []; /** * Initializes PropAssert. This method should be called once for each object, preferably at construction time. * * @return bool Always returns true, so it can be comfortably invoked in an assert statement. */ protected function initPropAssert(): bool { $refl = new \ReflectionClass(get_class($this)); $propList = $refl->getProperties(\ReflectionProperty::IS_PUBLIC); foreach ($propList as $prop) { $name = $prop->name; $this->__propAssert[$name] = [ 'guard' => null, 'value' => $this->{$name}, ]; unset($this->{$name}); } return true; } /** * Configured how a given public property is handled. By default, public properties are read-only. With this * method, * they can be set as writable, with a "guard" function, that checks the type and format of the value being set. * * @param string $name A public property name (the property should already be declared as public). * @param \Closure|null $guard A closure that guards the property while being set, or null to make the property * read-only (all public properties are read-only by default). If provided, the closure * should be in format ($value) => bool|string. The guard accepts a value about to be * set for the given property, and returns true if the value is valid, and false or a * string error message (for ex. "a string of length 6-32 chars"), if the value is * invalid. * @return bool Always returns true, so it can be comfortably invoked in an assert statement. * @throws \Error On bad input. */ protected function setPropAssert($name, ?\Closure $guard): bool { $this->__propAssertRequireDefined($name); $this->__propAssert[$name]['guard'] = $guard; return true; } // Internal method, don't invoke. public function __get($name) { $this->__propAssertRequireDefined($name); $isInternal = $this->__propAssertIsInternalCaller(); $prop = $this->__propAssert[$name]; return $this->__propAssert[$name]['value']; } // Internal method, don't invoke. public function __set($name, $value) { $this->__propAssertRequireDefined($name); $isInternal = $this->__propAssertIsInternalCaller(); $guard = $this->__propAssert[$name]['guard']; if (!$isInternal && !$guard) { throw new \TypeError('Cannot set read-only property ' . get_class($this) . '::$' . $name); } $result = $guard($value); if ($result === true) { $this->__propAssert[$name]['value'] = $value; } else { $details = is_string($result) ? ', expecting ' . $result : ''; throw new \TypeError('Invalid value for property ' . get_class($this) . '::$' . $name . $details); } } // Internal method, don't invoke. public function __isset($name) { $this->__propAssertRequireDefined($name); return $this->__propAssert[$name]['value'] === null; } // Internal method, don't invoke. public function __unset($name) { $this->__propAssertRequireDefined($name); throw new \TypeError('Cannot unset property ' . get_class($this) . '::$' . $name); } // Internal method, don't invoke. private function __propAssertRequireDefined($name) { if (!isset($this->__propAssert[$name])) { throw new \TypeError('Undefined property ' . get_class($this) . '::$' . $name); } } // Internal method, don't invoke. private function __propAssertIsInternalCaller() { // TODO: Relax this check, so static methods of the class and cross-object calls can skip checks? $object = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS, 3)[2]['object'] ?? null; return $this === $object; } } // -------------------------------------------------------------------------------------------------------------------- class Foo { use PropAssert; public $a = 10, $b = 20; function __construct() { $this->initPropAssert(); $this->setPropAssert('b', function ($v) { if (!is_int($v)) return 'an integer, you moran'; return true; }); } } $foo = new Foo(); echo $foo->a . PHP_EOL; echo $foo->b . PHP_EOL; $foo->b = 123; echo $foo->b . PHP_EOL; try { $foo->b = 'moran'; } catch (\Throwable $e) { echo $e->getMessage() . PHP_EOL; } try { $foo->a = 100; } catch (\Throwable $e) { echo $e->getMessage() . PHP_EOL; }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Found catch point at position: 18
Branch analysis from position: 18
2 jumps found. (Code = 107) Position 1 = 19, Position 2 = -2
Branch analysis from position: 19
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
Found catch point at position: 26
Branch analysis from position: 26
2 jumps found. (Code = 107) Position 1 = 27, Position 2 = -2
Branch analysis from position: 27
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  (null)
number of ops:  32
compiled vars:  !0 = $foo, !1 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  121     0  E >   DECLARE_CLASS                                            'foo'
  136     1        NEW                                              $2      'Foo'
          2        DO_FCALL                                      0          
          3        ASSIGN                                                   !0, $2
  137     4        FETCH_OBJ_R                                      ~5      !0, 'a'
          5        CONCAT                                           ~6      ~5, '%0A'
          6        ECHO                                                     ~6
  138     7        FETCH_OBJ_R                                      ~7      !0, 'b'
          8        CONCAT                                           ~8      ~7, '%0A'
          9        ECHO                                                     ~8
  140    10        ASSIGN_OBJ                                               !0, 'b'
         11        OP_DATA                                                  123
  141    12        FETCH_OBJ_R                                      ~10     !0, 'b'
         13        CONCAT                                           ~11     ~10, '%0A'
         14        ECHO                                                     ~11
  144    15        ASSIGN_OBJ                                               !0, 'b'
         16        OP_DATA                                                  'moran'
         17      > JMP                                                      ->23
  145    18  E > > CATCH                                       last         'Throwable'
  146    19    >   INIT_METHOD_CALL                                         !1, 'getMessage'
         20        DO_FCALL                                      0  $13     
         21        CONCAT                                           ~14     $13, '%0A'
         22        ECHO                                                     ~14
  151    23    >   ASSIGN_OBJ                                               !0, 'a'
         24        OP_DATA                                                  100
         25      > JMP                                                      ->31
  152    26  E > > CATCH                                       last         'Throwable'
  153    27    >   INIT_METHOD_CALL                                         !1, 'getMessage'
         28        DO_FCALL                                      0  $16     
         29        CONCAT                                           ~17     $16, '%0A'
         30        ECHO                                                     ~17
  154    31    > > RETURN                                                   1

Function %00%7Bclosure%7D%2Fin%2F9YFRE%3A129%240:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 5
Branch analysis from position: 4
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  {closure}
number of ops:  7
compiled vars:  !0 = $v
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  129     0  E >   RECV                                             !0      
  130     1        TYPE_CHECK                                   16  ~1      !0
          2        BOOL_NOT                                         ~2      ~1
          3      > JMPZ                                                     ~2, ->5
          4    > > RETURN                                                   'an+integer%2C+you+moran'
  131     5    > > RETURN                                                   <true>
  132     6*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2F9YFRE%3A129%240

Class PropAssert:
Function initpropassert:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 11, Position 2 = 22
Branch analysis from position: 11
2 jumps found. (Code = 78) Position 1 = 12, Position 2 = 22
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 11
Branch analysis from position: 11
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
filename:       /in/9YFRE
function name:  initPropAssert
number of ops:  26
compiled vars:  !0 = $refl, !1 = $propList, !2 = $prop, !3 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   27     0  E >   NEW                                              $4      'ReflectionClass'
          1        FETCH_THIS                                       ~5      
          2        GET_CLASS                                        ~6      ~5
          3        SEND_VAL_EX                                              ~6
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !0, $4
   28     6        INIT_METHOD_CALL                                         !0, 'getProperties'
          7        SEND_VAL_EX                                              1
          8        DO_FCALL                                      0  $9      
          9        ASSIGN                                                   !1, $9
   30    10      > FE_RESET_R                                       $11     !1, ->22
         11    > > FE_FETCH_R                                               $11, !2, ->22
   31    12    >   FETCH_OBJ_R                                      ~12     !2, 'name'
         13        ASSIGN                                                   !3, ~12
   33    14        INIT_ARRAY                                       ~16     null, 'guard'
   34    15        FETCH_OBJ_R                                      ~17     !3
         16        ADD_ARRAY_ELEMENT                                ~16     ~17, 'value'
   32    17        FETCH_OBJ_W                                      $14     '__propAssert'
         18        ASSIGN_DIM                                               $14, !3
   34    19        OP_DATA                                                  ~16
   36    20        UNSET_OBJ                                                !3
   30    21      > JMP                                                      ->11
         22    >   FE_FREE                                                  $11
   39    23      > RETURN                                                   <true>
   40    24*       VERIFY_RETURN_TYPE                                       
         25*     > RETURN                                                   null

End of function initpropassert

Function setpropassert:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  setPropAssert
number of ops:  12
compiled vars:  !0 = $name, !1 = $guard
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   57     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   58     2        INIT_METHOD_CALL                                         '__propAssertRequireDefined'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   59     5        FETCH_OBJ_W                                      $3      '__propAssert'
          6        FETCH_DIM_W                                      $4      $3, !0
          7        ASSIGN_DIM                                               $4, 'guard'
          8        OP_DATA                                                  !1
   60     9      > RETURN                                                   <true>
   61    10*       VERIFY_RETURN_TYPE                                       
         11*     > RETURN                                                   null

End of function setpropassert

Function __get:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  __get
number of ops:  15
compiled vars:  !0 = $name, !1 = $isInternal, !2 = $prop
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   64     0  E >   RECV                                             !0      
   65     1        INIT_METHOD_CALL                                         '__propAssertRequireDefined'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   66     4        INIT_METHOD_CALL                                         '__propAssertIsInternalCaller'
          5        DO_FCALL                                      0  $4      
          6        ASSIGN                                                   !1, $4
   67     7        FETCH_OBJ_R                                      ~6      '__propAssert'
          8        FETCH_DIM_R                                      ~7      ~6, !0
          9        ASSIGN                                                   !2, ~7
   69    10        FETCH_OBJ_R                                      ~9      '__propAssert'
         11        FETCH_DIM_R                                      ~10     ~9, !0
         12        FETCH_DIM_R                                      ~11     ~10, 'value'
         13      > RETURN                                                   ~11
   70    14*     > RETURN                                                   null

End of function __get

Function __set:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 26
Branch analysis from position: 17
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 26
2 jumps found. (Code = 43) Position 1 = 32, Position 2 = 37
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 54
Branch analysis from position: 54
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 37
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 42
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 43
Branch analysis from position: 43
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 42
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 16
filename:       /in/9YFRE
function name:  __set
number of ops:  55
compiled vars:  !0 = $name, !1 = $value, !2 = $isInternal, !3 = $guard, !4 = $result, !5 = $details
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   73     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   74     2        INIT_METHOD_CALL                                         '__propAssertRequireDefined'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
   75     5        INIT_METHOD_CALL                                         '__propAssertIsInternalCaller'
          6        DO_FCALL                                      0  $7      
          7        ASSIGN                                                   !2, $7
   76     8        FETCH_OBJ_R                                      ~9      '__propAssert'
          9        FETCH_DIM_R                                      ~10     ~9, !0
         10        FETCH_DIM_R                                      ~11     ~10, 'guard'
         11        ASSIGN                                                   !3, ~11
   78    12        BOOL_NOT                                         ~13     !2
         13      > JMPZ_EX                                          ~13     ~13, ->16
         14    >   BOOL_NOT                                         ~14     !3
         15        BOOL                                             ~13     ~14
         16    > > JMPZ                                                     ~13, ->26
   79    17    >   NEW                                              $15     'TypeError'
         18        FETCH_THIS                                       ~16     
         19        GET_CLASS                                        ~17     ~16
         20        CONCAT                                           ~18     'Cannot+set+read-only+property+', ~17
         21        CONCAT                                           ~19     ~18, '%3A%3A%24'
         22        CONCAT                                           ~20     ~19, !0
         23        SEND_VAL_EX                                              ~20
         24        DO_FCALL                                      0          
         25      > THROW                                         0          $15
   82    26    >   INIT_DYNAMIC_CALL                                        !3
         27        SEND_VAR_EX                                              !1
         28        DO_FCALL                                      0  $22     
         29        ASSIGN                                                   !4, $22
   84    30        TYPE_CHECK                                    8          !4
         31      > JMPZ                                                     ~24, ->37
   85    32    >   FETCH_OBJ_W                                      $25     '__propAssert'
         33        FETCH_DIM_W                                      $26     $25, !0
         34        ASSIGN_DIM                                               $26, 'value'
         35        OP_DATA                                                  !1
         36      > JMP                                                      ->54
   87    37    >   TYPE_CHECK                                   64          !4
         38      > JMPZ                                                     ~28, ->42
         39    >   CONCAT                                           ~29     '%2C+expecting+', !4
         40        QM_ASSIGN                                        ~30     ~29
         41      > JMP                                                      ->43
         42    >   QM_ASSIGN                                        ~30     ''
         43    >   ASSIGN                                                   !5, ~30
   88    44        NEW                                              $32     'TypeError'
         45        FETCH_THIS                                       ~33     
         46        GET_CLASS                                        ~34     ~33
         47        CONCAT                                           ~35     'Invalid+value+for+property+', ~34
         48        CONCAT                                           ~36     ~35, '%3A%3A%24'
         49        CONCAT                                           ~37     ~36, !0
         50        CONCAT                                           ~38     ~37, !5
         51        SEND_VAL_EX                                              ~38
         52        DO_FCALL                                      0          
         53      > THROW                                         0          $32
   90    54    > > RETURN                                                   null

End of function __set

Function __isset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  __isset
number of ops:  10
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   93     0  E >   RECV                                             !0      
   94     1        INIT_METHOD_CALL                                         '__propAssertRequireDefined'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
   95     4        FETCH_OBJ_R                                      ~2      '__propAssert'
          5        FETCH_DIM_R                                      ~3      ~2, !0
          6        FETCH_DIM_R                                      ~4      ~3, 'value'
          7        TYPE_CHECK                                    2  ~5      ~4
          8      > RETURN                                                   ~5
   96     9*     > RETURN                                                   null

End of function __isset

Function __unset:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 108) Position 1 = -2
filename:       /in/9YFRE
function name:  __unset
number of ops:  14
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   99     0  E >   RECV                                             !0      
  100     1        INIT_METHOD_CALL                                         '__propAssertRequireDefined'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
  101     4        NEW                                              $2      'TypeError'
          5        FETCH_THIS                                       ~3      
          6        GET_CLASS                                        ~4      ~3
          7        CONCAT                                           ~5      'Cannot+unset+property+', ~4
          8        CONCAT                                           ~6      ~5, '%3A%3A%24'
          9        CONCAT                                           ~7      ~6, !0
         10        SEND_VAL_EX                                              ~7
         11        DO_FCALL                                      0          
         12      > THROW                                         0          $2
  102    13*     > RETURN                                                   null

End of function __unset

Function __propassertrequiredefined:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 14
Branch analysis from position: 5
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  __propAssertRequireDefined
number of ops:  15
compiled vars:  !0 = $name
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  105     0  E >   RECV                                             !0      
  106     1        FETCH_OBJ_IS                                     ~1      '__propAssert'
          2        ISSET_ISEMPTY_DIM_OBJ                         0  ~2      ~1, !0
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ                                                     ~3, ->14
  107     5    >   NEW                                              $4      'TypeError'
          6        FETCH_THIS                                       ~5      
          7        GET_CLASS                                        ~6      ~5
          8        CONCAT                                           ~7      'Undefined+property+', ~6
          9        CONCAT                                           ~8      ~7, '%3A%3A%24'
         10        CONCAT                                           ~9      ~8, !0
         11        SEND_VAL_EX                                              ~9
         12        DO_FCALL                                      0          
         13      > THROW                                         0          $4
  109    14    > > RETURN                                                   null

End of function __propassertrequiredefined

Function __propassertisinternalcaller:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  __propAssertIsInternalCaller
number of ops:  13
compiled vars:  !0 = $object
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  114     0  E >   INIT_FCALL                                               'debug_backtrace'
          1        SEND_VAL                                                 3
          2        SEND_VAL                                                 3
          3        DO_ICALL                                         $1      
          4        FETCH_DIM_IS                                     ~2      $1, 2
          5        FETCH_DIM_IS                                     ~3      ~2, 'object'
          6        COALESCE                                         ~4      ~3
          7        QM_ASSIGN                                        ~4      null
          8        ASSIGN                                                   !0, ~4
  115     9        FETCH_THIS                                       ~6      
         10        IS_IDENTICAL                                     ~7      !0, ~6
         11      > RETURN                                                   ~7
  116    12*     > RETURN                                                   null

End of function __propassertisinternalcaller

End of class PropAssert.

Class Foo:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/9YFRE
function name:  __construct
number of ops:  8
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  127     0  E >   INIT_METHOD_CALL                                         'initPropAssert'
          1        DO_FCALL                                      0          
  129     2        INIT_METHOD_CALL                                         'setPropAssert'
          3        SEND_VAL_EX                                              'b'
          4        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2F9YFRE%3A129%240'
  132     5        SEND_VAL_EX                                              ~1
          6        DO_FCALL                                      0          
  133     7      > RETURN                                                   null

End of function __construct

End of class Foo.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
158.34 ms | 1416 KiB | 15 Q