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
Comments
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 |
Also prevent psalm from analyzing the actual constant used for tests configuration by feeding it a stub instead, see vimeo/psalm#4024
We use define('DEBUG', /** @var bool $x */ $x = false); in a stub - works. |
I found these snippets: https://psalm.dev/r/77d4facff4<?php
define('DEBUG', /** @var bool $x */ $x = false);
/** @psalm-trace DEBUG */
if (DEBUG) {
echo 'x';
}
|
@philstutz this works for you in stubs? When I do that the EDIT: even in an autoloader.php it doesn't work? Always mixed |
@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 For our use case |
@philstutz I created a PR that will allow doing this in the stubs:
|
Do I understand correctly that this stub workaround does not work for class constants since |
Yes, it only works for |
I'm using a php file with constants as configuration, like this (
config.php
):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:I found #2644 where
<stubs>
are suggested. Addingconfig.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:How to handle this case? I cannot define the constants without values because that is a syntax error. Thanks.
The text was updated successfully, but these errors were encountered: