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

SprintfReturnTypeProvider not working well with array unpacking #9873

Closed
fancyweb opened this issue Jun 5, 2023 · 8 comments · Fixed by #9943 or #9975
Closed

SprintfReturnTypeProvider not working well with array unpacking #9873

fancyweb opened this issue Jun 5, 2023 · 8 comments · Fixed by #9943 or #9975

Comments

@fancyweb
Copy link

fancyweb commented Jun 5, 2023

Hello, SprintfReturnTypeProvider crashes when sprintf is used with array unpacking because it considers there's one arg only, eg:

$array = ['foo', 'bar', 'ccc'];
sprintf('%s %s %s', ...$array);
@psalm-github-bot
Copy link

Hey @fancyweb, can you reproduce the issue on https://psalm.dev ?

@fancyweb
Copy link
Author

fancyweb commented Jun 5, 2023

Some real code that shows the bug: https://github.com/symfony/symfony/pull/50565/files

@fancyweb
Copy link
Author

fancyweb commented Jun 5, 2023

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/260236cf5b
<?php

$a = ['foo', 'bar', 'ccc'];
sprintf('%s %s %s', ...$a);
Psalm encountered an internal error:

/vendor/vimeo/psalm/src/Psalm/Internal/Provider/ReturnTypeProvider/SprintfReturnTypeProvider.php: 4 arguments are required, 2 given

@kkmuffme
Copy link
Contributor

kkmuffme commented Jun 7, 2023

This is partially (= the fatal error) fixed in #9877, however in this case you should probably use vsprintf instead of sprintf anyway.
We should perhaps add an error for that (ArgumentTypeCoercion?) to use vsprintf in that case, but also handle it that it won't report TooFewArguments error.

kkmuffme added a commit to kkmuffme/psalm that referenced this issue Jun 21, 2023
kkmuffme added a commit to kkmuffme/psalm that referenced this issue Jun 24, 2023
@mrVrAlex
Copy link

mrVrAlex commented Jun 29, 2023

@orklah @kkmuffme PR not full resolved this issue, look this case: https://psalm.dev/r/308b6b4ae5
Should I open new bug issue? Or just re-open it? (In v5.12 it is produce no errors)

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/308b6b4ae5
<?php

$a = ['Some %s error', 1];
echo sprintf(...$a);
Psalm output (using commit 086b943):

ERROR: TooFewArguments - 4:6 - Too few arguments for sprintf, expecting at least 2 arguments

kkmuffme added a commit to kkmuffme/psalm that referenced this issue Jun 29, 2023
@orklah orklah reopened this Jun 29, 2023
@kkmuffme
Copy link
Contributor

Fixed in #9975

nicolas-grekas added a commit to symfony/symfony that referenced this issue Jul 19, 2023
…ancyweb)

This PR was merged into the 5.4 branch.

Discussion
----------

[Routing] Use vsprintf instead of sprintf + unpacking

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

A very small PR "reverting" #50565 and applying vimeo/psalm#9873 (comment) suggestion of using vsprintf instead.

Targeting 5.4 since it "reverts" the last one but we could also decide to target 6.4.

Commits
-------

2f6a355 [Routing] Use vsprintf instead of sprintf + unpacking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants