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

str_pad, str_repeat and non-empty-strings #6049

Closed
ciaranmcnulty opened this issue Jul 5, 2021 · 7 comments · Fixed by #6094
Closed

str_pad, str_repeat and non-empty-strings #6049

ciaranmcnulty opened this issue Jul 5, 2021 · 7 comments · Fixed by #6094
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement internal stubs/callmap

Comments

@ciaranmcnulty
Copy link
Contributor

Psalm does not know that str_pad is guaranteed to return a non-empty-string for certain inputs

https://psalm.dev/r/c7f5c10980

If the input is a non-empty-string and the padding is >0, this should always be the case

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @param non-empty-string $foo
 */
function foo($foo) : void
{
	echo $foo;
}

foo(str_pad('a', 100));
Psalm output (using commit bee4e83):

ERROR: ArgumentTypeCoercion - 11:5 - Argument 1 of foo expects non-empty-string, parent type string provided

@weirdan
Copy link
Collaborator

weirdan commented Jul 5, 2021

If the input is a non-empty-string and the padding is >0, this should always be the case

Actually it's or, not and

@weirdan
Copy link
Collaborator

weirdan commented Jul 5, 2021

@ciaranmcnulty does the following signature match your expectations: https://psalm.dev/r/ecdcca18ba ?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/ecdcca18ba
<?php
/**
 * @return ($_string is non-empty-string ? non-empty-string : ($_length is positive-int ? non-empty-string: string))
 */
function f(string $_string, int $_length): string {
  return $_string; // to suppress the error
}

$_a = f("a", 0); /** @psalm-trace $_a */; // expected: non-empty-string 
$_b = f("", 1); /** @psalm-trace $_b */; // expected: non-empty-string
$_c = f("", -1); /** @psalm-trace $_c */; // expected: string
Psalm output (using commit bee4e83):

INFO: Trace - 9:41 - $_a: non-empty-string

INFO: Trace - 10:40 - $_b: non-empty-string

INFO: Trace - 11:41 - $_c: string

@ciaranmcnulty
Copy link
Contributor Author

It looks like there is some funny stuff when the length is less than the original string length too

@weirdan
Copy link
Collaborator

weirdan commented Jul 5, 2021

Got an example?

@ciaranmcnulty
Copy link
Contributor Author

Actually that looks fine. There's a third param but it doesn't affect the logic as it has to be a non-empty-string.

str_repeat seems to have a very similar issue / solution

@weirdan weirdan added the easy problems Issues that can be fixed without background knowledge of Psalm label Jul 5, 2021
@ciaranmcnulty ciaranmcnulty changed the title str_pad and non-empty-string str_pad, str_repeat and non-empty-strings Jul 6, 2021
weirdan added a commit to weirdan/psalm that referenced this issue Jul 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement internal stubs/callmap
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants