Skip to content

How to tell psalm that constants are configurable? #4024

@AgentOak

Description

@AgentOak

I'm using a php file with constants as configuration, like this (config.php):

<?php
/**
 * Enables debug mode, bla bla bla
 *
 * @var bool
 */
const DEBUG = false;

This file is required() from a bootstrap file before classes that make use of these constants are autoloaded.

In my psalm.xml I list <file name="config.php"> in <projectFiles>, but then psalm complains about UndefinedConstant violations in other files:

ERROR: UndefinedConstant - src/Templating/Template.php:29:29 - Const DEBUG is not defined (see https://psalm.dev/020)
        $options["debug"] = DEBUG;

I found #2644 where <stubs> are suggested. Adding config.php as a stub fixes these warnings, but now psalm also uses the predefined values of these constants for analysis and as a result finds violations like this:

ERROR: TypeDoesNotContainType - src/Templating/Template.php:30:29 - if (false) is impossible (see https://psalm.dev/056)
        $options["cache"] = DEBUG ? false : self::CACHE_DIR;

How to handle this case? I cannot define the constants without values because that is a syntax error. Thanks.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions