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

Concat should never remove "non-empty-"/"non-falsy-" from string #9411

Closed
kkmuffme opened this issue Feb 26, 2023 · 6 comments · Fixed by #9422
Closed

Concat should never remove "non-empty-"/"non-falsy-" from string #9411

kkmuffme opened this issue Feb 26, 2023 · 6 comments · Fixed by #9422
Labels
easy problems Issues that can be fixed without background knowledge of Psalm enhancement good first issue Help wanted

Comments

@kkmuffme
Copy link
Contributor

https://psalm.dev/r/0baa3234b5

When concatenating a non-empty/non-falsy string with something else that is unknown (constants), the type should not "fall back", since it's wrong. And instead keep the type it already has (e.g. non-falsy-string in my example)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/0baa3234b5
<?php

/**
 * @param non-falsy-string $arg
 * @return non-falsy-string
 */
function foo( $arg ) {
    $result = FOO . $arg;
    
    /** @psalm-trace $result */;
    
    return $result;
}
Psalm output (using commit a0ea8eb):

ERROR: UndefinedConstant - 8:15 - Const FOO is not defined

INFO: Trace - 10:32 - $result: string

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

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

@orklah
Copy link
Collaborator

orklah commented Feb 26, 2023

It's handled here:

It should be a pretty simple change

@orklah orklah added enhancement easy problems Issues that can be fixed without background knowledge of Psalm Help wanted good first issue labels Feb 26, 2023
@EgorBakulin
Copy link

It's handled here:

It should be a pretty simple change

The problem is more complex than it might seem.

$left_type = $statements_analyzer->node_data->getType($left);

This variable is null for this snippets https://psalm.dev/r/f264a4732f https://psalm.dev/r/9ef452ff58

@psalm-github-bot
Copy link

I found these snippets:

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

/**
 * @param non-empty-string $arg
 * @return non-empty-string
 */
function foo( $arg ) {
    /** @psalm-suppress UndefinedConstant */
    return FOO . $arg;
}
Psalm output (using commit 1a2909e):

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

INFO: MoreSpecificReturnType - 5:12 - The declared return type 'non-empty-string' for foo is more specific than the inferred return type 'string'
https://psalm.dev/r/9ef452ff58
<?php

/**
 * @param non-falsy-string $arg
 * @return non-falsy-string
 */
function foo( $arg ) {
    /** @psalm-suppress UndefinedConstant */
    return FOO . $arg;
}
Psalm output (using commit 1a2909e):

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

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

@orklah
Copy link
Collaborator

orklah commented Feb 27, 2023

yeah, I guess it makes sense given FOO was not defined. Then

will return false, so adding an else and setting $result_type accordingly with the known other operand should still be easy

@EgorBakulin
Copy link

#9422 I created pool request with fixes. But I newer worked with gihub pool requests. And I don't know how to link pool request to this issue.

orklah added a commit that referenced this issue Feb 27, 2023
…emove-non-empty-non-falsy-from-string

concat should never remove non empty non falsy from string #9411
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 good first issue Help wanted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants