3v4l.org

run code in 300+ PHP versions simultaneously
<?php class T { } class X { public function __construct(T $t) { $this->t = $t; } } class Builder { /** * @var T|null */ public $t; public function createX(): X { $this->validate(); // assert($this->t !== NULL); works here xD // PHPStan complains that at this line, $this->t could be null, except we've already checked it isn't... // Parameter #1 $t of class X constructor expects T, T|null given. return new X($this->t); } private function validate() { // assert($this->t !== NULL); does not work here xD if (is_null($this->t)) { throw new \Exception; } } } $builder = new Builder; $builder->t = new T; $builder->createX();
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BeN7t
function name:  (null)
number of ops:  10
compiled vars:  !0 = $builder
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   40     0  E >   NEW                                              $1      'Builder'
          1        DO_FCALL                                      0          
          2        ASSIGN                                                   !0, $1
   42     3        NEW                                              $5      'T'
          4        DO_FCALL                                      0          
          5        ASSIGN_OBJ                                               !0, 't'
          6        OP_DATA                                                  $5
   44     7        INIT_METHOD_CALL                                         !0, 'createX'
          8        DO_FCALL                                      0          
          9      > RETURN                                                   1

Class T: [no user functions]
Class X:
Function __construct:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BeN7t
function name:  __construct
number of ops:  4
compiled vars:  !0 = $t
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    6     0  E >   RECV                                             !0      
    7     1        ASSIGN_OBJ                                               't'
          2        OP_DATA                                                  !0
    8     3      > RETURN                                                   null

End of function __construct

End of class X.

Class Builder:
Function createx:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BeN7t
function name:  createX
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   20     0  E >   INIT_METHOD_CALL                                         'validate'
          1        DO_FCALL                                      0          
   27     2        NEW                                              $1      'X'
          3        CHECK_FUNC_ARG                                           
          4        FETCH_OBJ_FUNC_ARG                               $2      't'
          5        SEND_FUNC_ARG                                            $2
          6        DO_FCALL                                      0          
          7        VERIFY_RETURN_TYPE                                       $1
          8      > RETURN                                                   $1
   28     9*       VERIFY_RETURN_TYPE                                       
         10*     > RETURN                                                   null

End of function createx

Function validate:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 3, Position 2 = 6
Branch analysis from position: 3
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/BeN7t
function name:  validate
number of ops:  7
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   34     0  E >   FETCH_OBJ_R                                      ~0      't'
          1        TYPE_CHECK                                    2          ~0
          2      > JMPZ                                                     ~1, ->6
   35     3    >   NEW                                              $2      'Exception'
          4        DO_FCALL                                      0          
          5      > THROW                                         0          $2
   37     6    > > RETURN                                                   null

End of function validate

End of class Builder.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
135.4 ms | 1000 KiB | 13 Q