-
Notifications
You must be signed in to change notification settings - Fork 652
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
Strlen as guard for non-empty-string #10072
Comments
I found these snippets: https://psalm.dev/r/bba89100c3<?php
/** @return non-empty-string */
function getHardcodedString1(): string {
return 'foo';
}
/** @psalm-check-type $string1 = non-empty-string */
$string1 = getHardcodedString1();
/** @return non-empty-string */
function getHardcodedString2(): string {
/** @psalm-check-type $string = non-empty-string */
$string = 'foo';
return $string;
}
/** @psalm-check-type $string2 = non-empty-string */
$string2 = getHardcodedString2();
/** @return non-empty-string */
function getHardcodedString3(): string {
/** @psalm-check-type STRING = non-empty-string */
define('STRING', 'foo');
return STRING;
}
/** @psalm-check-type $string3 = non-empty-string */
$string3 = getHardcodedString3();
final class Example
{
private const FOO = 'foo';
/** @var non-empty-string */
private string $bar = 'bar';
/** @return non-empty-string */
public function getFoo(): string
{
return self::FOO;
}
/** @return non-empty-string */
public function getBar(): string
{
return $this->bar;
}
}
/** @psalm-check-type $string4 = non-empty-string */
$string4 = (new Example())->getFoo();
/** @psalm-check-type $string5 = non-empty-string */
$string5 = (new Example())->getBar();
$someUserInput = $_GET['foo'] ?? '';
if (is_string($someUserInput) && strlen($someUserInput) > 0) {
/** @psalm-check-type $string6 = non-empty-string */
$string6 = $someUserInput;
}
|
Thanks for creating the issue! If you're interested for looking into it, we basically did the same thing for count on arrays here:
|
Duplicate of #7387 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Followup ticket for #10056 :
A condition with a
Strlen
check should be regarded as valid guard fornon-empty-string
warning.See: https://psalm.dev/r/bba89100c3
The text was updated successfully, but these errors were encountered: