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

Infer types based on psalm-flow #7773

Closed
staabm opened this issue Mar 10, 2022 · 3 comments
Closed

Infer types based on psalm-flow #7773

staabm opened this issue Mar 10, 2022 · 3 comments

Comments

@staabm
Copy link
Contributor

staabm commented Mar 10, 2022

When a function defines @psalm-flow And the return-type is known, couldn‘t this information be utilized to specifiy/narrow the type of the parameter, which flows thru the function.

see https://psalm.dev/r/78576f9081 which shows the idea applied to strings.
It would work the same way for non-empty-array etc. - I guess.

This idea was born, as I was filling a similar but more concrete use-case for phpstan:
phpstan/phpstan#6792

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/78576f9081
<?php // --taint-analysis

/**
 * @param string $value
 * @psalm-flow ($value) -> return
 *
 */
function doThis(string $value, bool $isSecure = false): string {
   return $value;
}

if (doThis($s) != '') {
    echo $s; // should be considered non-empty-string
}
if (doThis($s) !== '') {
    echo $s; // should be considered non-empty-string
}
if (doThis($s)) {
    echo $s; // should be considered non-empty-string
}
Psalm output (using commit f0b2142):

No issues!

@orklah
Copy link
Collaborator

orklah commented Mar 10, 2022

@psalm-flow is not what you want to use in this case.

The flow annotation only tells that any taint coming in is going out, but you have no idea what the function does.

It could completely transform the string (like truncate it or concatenate it). You can't deduce anything about the format of the output based on the input nor the other way around

@orklah orklah closed this as completed Mar 10, 2022
@AndrolGenhald
Copy link
Collaborator

AndrolGenhald commented Mar 10, 2022

@psalm-flow isn't documented, but from what I gather it's really there to say which arguments apply a taint to the return value, it doesn't actually say the return value will contain those arguments. For example, I believe this is entirely valid, which makes this impossible.

Edit: And orklah beat me to it 😛

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