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
A string after strlen > 0 check should be a non-empty-string #7387
Comments
I found these snippets: https://psalm.dev/r/ca291dd273<?php
/**
* @return non-empty-string
*/
function nonEmptyString(string $str): string {
return strlen($str) > 0 ? $str : 'string';
}
/**
* @return non-empty-string
*/
function nonEmptyMbString(string $str): string {
return mb_strlen($str) > 0 ? $str : 'string';
}
|
Good idea! This is handled in AssertionFinder. The role of this class is to find any hint it can use to refine a variable type. For example AssertionFinder has 4 main methods: What you want to do is actually closer to what we already have with count (for example in the count() case is handled here for getGreaterAssertions:
The method we call must detect when count() > 5 is used, and then extract an assertion from that In your case, you'll want to detect any strlen used on a string and then deduct if you can infer something from it.
So this means you don't have to change anything in getLesserAssertions. Don't hesitate to ask questions or to ping me for more help or explanations! |
#7387 Add asserting non-empty-string by strlen
Should this be closed now? |
This reverts commit 0ef7ec1.
This reverts commit 0ef7ec1.
The PR implementing this has been reverted, so I'm reopening the issue. |
https://psalm.dev/r/ca291dd273
The text was updated successfully, but these errors were encountered: