Skip to content

Commit 47923ed

Browse files
Allow to dump more than one thing (#4941)
1 parent 5557c78 commit 47923ed

12 files changed

+120
-21
lines changed

src/Rules/Debug/DumpNativeTypeRule.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function processNode(Node $node, Scope $scope): array
3535
return [];
3636
}
3737

38-
if (count($node->getArgs()) === 0) {
38+
$args = $node->getArgs();
39+
if (count($args) === 0) {
3940
return [];
4041
}
4142

@@ -48,14 +49,17 @@ public function processNode(Node $node, Scope $scope): array
4849
return [];
4950
}
5051

51-
return [
52-
RuleErrorBuilder::message(
52+
$errors = [];
53+
foreach ($args as $arg) {
54+
$errors[] = RuleErrorBuilder::message(
5355
sprintf(
5456
'Dumped type: %s',
55-
$scope->getNativeType($node->getArgs()[0]->value)->describe(VerbosityLevel::precise()),
57+
$scope->getNativeType($arg->value)->describe(VerbosityLevel::precise()),
5658
),
57-
)->nonIgnorable()->identifier('phpstan.dumpNativeType')->build(),
58-
];
59+
)->nonIgnorable()->identifier('phpstan.dumpNativeType')->build();
60+
}
61+
62+
return $errors;
5963
}
6064

6165
}

src/Rules/Debug/DumpPhpDocTypeRule.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function processNode(Node $node, Scope $scope): array
3535
return [];
3636
}
3737

38-
if (count($node->getArgs()) === 0) {
38+
$args = $node->getArgs();
39+
if (count($args) === 0) {
3940
return [];
4041
}
4142

@@ -48,14 +49,17 @@ public function processNode(Node $node, Scope $scope): array
4849
return [];
4950
}
5051

51-
return [
52-
RuleErrorBuilder::message(
52+
$errors = [];
53+
foreach ($args as $arg) {
54+
$errors[] = RuleErrorBuilder::message(
5355
sprintf(
5456
'Dumped type: %s',
55-
$this->printer->print($scope->getType($node->getArgs()[0]->value)->toPhpDocNode()),
57+
$this->printer->print($scope->getType($arg->value)->toPhpDocNode()),
5658
),
57-
)->nonIgnorable()->identifier('phpstan.dumpPhpDocType')->build(),
58-
];
59+
)->nonIgnorable()->identifier('phpstan.dumpPhpDocType')->build();
60+
}
61+
62+
return $errors;
5963
}
6064

6165
}

src/Rules/Debug/DumpTypeRule.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ public function processNode(Node $node, Scope $scope): array
3535
return [];
3636
}
3737

38-
if (count($node->getArgs()) === 0) {
38+
$args = $node->getArgs();
39+
if (count($args) === 0) {
3940
return [];
4041
}
4142

@@ -48,14 +49,17 @@ public function processNode(Node $node, Scope $scope): array
4849
return [];
4950
}
5051

51-
return [
52-
RuleErrorBuilder::message(
52+
$errors = [];
53+
foreach ($args as $arg) {
54+
$errors[] = RuleErrorBuilder::message(
5355
sprintf(
5456
'Dumped type: %s',
55-
$scope->getType($node->getArgs()[0]->value)->describe(VerbosityLevel::precise()),
57+
$scope->getType($arg->value)->describe(VerbosityLevel::precise()),
5658
),
57-
)->nonIgnorable()->identifier('phpstan.dumpType')->build(),
58-
];
59+
)->nonIgnorable()->identifier('phpstan.dumpType')->build();
60+
}
61+
62+
return $errors;
5963
}
6064

6165
}

src/dumpType.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,38 @@
55
/**
66
* @phpstan-pure
77
* @param mixed $value
8+
* @param mixed $values
89
* @return mixed
910
*
1011
* @throws void
1112
*/
12-
function dumpType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
13+
function dumpType($value, ...$values) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
1314
{
1415
return null;
1516
}
1617

1718
/**
1819
* @phpstan-pure
1920
* @param mixed $value
21+
* @param mixed $values
2022
* @return mixed
2123
*
2224
* @throws void
2325
*/
24-
function dumpNativeType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
26+
function dumpNativeType($value, ...$values) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
2527
{
2628
return null;
2729
}
2830

2931
/**
3032
* @phpstan-pure
3133
* @param mixed $value
34+
* @param mixed $values
3235
* @return mixed
3336
*
3437
* @throws void
3538
*/
36-
function dumpPhpDocType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
39+
function dumpPhpDocType($value, ...$values) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
3740
{
3841
return null;
3942
}

tests/PHPStan/Rules/Debug/DumpNativeTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ public function testRule(): void
2727
'Dumped type: array',
2828
12,
2929
],
30+
[
31+
'Dumped type: non-empty-array',
32+
14,
33+
],
34+
[
35+
'Dumped type: array',
36+
14,
37+
],
3038
]);
3139
}
3240

tests/PHPStan/Rules/Debug/DumpPhpDocTypeRuleTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ public function testRuleSymbols(): void
100100
'Dumped type: T',
101101
36,
102102
],
103+
[
104+
'Dumped type: array{1: 1}',
105+
41,
106+
],
107+
[
108+
'Dumped type: array{2: 2}',
109+
41,
110+
],
103111
]);
104112
}
105113

tests/PHPStan/Rules/Debug/DumpTypeRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,20 @@ public function testRuleInUse(): void
5050
]);
5151
}
5252

53+
public function testRuleWithMultipleVars(): void
54+
{
55+
$this->analyse([__DIR__ . '/data/dump-type-variadic.php'], [
56+
[
57+
'Dumped type: non-empty-array',
58+
10,
59+
],
60+
[
61+
'Dumped type: array',
62+
10,
63+
],
64+
]);
65+
}
66+
5367
public function testBug7803(): void
5468
{
5569
$this->analyse([__DIR__ . '/data/bug-7803.php'], [

tests/PHPStan/Rules/Debug/data/dump-native-type.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ function (array $a, array $b) {
1010

1111
dumpNativeType($a);
1212
dumpNativeType($b);
13+
14+
dumpNativeType($a, $b);
1315
};

tests/PHPStan/Rules/Debug/data/dump-phpdoc-type.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,5 @@ function id($value)
3737

3838
return $value;
3939
}
40+
41+
dumpPhpDocType([1 => 1], [2 => 2]);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace PHPStan;
4+
5+
function (array $a, array $b) {
6+
if ($a === []) {
7+
return;
8+
}
9+
10+
dumpType($a, $b);
11+
};

0 commit comments

Comments
 (0)