【问题标题】:How to make Psalm recognize variables from an included file如何让 Psalm 识别包含文件中的变量
【发布时间】:2021-11-24 22:06:54
【问题描述】:

我有一个包含在函数中的配置文件,如下所示:

function getConnection() {
  include 'config.php';
  return new Connection($config['host']);
}

问题是让 Psalm 从配置文件中识别出$config 变量。可能的?最好使用数组形状表示法。

【问题讨论】:

    标签: psalm-php


    【解决方案1】:

    通过在包含行上方添加/** @var ... 注释解决:

    function getConnection() {
      /** @var array{host: string} $config */
      include 'config.php';
      return new Connection($config['host']);
    }
    
    

    【讨论】:

      猜你喜欢
      • 2016-10-19
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2019-03-10
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多