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

Psalm 5.7 started complaining about mb_strlen checks on array elements #9341

Closed
kamil-tekiela opened this issue Feb 19, 2023 · 6 comments · Fixed by #9358
Closed

Psalm 5.7 started complaining about mb_strlen checks on array elements #9341

kamil-tekiela opened this issue Feb 19, 2023 · 6 comments · Fixed by #9358
Labels

Comments

@kamil-tekiela
Copy link
Contributor

It manifests as multiple different issues. Here's one example:
https://psalm.dev/r/897a70d45b

Asserting string length doesn't mean checking if the string is non-empty.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/897a70d45b
<?php
$GLOBALS['sql_query'] = rand(0,1) ? 'asd' : null;
if(!empty($GLOBALS['sql_query']) && mb_strlen($GLOBALS['sql_query']) > 2)
{
    echo 'true';
}
Psalm output (using commit d934875):

ERROR: RedundantCondition - 3:4 - $GLOBALS['sql_query'] is non-empty has already been asserted

@weirdan weirdan changed the title Psalm 5.7 started complaining about mb_strlen checks Psalm 5.7 started complaining about mb_strlen checks on array elements Feb 19, 2023
@weirdan
Copy link
Collaborator

weirdan commented Feb 19, 2023

It doesn't seem to happen on a plain variable though: https://psalm.dev/r/6178eba089

Asserting string length doesn't mean checking if the string is non-empty.

It's the other way around. Asserting non-emptyness doesn't mean the string can't be less than three chars (as in your example).

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6178eba089
<?php
$query = rand(0,1) ? 'asd' : null;

if(!empty($query) && mb_strlen($query) > 2)
{
    echo 'true';
}
Psalm output (using commit d934875):

No issues!

@weirdan weirdan added the bug label Feb 19, 2023
@weirdan
Copy link
Collaborator

weirdan commented Feb 19, 2023

cc: @LeoVie

@weirdan
Copy link
Collaborator

weirdan commented Feb 19, 2023

It also doesn't happen with nested ifs: https://psalm.dev/r/6ad10cdab0

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/6ad10cdab0
<?php
$GLOBALS['sql_query'] = rand(0,1) ? 'asd' : null;
if (!empty($GLOBALS['sql_query'])) {
    if (mb_strlen($GLOBALS['sql_query']) > 2) {
       echo 'true';
    }
}
Psalm output (using commit d934875):

No issues!

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