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

Problem with $_ENV #3564

Closed
matiux opened this issue Jun 11, 2020 · 8 comments
Closed

Problem with $_ENV #3564

matiux opened this issue Jun 11, 2020 · 8 comments

Comments

@matiux
Copy link

matiux commented Jun 11, 2020

I have this function to take environment variables in various ways

https://psalm.dev/r/084f2df25d

But I'm struggling to figure out how to solve the TypeDoesNotContainType error

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/084f2df25d
<?php

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

ERROR: TypeDoesNotContainType - 7:33 - string is always defined and non-null

@weirdan
Copy link
Collaborator

weirdan commented Jun 11, 2020

Shouldn't the last ternary be wrapped in parens?
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 07c7c30):

No issues!

@matiux
Copy link
Author

matiux commented Jun 11, 2020

Thanks, I hadn't considered this syntax and actually don't understand why in this way works.

@muglug
Copy link
Collaborator

muglug commented Jun 11, 2020

It's about operator precedence – when in doubt, parenthesize

@muglug muglug closed this as completed Jun 11, 2020
@matiux
Copy link
Author

matiux commented Nov 4, 2022

@muglug some updates lead me to reopen this issue 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 4, 2022

@matiux please open a new issue.

It appears someone thought $_ENV usually contains argv element, like $_SERVER does:

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,

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

No branches or pull requests

3 participants