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

htmlspecialchars can return non-empty-string #5785

Merged
merged 1 commit into from
May 17, 2021

Conversation

still-dreaming-1
Copy link
Contributor

https://psalm.dev/r/397951a5d7

The example code shows the issue I am trying to fix. If the code has already done something to guarantee a value is a non-empty-string, there are several built in functions you could pass it to that should then also return a non-empty-string. This will prevent redundant checks and added runtime in projects making use of non-empty-string. This pull request is only changing this one function for now, but if it is accepted I will probably change additional similar functions.

I don't really understand @psalm-flow, hopefully this added @psalm-return does not interfere? Is there a better approach where @psalm-flow could always imply starting from a non-empty-string would return one (again speaking from a place of not understanding @psalm-flow).

I looked for a good place to add tests for this change, but I'm lost on that front. Can someone recommend something, or is this a change that would be overkill to explicitly test?

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/397951a5d7
<?php

/**
 * @param non-empty-string $value
 * @return non-empty-string
 */
function escapeForHtml(string $value): string
{
    return \htmlspecialchars($value);
}
Psalm output (using commit ff0c1ce):

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

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

@muglug muglug merged commit a62ee22 into vimeo:master May 17, 2021
@muglug
Copy link
Collaborator

muglug commented May 17, 2021

Thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants