3v4l.org

run code in 300+ PHP versions simultaneously
<?php function hasImplicitNullDefaultValue(\ReflectionProperty $reflection): bool { if (null !== $reflection->getDefaultValue()) { return false; } if (false === $reflection->getDocComment()) { return true; } $docComment = $reflection->getDocComment(); if (!preg_match('/@var\s+([^\s]+)/', $docComment, $matches)) { return true; } return false !== strpos(strtolower($matches[1]), 'null'); } class foo { public $foo; /** * @var int */ public $bar; /** * @var int|null */ public $baz; } echo '$foo' . PHP_EOL; echo 'Implicit null set. As we do not know otherwise null is fine' . PHP_EOL; $reflection = new ReflectionProperty('foo', 'foo'); var_Dump($reflection->hasDefaultValue()); var_Dump($reflection->getDefaultValue()); var_Dump(hasImplicitNullDefaultValue($reflection)); echo PHP_EOL . '$bar' . PHP_EOL; echo 'null can not be an implicit value as this should be an int' . PHP_EOL; $reflection = new ReflectionProperty('foo', 'bar'); var_Dump($reflection->hasDefaultValue()); var_Dump($reflection->getDefaultValue()); var_Dump(hasImplicitNullDefaultValue($reflection)); echo PHP_EOL . '$baz' . PHP_EOL; echo 'Null is explicitly allowed as type, so the implicit null is fine' . PHP_EOL; $reflection = new ReflectionProperty('foo', 'baz'); var_Dump($reflection->hasDefaultValue()); var_Dump($reflection->getDefaultValue()); var_Dump(hasImplicitNullDefaultValue($reflection));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O70Fj
function name:  (null)
number of ops:  70
compiled vars:  !0 = $reflection
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   ECHO                                                     '%24foo%0A'
   39     1        ECHO                                                     'Implicit+null+set.+As+we+do+not+know+otherwise+null+is+fine%0A'
   40     2        NEW                                              $1      'ReflectionProperty'
          3        SEND_VAL_EX                                              'foo'
          4        SEND_VAL_EX                                              'foo'
          5        DO_FCALL                                      0          
          6        ASSIGN                                                   !0, $1
   41     7        INIT_FCALL                                               'var_dump'
          8        INIT_METHOD_CALL                                         !0, 'hasDefaultValue'
          9        DO_FCALL                                      0  $4      
         10        SEND_VAR                                                 $4
         11        DO_ICALL                                                 
   42    12        INIT_FCALL                                               'var_dump'
         13        INIT_METHOD_CALL                                         !0, 'getDefaultValue'
         14        DO_FCALL                                      0  $6      
         15        SEND_VAR                                                 $6
         16        DO_ICALL                                                 
   43    17        INIT_FCALL                                               'var_dump'
         18        INIT_FCALL                                               'hasimplicitnulldefaultvalue'
         19        SEND_VAR                                                 !0
         20        DO_FCALL                                      0  $8      
         21        SEND_VAR                                                 $8
         22        DO_ICALL                                                 
   45    23        ECHO                                                     '%0A%24bar%0A'
   46    24        ECHO                                                     'null+can+not+be+an+implicit+value+as+this+should+be+an+int%0A'
   47    25        NEW                                              $10     'ReflectionProperty'
         26        SEND_VAL_EX                                              'foo'
         27        SEND_VAL_EX                                              'bar'
         28        DO_FCALL                                      0          
         29        ASSIGN                                                   !0, $10
   48    30        INIT_FCALL                                               'var_dump'
         31        INIT_METHOD_CALL                                         !0, 'hasDefaultValue'
         32        DO_FCALL                                      0  $13     
         33        SEND_VAR                                                 $13
         34        DO_ICALL                                                 
   49    35        INIT_FCALL                                               'var_dump'
         36        INIT_METHOD_CALL                                         !0, 'getDefaultValue'
         37        DO_FCALL                                      0  $15     
         38        SEND_VAR                                                 $15
         39        DO_ICALL                                                 
   50    40        INIT_FCALL                                               'var_dump'
         41        INIT_FCALL                                               'hasimplicitnulldefaultvalue'
         42        SEND_VAR                                                 !0
         43        DO_FCALL                                      0  $17     
         44        SEND_VAR                                                 $17
         45        DO_ICALL                                                 
   52    46        ECHO                                                     '%0A%24baz%0A'
   53    47        ECHO                                                     'Null+is+explicitly+allowed+as+type%2C+so+the+implicit+null+is+fine%0A'
   54    48        NEW                                              $19     'ReflectionProperty'
         49        SEND_VAL_EX                                              'foo'
         50        SEND_VAL_EX                                              'baz'
         51        DO_FCALL                                      0          
         52        ASSIGN                                                   !0, $19
   55    53        INIT_FCALL                                               'var_dump'
         54        INIT_METHOD_CALL                                         !0, 'hasDefaultValue'
         55        DO_FCALL                                      0  $22     
         56        SEND_VAR                                                 $22
         57        DO_ICALL                                                 
   56    58        INIT_FCALL                                               'var_dump'
         59        INIT_METHOD_CALL                                         !0, 'getDefaultValue'
         60        DO_FCALL                                      0  $24     
         61        SEND_VAR                                                 $24
         62        DO_ICALL                                                 
   57    63        INIT_FCALL                                               'var_dump'
         64        INIT_FCALL                                               'hasimplicitnulldefaultvalue'
         65        SEND_VAR                                                 !0
         66        DO_FCALL                                      0  $26     
         67        SEND_VAR                                                 $26
         68        DO_ICALL                                                 
         69      > RETURN                                                   1

Function hasimplicitnulldefaultvalue:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 10, Position 2 = 11
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 22
Branch analysis from position: 21
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 22
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/O70Fj
function name:  hasImplicitNullDefaultValue
number of ops:  35
compiled vars:  !0 = $reflection, !1 = $docComment, !2 = $matches
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
    5     1        INIT_METHOD_CALL                                         !0, 'getDefaultValue'
          2        DO_FCALL                                      0  $3      
          3        TYPE_CHECK                                  1020          $3
          4      > JMPZ                                                     ~4, ->6
    6     5    > > RETURN                                                   <false>
    9     6    >   INIT_METHOD_CALL                                         !0, 'getDocComment'
          7        DO_FCALL                                      0  $5      
          8        TYPE_CHECK                                    4          $5
          9      > JMPZ                                                     ~6, ->11
   10    10    > > RETURN                                                   <true>
   13    11    >   INIT_METHOD_CALL                                         !0, 'getDocComment'
         12        DO_FCALL                                      0  $7      
         13        ASSIGN                                                   !1, $7
   14    14        INIT_FCALL                                               'preg_match'
         15        SEND_VAL                                                 '%2F%40var%5Cs%2B%28%5B%5E%5Cs%5D%2B%29%2F'
         16        SEND_VAR                                                 !1
         17        SEND_REF                                                 !2
         18        DO_ICALL                                         $9      
         19        BOOL_NOT                                         ~10     $9
         20      > JMPZ                                                     ~10, ->22
   15    21    > > RETURN                                                   <true>
   18    22    >   INIT_FCALL                                               'strpos'
         23        INIT_FCALL                                               'strtolower'
         24        FETCH_DIM_R                                      ~11     !2, 1
         25        SEND_VAL                                                 ~11
         26        DO_ICALL                                         $12     
         27        SEND_VAR                                                 $12
         28        SEND_VAL                                                 'null'
         29        DO_ICALL                                         $13     
         30        TYPE_CHECK                                  1018  ~14     $13
         31        VERIFY_RETURN_TYPE                                       ~14
         32      > RETURN                                                   ~14
   19    33*       VERIFY_RETURN_TYPE                                       
         34*     > RETURN                                                   null

End of function hasimplicitnulldefaultvalue

Class foo: [no user functions]

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
151.11 ms | 1025 KiB | 20 Q