psalm-php

How to make Psalm recognize variables from an included file


I have a config file that's included in a function, like this:

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

The issue is to make Psalm recognize the $config variable from the config file. Possible? Preferable using array shape notation.


Solution

  • Solved by adding a /** @var ... annotation above the include line:

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