Skip to content

Conversation

@VincentLanglet
Copy link
Contributor

@VincentLanglet VincentLanglet commented Oct 24, 2025

@VincentLanglet VincentLanglet marked this pull request as draft October 24, 2025 21:54
@VincentLanglet VincentLanglet marked this pull request as ready for review October 24, 2025 22:13
@phpstan-bot
Copy link
Collaborator

This pull request has been marked as ready for review.

sprintf('%s %s', 'foo'); // one parameter missing
sprintf('foo', 'foo'); // one parameter over
sprintf('foo %s', 'foo', 'bar'); // one parameter over
sprintf('%2$s %1$s %% %1$s %%%', 'one'); // one parameter missing
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Adding a second parameter does not solve the error since %%% was invalid placeholder.

sprintf('%2$s %1$s %% %1$s %%', 'one'); // one parameter missing
sprintf('%2$s %%'); // two parameters required
sprintf('%2$s %1$s %1$s %s %s %s %s'); // four parameters required
sprintf('%2$s %1$s %% %s %s %s %s %%% %%%%', 'one', 'two', 'three', 'four'); // ok
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was producing an error https://3v4l.org/2dPI6

Copy link
Contributor

@staabm staabm Oct 25, 2025

Choose a reason for hiding this comment

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

we had such test problems in the past. I wonder whether we can somehow detect such bogus tests
(in the general case, not just in (s)printf)

if ($count === null) {
return [
RuleErrorBuilder::message(sprintf(
'Call to %s contains an invalid placeholder.',
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to know what the invalid placeholder is and its position. Otherwise it'd be hard to debug with long format strings as %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about this and I was wondering if we should iterate and add more details with acceptsReasonsTip later because:

  • This would require some weird signature for getPrintfPlaceholdersCount (Returning the count + the invalid placeholders/positions)

  • This would require to handle error message for

    • Union string %c%|%c%c% => What would be the message ? 3rd position ? 5th position ?
    • String with multiple wrong placeholder like %%% %%% => Should we report all the invalid ones ?

=> maybe you already have an idea about the format of the message @ondrejmirtes ?

@ondrejmirtes
Copy link
Member

Also maybe this belongs to bleeding edge only?

@VincentLanglet
Copy link
Contributor Author

Also maybe this belongs to bleeding edge only?

I would have consider this kinda as a "bugfix".
Technically the sprintf('%') call should have been reported since there is was missing parameter https://3v4l.org/lAIAAs

I can put this behind bleeding edge, but that would mean adding an extra parameters to getPrintfPlaceholdersCount methods with something like getPrintfPlaceholdersCount($format, $reportInvalidPlaceholder). It's your call !

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.

Missing sprintf error Warning: printf(): Too few arguments

4 participants