Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to tell psalm that constants are configurable? #4024

Closed
AgentOak opened this issue Aug 18, 2020 · 8 comments · Fixed by #9992
Closed

How to tell psalm that constants are configurable? #4024

AgentOak opened this issue Aug 18, 2020 · 8 comments · Fixed by #9992

Comments

@AgentOak
Copy link

AgentOak commented Aug 18, 2020

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.

@ygottschalk
Copy link
Contributor

As a workaround: Try including it as an autoloader

<psalm [...] autoloader="config.php">
    <projectFiles>
        [...]
    </projectFiles>
</psalm>

(maybe you'll additionally need to include the file <file name="config.php"> in <projectFiles>)
Hope this helps

sad-spirit added a commit to sad-spirit/pg-wrapper that referenced this issue Nov 18, 2021
Also prevent psalm from analyzing the actual constant used for tests configuration by feeding it a stub instead, see vimeo/psalm#4024
@philstutz
Copy link

We use

define('DEBUG', /** @var bool $x */ $x = false);

in a stub - works.

https://psalm.dev/r/77d4facff4

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/77d4facff4
<?php

define('DEBUG', /** @var bool $x */ $x = false);

/** @psalm-trace DEBUG */

if (DEBUG) {
    echo 'x';
}
Psalm output (using commit 47a2545):

INFO: Trace - 7:1 - DEBUG: bool

@kkmuffme
Copy link
Contributor

kkmuffme commented Jul 3, 2023

@philstutz this works for you in stubs? When I do that the DEBUG constant is treated as mixed, not bool.
Only works in actual code (or autoloader) but won't work in stubs.

EDIT: even in an autoloader.php it doesn't work? Always mixed

@philstutz
Copy link

@kkmuffme You're right, can't get that working with 4.17/4.18 (was the current version then) nor with latest. Only same file as in the snippet or direct requires work.

For our use case mixed seems to have been enough to get rid of the RedundantCondition and other findings.

@kkmuffme
Copy link
Contributor

kkmuffme commented Jul 3, 2023

@philstutz I created a PR that will allow doing this in the stubs:

define('DEBUG', /** @var bool */ false);

@xPaw
Copy link

xPaw commented Nov 23, 2023

Do I understand correctly that this stub workaround does not work for class constants since define('Thing::DEBUG', ...) wouldn't work?

@kkmuffme
Copy link
Contributor

Yes, it only works for define as per my PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants