Skip to content

More precise sprintf() format-arg based return type #3192

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

Merged
merged 20 commits into from
Jul 19, 2024

Conversation

staabm
Copy link
Contributor

@staabm staabm commented Jun 28, 2024

extracted from #3168

closes phpstan/phpstan#11248
closes phpstan/phpstan#10493

the previous format-type based logic was too optimistic regarding types and false-positively created too precise types in some cases.

@@ -107,13 +107,17 @@ function stringFunctions(string $s, $nonFalsey, $arrayOfNonFalsey, $nonEmptyArra
assertType('string', sprintf($nonFalsey));
assertType("'foo'", sprintf('foo'));
assertType("string", sprintf(...$arr));
assertType("non-falsy-string", sprintf('%s', ...$arr)); // should be 'string'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixes previous too specific types

@staabm
Copy link
Contributor Author

staabm commented Jun 28, 2024

I think the remaining error is a bug: phpstan/phpstan#11249

@staabm staabm marked this pull request as ready for review June 28, 2024 10:30
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

@staabm staabm force-pushed the sprintf4 branch 4 times, most recently from 056c194 to ce1c5c2 Compare July 5, 2024 09:44
@staabm
Copy link
Contributor Author

staabm commented Jul 19, 2024

sorry for all these commits to make it finally build on php 7.2 without errors.

@ondrejmirtes ondrejmirtes merged commit 9bd84cf into phpstan:1.11.x Jul 19, 2024
447 of 454 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

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.

sprintf with non-empty-string arguments Sprintf can return empty string in case of combining nulls via %s
3 participants