Skip to content

Commit 93ac2e4

Browse files
Allow to dump more than one thing
1 parent d9b9af7 commit 93ac2e4

File tree

10 files changed

+78
-21
lines changed

10 files changed

+78
-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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* @throws void
1111
*/
12-
function dumpType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
12+
function dumpType(...$value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
1313
{
1414
return null;
1515
}
@@ -21,7 +21,7 @@ function dumpType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
2121
*
2222
* @throws void
2323
*/
24-
function dumpNativeType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
24+
function dumpNativeType(...$value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
2525
{
2626
return null;
2727
}
@@ -33,7 +33,7 @@ function dumpNativeType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.F
3333
*
3434
* @throws void
3535
*/
36-
function dumpPhpDocType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
36+
function dumpPhpDocType(...$value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
3737
{
3838
return null;
3939
}

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)