Skip to content

Incorrect "Call to sprintf contains ... placeholders, ... value given." when format string is read from array which had its keys dynamically assigned #8774

Closed

Description

@proton-ab

Bug report

In certain unknown cases, when sprintf is called with format string retrieved from array of elements, and if said array had previously keys assigned dynamically (not necessarily the one being used in call to sprintf), PHPStan will fail to properly parse format string and instead will assume that every key contains format string with as many placeholders as key with format string containing highest amount of them.

Code snippet that reproduces the problem

https://phpstan.org/r/e9edd4d9-5109-4ddd-96c9-5404c0b03f18
https://phpstan.org/r/dffec326-aa07-47d9-8de7-c017df411589

Expected output

No error reported.

Activity

  1. ondrejmirtes commented on Jan 26, 2023

    @ondrejmirtes
    Member

    You can see what happens with the types here:

    Before the foreach $summaryTemplates['Reset'] is correct. But assignment into $summaryTemplates[$key] confuses PHPStan and it generalizes the type. It loses the information about specific array keys.

    And PrintfParametersRule takes the largest placeholder count and compares against that: https://github.com/phpstan/phpstan-src/blob/b736ddcecdb6506e17ebf227654755a4ac5b82e5/src/Rules/Functions/PrintfParametersRule.php#L81

  2. proton-ab commented on Feb 1, 2023

    @proton-ab
    Author

    Do you think this is something that will be fixed near-term or is there some proposed workaround? I have dozen of these errors and obviously placing ignore comments in 30+ places is sub-optimal, but so is ignoring the error globally.

  3. ondrejmirtes commented on Feb 1, 2023

    @ondrejmirtes
    Member

    The code can usually be rewritten to be more static analyser friendly, and with that it becomes more readable as well. I started thinking about rewriting your example, and I realized I don't actually know what it does with all the strings 😅

    But if I transformed it correctly, this is perfectly fine code for me: https://phpstan.org/r/5d809feb-e0f1-4ffc-becb-9f7dcc2e4950

  4. proton-ab commented on Feb 1, 2023

    @proton-ab
    Author

    Your rewrite is correct however it duplicates a lot of code and is susceptible to human error when adding new fields. The idea here is that form fields are created dynamically from DISABLE_KEYS_AND_LABELS.

    I don't believe this code is particularly hard for PHPStan nor I think it should generalize it in such a way. Assigning values dynamically is not something exotic and I'd certainly consider static analyzer expanding type of single element of array to set of types of all elements to be critical issue.

    Thanks for response regardless; I will ignore this error for now.

  5. added a commit that references this issue on Feb 16, 2026
  6. phpstan-bot commented on Mar 14, 2026

    @phpstan-bot
    Contributor

    Closed via merging phpstan/phpstan-src#5193

  7. github-actions commented on Apr 15, 2026

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

  8. locked as resolved and limited conversation to collaborators on Apr 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions