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 false positive detection #10897

Open
AlexMinaev19 opened this issue Apr 11, 2024 · 4 comments
Open

non-empty-string false positive detection #10897

AlexMinaev19 opened this issue Apr 11, 2024 · 4 comments

Comments

@AlexMinaev19
Copy link

I have a false positive detection for the non-empty-string type and strange behavior with the empty function.

https://psalm.dev/r/cd3d4258fb

Copy link

I found these snippets:

https://psalm.dev/r/cd3d4258fb
<?php

/**
 * @return non-empty-string
 */
function foo(string $i): string {
    $i = trim($i);
    $l = strlen($i);
    
	if (0 === $l) {
        throw new \InvalidArgumentException('The alphabet has to be a non-empty string.');
    }
    
    return $i;
}

/**
 * @return non-empty-string
 */
function bar(string $i): string {  
    $i = trim($i);
    
	if (0 === strlen($i)) {
        throw new \InvalidArgumentException('The alphabet has to be a non-empty string.');
    }
    
    return $i;
}

/**
 * @return non-empty-string
 */
function baz(string $i): string {  
    $i = trim($i);
    
    // strange, because '0' will be empty for `empty` function
	if (empty($i)) {
        throw new \InvalidArgumentException('The alphabet has to be a non-empty string.');
    }
    
    return $i;
}
Psalm output (using commit ef3b018):

INFO: LessSpecificReturnStatement - 14:12 - The type 'string' is more general than the declared return type 'non-empty-string' for foo

INFO: MoreSpecificReturnType - 4:12 - The declared return type 'non-empty-string' for foo is more specific than the inferred return type 'string'

INFO: LessSpecificReturnStatement - 27:12 - The type 'string' is more general than the declared return type 'non-empty-string' for bar

INFO: MoreSpecificReturnType - 18:12 - The declared return type 'non-empty-string' for bar is more specific than the inferred return type 'string'

@kkmuffme
Copy link
Contributor

kkmuffme commented May 3, 2024

The strlen thing is a duplicate of #7387

strange behavior with the empty function.

Please elaborate, I don't see any strange behavior there.

@AlexMinaev19
Copy link
Author

AlexMinaev19 commented May 6, 2024

When I wrote this, it was a bug with an empty function. You can see it in the report of the initial snippet (function baz). Currently, it works fine:

https://psalm.dev/r/f6cae1f221

Copy link

I found these snippets:

https://psalm.dev/r/f6cae1f221
<?php

/**
 * @return non-empty-string
 */
function baz(string $s): string {
    $s = trim($s);

    if (empty($s)) {
        throw new \InvalidArgumentException('String is empty');
    }
    
    return $s;
}

baz('');
Psalm output (using commit 16b24bd):

No issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants