You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect "Call to sprintf contains ... placeholders, ... value given." when format string is read from array which had its keys dynamically assigned #8774
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.
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.
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.
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 😅
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.
Activity
ondrejmirtes commentedon Jan 26, 2023
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
proton-ab commentedon Feb 1, 2023
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.
ondrejmirtes commentedon Feb 1, 2023
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
proton-ab commentedon Feb 1, 2023
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.
Add regression test for #8774
Add regression tests for related issues fixed by constant array shape…
phpstan-bot commentedon Mar 14, 2026
Closed via merging phpstan/phpstan-src#5193
github-actions commentedon Apr 15, 2026
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.