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

Still having problems with $_ENV #8662

Closed
matiux opened this issue Nov 5, 2022 · 3 comments · Fixed by #8666
Closed

Still having problems with $_ENV #8662

matiux opened this issue Nov 5, 2022 · 3 comments · Fixed by #8666
Labels

Comments

@matiux
Copy link

matiux commented Nov 5, 2022

I had already opened an issue about this: #3564 (comment)

But now, same code has a new error: https://psalm.dev/r/f48b8a52dc

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/f48b8a52dc
<?php

class EnvVarUtil
{
    public static function get(string $name, string $default = ''): string
    {
        return getenv($name) ?: (string) ($_ENV[$name] ?? $default);
    }
}
Psalm output (using commit d0be59e):

ERROR: PossiblyInvalidCast - 7:43 - non-empty-list<string> cannot be cast to string

@weirdan
Copy link
Collaborator

weirdan commented Nov 5, 2022

The type of $_ENV is constructed here:

if ($var_id === '$_SERVER' || $var_id === '$_ENV') {
$string_helper = Type::getString();
$string_helper->possibly_undefined = true;
$non_empty_string_helper = Type::getNonEmptyString();
$non_empty_string_helper->possibly_undefined = true;
$argv_helper = new Union([
new TNonEmptyList(Type::getString())
]);
$argv_helper->possibly_undefined = true;
$argc_helper = new Union([
new TIntRange(1, null)
]);
$argc_helper->possibly_undefined = true;
$request_time_helper = new Union([
new TIntRange(time(), null)
]);
$request_time_helper->possibly_undefined = true;
$request_time_float_helper = Type::getFloat();
$request_time_float_helper->possibly_undefined = true;
$bool_string_helper = new Union([new TBool(), new TString()]);
$bool_string_helper->possibly_undefined = true;
$detailed_type = new TKeyedArray(
[
// https://www.php.net/manual/en/reserved.variables.server.php
'PHP_SELF' => $non_empty_string_helper,
'argv' => $argv_helper,
'argc' => $argc_helper,

Even though most entries in the $_SERVER and $_ENV often have the same name and type, argc and argv usually are only present in the $_SERVER array.

@weirdan weirdan added the bug label Nov 5, 2022
weirdan added a commit to weirdan/psalm that referenced this issue Nov 5, 2022
@matiux
Copy link
Author

matiux commented Nov 7, 2022

Thanks @weirdan

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

Successfully merging a pull request may close this issue.

2 participants