Skip to content

Infer non-empty and lowercase strings from sprintf() with unpacked arguments - #7308

Open
zonuexe wants to merge 1 commit into
phpstan:2.3.xfrom
zonuexe:sprintf-unpacked-args
Open

zonuexe wants to merge 1 commit into
phpstan:2.3.xfrom
zonuexe:sprintf-unpacked-args

Conversation

@zonuexe

@zonuexe zonuexe commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

sprintf() with unpacked arguments loses non-empty-string and lowercase-string, although vsprintf() keeps them for the same list. This PR lets unpacked arguments contribute to these two types, as the values of the vsprintf() array already do.

https://phpstan.org/r/bb6ff2df-c0aa-445f-b368-0fda6c8a6e4f

Impact

With $format being non-empty-string and $args being non-empty-list<non-empty-string> (or the lowercase-string equivalents):

Expression Before After
sprintf($format, ...$args) string non-empty-string
sprintf($lowerFormat, ...$lowerArgs) string lowercase-string
vsprintf($format, $args) non-empty-string unchanged

The lowercase check also applies to constant formats. With $lowerArgs being list<lowercase-string>, sprintf('%s', ...$lowerArgs) changes from string to lowercase-string, and sprintf('a%s', ...$lowerArgs) and sprintf('%d', ...$lowerArgs) gain lowercase-string on top of non-falsy-string and numeric-string.

Cause

SprintfFunctionDynamicReturnTypeExtension::allValuesSatisfies() asks whether every argument converts to a non-empty (or lowercase) string. For an unpacked argument it asked this about the array being unpacked rather than about its values, and an array never satisfies either condition.

Fix

For an unpacked argument the check now uses the iterable value type, which is what the vsprintf() branch of the same method already does.

An unpacked list that may be empty is treated like the vsprintf() case: only its value type is considered. With no values, sprintf() either prints the literal text of the format or throws ArgumentCountError.

The check does not inspect the placeholders of the format. A format such as '%.0s' can still produce an empty string, and '%c' can produce an uppercase letter from a lowercase format and integer arguments. Positional sprintf() and vsprintf() already infer the narrower type in these cases when the arguments are not constants, and unpacked sprintf() now behaves the same way. This includes the resulting false identical.alwaysFalse errors, for example when comparing sprintf('%c', ...$ints) with 'A'.

Not covered

Formats that are a single placeholder still do not take the type of an unpacked value. sprintf('%s', ...$nonFalsyArgs) gives non-empty-string, not non-falsy-string, and with an argnum placeholder, sprintf('%2$s', ...$args) stays string.

Testing

Added tests/PHPStan/Analyser/nsrt/sprintf-unpacked-args.php, covering lists, array shapes, iterable and Traversable values, integers, mixes of positional and unpacked arguments, and constant formats. It fails without the source change and passes with it.

…guments

allValuesSatisfies() checked the type of an unpacked argument itself instead of its iterable value type. The array type never converts to a non-empty or lowercase string, so sprintf($format, ...$args) lost the accessory types that vsprintf($format, $args) keeps.
@staabm

staabm commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

hey. please see the AI findings I got for your PR:

  1. Unpacked integers can produce uppercase output — src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php:194–196

    The lowercase check accepts an unpacked integer because its string conversion is lowercase-compatible. But %c formats the integer as a character, so sprintf('%c', ...[65]) returns 'A'. The inferred lowercase-string is therefore unsound and may trigger false identical.alwaysFalse diagnostics. The new tests assert lowercase inference for unpacked integers but do not cover %c.

  2. Precision can produce an empty string from non-empty arguments — src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php:194–196

    The non-empty check considers argument values but not format precision. For example, sprintf('%.0s', ...['x']) returns '', despite the argument being non-empty. The new tests don’t cover precision-limited formats, so this unsound non-empty-string inference is not caught.

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.

2 participants