Skip to content

Include argument index in dumpType message to preserve argument order#5503

Merged
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-cs424p4
Apr 21, 2026
Merged

Include argument index in dumpType message to preserve argument order#5503
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-cs424p4

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

When PHPStan\dumpType() is called with multiple arguments, the dumped types were displayed in alphabetical order by message rather than in argument order. This happened because AnalyserResult::getErrors() sorts errors on the same line alphabetically by message.

The fix adds an argument index (#1, #2, ...) to the message when multiple arguments are passed, ensuring the sort produces the correct argument order.

Changes

  • src/Rules/Debug/DumpTypeRule.php — include #N in message format when count($args) > 1
  • src/Rules/Debug/DumpNativeTypeRule.php — same fix for dumpNativeType()
  • src/Rules/Debug/DumpPhpDocTypeRule.php — same fix for dumpPhpDocType()
  • Updated existing multi-arg test expectations in all three test files to match the new "Dumped type #N: ..." format

Probed analogous cases:

  • dumpNativeType() — had the same bug, fixed
  • dumpPhpDocType() — had the same bug, fixed
  • Single-argument calls to all three functions — not affected, message format unchanged

Root cause

AnalyserResult::getErrors() and RuleTestCase::analyse() both sort errors by [file, line, message]. When multiple dumpType errors appear on the same line, the alphabetical sort on the message string reorders them, losing the original argument order. For example, dumpType($int, $bool) would show "Dumped type: bool" before "Dumped type: int<0, 100>" because 'b' < 'i'.

By including #1, #2, etc. in the message, the sort key becomes "Dumped type #1: ..." vs "Dumped type #2: ...", which preserves argument order regardless of the type descriptions.

Test

  • Added tests/PHPStan/Rules/Debug/data/bug-14508.php — regression test for the reported issue with dumpType()
  • Added tests/PHPStan/Rules/Debug/data/bug-14508-native.php — analogous test for dumpNativeType()
  • Added tests/PHPStan/Rules/Debug/data/bug-14508-phpdoc.php — analogous test for dumpPhpDocType()
  • All three tests verify that when $int and $bool are passed to the dump function, the int type is reported as #1 and bool as #2

Fixes phpstan/phpstan#14508

- When multiple arguments are passed to `dumpType()`, `dumpNativeType()`, or
  `dumpPhpDocType()`, messages now include `#N` (e.g. "Dumped type phpstan#1: int")
  to maintain stable argument ordering through the alphabetical error sort
- Single-argument calls retain the original "Dumped type: ..." format
- Fixed all three dump rules: DumpTypeRule, DumpNativeTypeRule, DumpPhpDocTypeRule
- Updated existing multi-arg test expectations to match new format
@ondrejmirtes ondrejmirtes merged commit 2526f50 into phpstan:2.1.x Apr 21, 2026
654 checks passed
@ondrejmirtes ondrejmirtes deleted the create-pull-request/patch-cs424p4 branch April 21, 2026 15:06
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