Skip to content

Commit 2526f50

Browse files
phpstan-botondrejmirtes
authored andcommitted
Include argument index in dumpType message to preserve argument order
- When multiple arguments are passed to `dumpType()`, `dumpNativeType()`, or `dumpPhpDocType()`, messages now include `#N` (e.g. "Dumped type #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
1 parent 1bf9426 commit 2526f50

9 files changed

Lines changed: 90 additions & 12 deletions

File tree

src/Rules/Debug/DumpNativeTypeRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public function processNode(Node $node, Scope $scope): array
4949
return [];
5050
}
5151

52+
$multipleArgs = count($args) > 1;
5253
$errors = [];
53-
foreach ($args as $arg) {
54+
foreach ($args as $i => $arg) {
5455
$errors[] = RuleErrorBuilder::message(
5556
sprintf(
56-
'Dumped type: %s',
57+
$multipleArgs ? 'Dumped type #%d: %s' : 'Dumped type: %2$s',
58+
$i + 1,
5759
$scope->getNativeType($arg->value)->describe(VerbosityLevel::precise()),
5860
),
5961
)->nonIgnorable()->identifier('phpstan.dumpNativeType')->build();

src/Rules/Debug/DumpPhpDocTypeRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public function processNode(Node $node, Scope $scope): array
4949
return [];
5050
}
5151

52+
$multipleArgs = count($args) > 1;
5253
$errors = [];
53-
foreach ($args as $arg) {
54+
foreach ($args as $i => $arg) {
5455
$errors[] = RuleErrorBuilder::message(
5556
sprintf(
56-
'Dumped type: %s',
57+
$multipleArgs ? 'Dumped type #%d: %s' : 'Dumped type: %2$s',
58+
$i + 1,
5759
$this->printer->print($scope->getType($arg->value)->toPhpDocNode()),
5860
),
5961
)->nonIgnorable()->identifier('phpstan.dumpPhpDocType')->build();

src/Rules/Debug/DumpTypeRule.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ public function processNode(Node $node, Scope $scope): array
4949
return [];
5050
}
5151

52+
$multipleArgs = count($args) > 1;
5253
$errors = [];
53-
foreach ($args as $arg) {
54+
foreach ($args as $i => $arg) {
5455
$errors[] = RuleErrorBuilder::message(
5556
sprintf(
56-
'Dumped type: %s',
57+
$multipleArgs ? 'Dumped type #%d: %s' : 'Dumped type: %2$s',
58+
$i + 1,
5759
$scope->getType($arg->value)->describe(VerbosityLevel::precise()),
5860
),
5961
)->nonIgnorable()->identifier('phpstan.dumpType')->build();

tests/PHPStan/Rules/Debug/DumpNativeTypeRuleTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,28 @@ public function testRule(): void
2828
12,
2929
],
3030
[
31-
'Dumped type: non-empty-array',
31+
'Dumped type #1: non-empty-array',
3232
14,
3333
],
3434
[
35-
'Dumped type: array',
35+
'Dumped type #2: array',
3636
14,
3737
],
3838
]);
3939
}
4040

41+
public function testBug14508(): void
42+
{
43+
$this->analyse([__DIR__ . '/data/bug-14508-native.php'], [
44+
[
45+
'Dumped type #1: int',
46+
10,
47+
],
48+
[
49+
'Dumped type #2: bool',
50+
10,
51+
],
52+
]);
53+
}
54+
4155
}

tests/PHPStan/Rules/Debug/DumpPhpDocTypeRuleTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,28 @@ public function testRuleSymbols(): void
101101
36,
102102
],
103103
[
104-
'Dumped type: array{1: 1}',
104+
'Dumped type #1: array{1: 1}',
105105
41,
106106
],
107107
[
108-
'Dumped type: array{2: 2}',
108+
'Dumped type #2: array{2: 2}',
109109
41,
110110
],
111111
]);
112112
}
113113

114+
public function testBug14508(): void
115+
{
116+
$this->analyse([__DIR__ . '/data/bug-14508-phpdoc.php'], [
117+
[
118+
'Dumped type #1: int<0, 100>',
119+
10,
120+
],
121+
[
122+
'Dumped type #2: bool',
123+
10,
124+
],
125+
]);
126+
}
127+
114128
}

tests/PHPStan/Rules/Debug/DumpTypeRuleTest.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ public function testRuleWithMultipleVars(): void
5454
{
5555
$this->analyse([__DIR__ . '/data/dump-type-variadic.php'], [
5656
[
57-
'Dumped type: non-empty-array',
57+
'Dumped type #1: non-empty-array',
5858
10,
5959
],
6060
[
61-
'Dumped type: array',
61+
'Dumped type #2: array',
6262
10,
6363
],
6464
]);
@@ -116,4 +116,18 @@ public function testBug11179NoNamespace(): void
116116
]);
117117
}
118118

119+
public function testBug14508(): void
120+
{
121+
$this->analyse([__DIR__ . '/data/bug-14508.php'], [
122+
[
123+
'Dumped type #1: int<0, 100>',
124+
10,
125+
],
126+
[
127+
'Dumped type #2: bool',
128+
10,
129+
],
130+
]);
131+
}
132+
119133
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14508Native;
4+
5+
use function PHPStan\dumpNativeType;
6+
7+
$bool_set_before = (bool) random_int(0, 1);
8+
$int_set_after = random_int(0, 100);
9+
10+
dumpNativeType($int_set_after, $bool_set_before);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14508PhpDoc;
4+
5+
use function PHPStan\dumpPhpDocType;
6+
7+
$bool_set_before = (bool) random_int(0, 1);
8+
$int_set_after = random_int(0, 100);
9+
10+
dumpPhpDocType($int_set_after, $bool_set_before);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14508;
4+
5+
use function PHPStan\dumpType;
6+
7+
$bool_set_before = (bool) random_int(0, 1);
8+
$int_set_after = random_int(0, 100);
9+
10+
dumpType($int_set_after, $bool_set_before);

0 commit comments

Comments
 (0)