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

Strlen as guard for non-empty-string #10072

Closed
ivoba opened this issue Aug 1, 2023 · 3 comments
Closed

Strlen as guard for non-empty-string #10072

ivoba opened this issue Aug 1, 2023 · 3 comments

Comments

@ivoba
Copy link

ivoba commented Aug 1, 2023

Followup ticket for #10056 :

A condition with a Strlen check should be regarded as valid guard for non-empty-string warning.

See: https://psalm.dev/r/bba89100c3

@psalm-github-bot
Copy link

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;
}
Psalm output (using commit 73ebe22):

ERROR: CheckType - 62:5 - Checked variable $string6 = non-empty-string does not match $string6 = string

@orklah
Copy link
Collaborator

orklah commented Aug 1, 2023

Thanks for creating the issue!

If you're interested for looking into it, we basically did the same thing for count on arrays here:

} elseif (self::hasNonEmptyCountCheck($expr)) {

@kkmuffme
Copy link
Contributor

kkmuffme commented Aug 4, 2023

Duplicate of #7387

@orklah orklah closed this as completed Aug 5, 2023
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