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

non empty string constants should be considered as a non empty string #6266

Closed
VincentLanglet opened this issue Aug 8, 2021 · 5 comments · Fixed by #6273
Closed

non empty string constants should be considered as a non empty string #6266

VincentLanglet opened this issue Aug 8, 2021 · 5 comments · Fixed by #6273

Comments

@VincentLanglet
Copy link
Contributor

With the option

ignoreInternalFunctionFalseReturn="false"

In

/** @var non-empty-string $foo */
$foo = PHP_EOL;
$bar = explode($foo, $diff);

$bar is considered as always an array.

In

$bar = explode(PHP_EOL, $diff);

$bar is considered as array|false.

So I would say that explode is correctly supporting non-empty-string, but some constant are not considered as non-empty-string. I tried

  • PHP_EOL
  • DIRECTORY_SEPARATOR
  • a custom class constant public const FOO = 'foo';

Should these be non-empty-string ?
https://psalm.dev/r/377464f1cd

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/377464f1cd
<?php

function foo(): void {
    /** @psalm-trace $a */
    $a = PHP_EOL;
    
    return;
}
Psalm output (using commit 3fb9e1c):

INFO: Trace - 5:5 - $a: string

INFO: UnusedVariable - 5:5 - $a is never referenced or the value is not used

@weirdan
Copy link
Collaborator

weirdan commented Aug 9, 2021

a custom class constant public const FOO = 'foo';

This one should be inferred as literal "foo".

@weirdan
Copy link
Collaborator

weirdan commented Aug 9, 2021

The other two, I believe, would safe to assume non-empty.

@orklah
Copy link
Collaborator

orklah commented Aug 9, 2021

public const FOO = 'foo';

Can be inherited and change value in the child

@VincentLanglet
Copy link
Contributor Author

public const FOO = 'foo';

Can be inherited and change value in the child

If I use static::FOO, or if it's final, it should be ok no ?

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.

3 participants