phpstan 可以检测到 "Typed property ... must not be accessed before initialization" 错误吗?

Can phpstan detect "Typed property ... must not be accessed before initialization" Errors?

我有一个PHP程序,

<?php
class Zap {
}

class Zip {
    public Zap $zap;
}
$object = new Zip;

var_dump(
    $object->zap
);

此程序因初始化的不可空 属性.

而产生错误

Fatal error: Uncaught Error: Typed property Zip::$zap must not be accessed before initialization in

phpstan 可以检测到这些类型的错误吗?我已经在最高级别扫描了这个程序,phpstan 看起来很开心

% ./vendor/bin/phpstan analyse --level=8 /tmp/test.php
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%


                                                                                                                        
 [OK] No errors                                                                                                         

如果 phpstan 无法检测到这些情况,是否有另一个 PHP 静态分析器可以?

看起来能够扫描未初始化的 属性 值 was added in July of 2020

但是,默认情况下禁用此功能。您需要使用设置 checkUninitializedProperties

的配置文件
% cat phpstan.neon 
parameters:
    checkUninitializedProperties: true

and then tell `phpstan` to use this configuration file.

 % ./vendor/bin/phpstan analyse --level=8 --configuration=./phpstan.neon /tmp/test.php
 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%

 ------ ------------------------------------------------------------------------------------------------------ 
  Line   /tmp/test.php                                                                                         
 ------ ------------------------------------------------------------------------------------------------------ 
  6      Class Zip has an uninitialized property $zap. Give it default value or assign it in the constructor.  
 ------ ------------------------------------------------------------------------------------------------------ 

                                                                                                                
 [ERROR] Found 1 error                                                                                                  

另外,冒着把显而易见的部分大声说出来的风险,这项检查假定了一种特定的编程风格。例如,下面的有效 PHP 程序

<?php
class Zap {
}

class Zip {
    public Zap $zap;
}
$object = new Zip;
$object->zap = new Zap;
var_dump(
    $object->zap
);

checkUninitializedProperties 检查仍然失败。