Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Rules/Debug/DumpNativeTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (count($node->getArgs()) === 0) {
$args = $node->getArgs();
if (count($args) === 0) {
return [];
}

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

return [
RuleErrorBuilder::message(
$errors = [];
foreach ($args as $arg) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'Dumped type: %s',
$scope->getNativeType($node->getArgs()[0]->value)->describe(VerbosityLevel::precise()),
$scope->getNativeType($arg->value)->describe(VerbosityLevel::precise()),
),
)->nonIgnorable()->identifier('phpstan.dumpNativeType')->build(),
];
)->nonIgnorable()->identifier('phpstan.dumpNativeType')->build();
}

return $errors;
}

}
16 changes: 10 additions & 6 deletions src/Rules/Debug/DumpPhpDocTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (count($node->getArgs()) === 0) {
$args = $node->getArgs();
if (count($args) === 0) {
return [];
}

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

return [
RuleErrorBuilder::message(
$errors = [];
foreach ($args as $arg) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'Dumped type: %s',
$this->printer->print($scope->getType($node->getArgs()[0]->value)->toPhpDocNode()),
$this->printer->print($scope->getType($arg->value)->toPhpDocNode()),
),
)->nonIgnorable()->identifier('phpstan.dumpPhpDocType')->build(),
];
)->nonIgnorable()->identifier('phpstan.dumpPhpDocType')->build();
}

return $errors;
}

}
16 changes: 10 additions & 6 deletions src/Rules/Debug/DumpTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

if (count($node->getArgs()) === 0) {
$args = $node->getArgs();
if (count($args) === 0) {
return [];
}

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

return [
RuleErrorBuilder::message(
$errors = [];
foreach ($args as $arg) {
$errors[] = RuleErrorBuilder::message(
sprintf(
'Dumped type: %s',
$scope->getType($node->getArgs()[0]->value)->describe(VerbosityLevel::precise()),
$scope->getType($arg->value)->describe(VerbosityLevel::precise()),
),
)->nonIgnorable()->identifier('phpstan.dumpType')->build(),
];
)->nonIgnorable()->identifier('phpstan.dumpType')->build();
}

return $errors;
}

}
9 changes: 6 additions & 3 deletions src/dumpType.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,38 @@
/**
* @phpstan-pure
* @param mixed $value
* @param mixed $values
* @return mixed
*
* @throws void
*/
function dumpType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
function dumpType($value, ...$values) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
{
return null;
}

/**
* @phpstan-pure
* @param mixed $value
* @param mixed $values
* @return mixed
*
* @throws void
*/
function dumpNativeType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
function dumpNativeType($value, ...$values) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
{
return null;
}

/**
* @phpstan-pure
* @param mixed $value
* @param mixed $values
* @return mixed
*
* @throws void
*/
function dumpPhpDocType($value) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
function dumpPhpDocType($value, ...$values) // phpcs:ignore Squiz.Functions.GlobalFunction.Found
{
return null;
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Debug/DumpNativeTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ public function testRule(): void
'Dumped type: array',
12,
],
[
'Dumped type: non-empty-array',
14,
],
[
'Dumped type: array',
14,
],
]);
}

Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Rules/Debug/DumpPhpDocTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public function testRuleSymbols(): void
'Dumped type: T',
36,
],
[
'Dumped type: array{1: 1}',
41,
],
[
'Dumped type: array{2: 2}',
41,
],
]);
}

Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Rules/Debug/DumpTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public function testRuleInUse(): void
]);
}

public function testRuleWithMultipleVars(): void
{
$this->analyse([__DIR__ . '/data/dump-type-variadic.php'], [
[
'Dumped type: non-empty-array',
10,
],
[
'Dumped type: array',
10,
],
]);
}

public function testBug7803(): void
{
$this->analyse([__DIR__ . '/data/bug-7803.php'], [
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Debug/data/dump-native-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ function (array $a, array $b) {

dumpNativeType($a);
dumpNativeType($b);

dumpNativeType($a, $b);
};
2 changes: 2 additions & 0 deletions tests/PHPStan/Rules/Debug/data/dump-phpdoc-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@ function id($value)

return $value;
}

dumpPhpDocType([1 => 1], [2 => 2]);
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Debug/data/dump-type-variadic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PHPStan;

function (array $a, array $b) {
if ($a === []) {
return;
}

dumpType($a, $b);
};
18 changes: 18 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2679,6 +2679,24 @@ public function testBug8936(): void
$this->analyse([__DIR__ . '/data/bug-8936.php'], []);
}

public function testDumpFunctions(): void
{
$this->analyse([__DIR__ . '/data/dump-functions.php'], [
[
'Function PHPStan\dumpType invoked with 0 parameters, at least 1 required.',
13,
],
[
'Function PHPStan\dumpNativeType invoked with 0 parameters, at least 1 required.',
14,
],
[
'Function PHPStan\dumpPhpDocType invoked with 0 parameters, at least 1 required.',
15,
],
]);
}

public function testBug14012(): void
{
$this->checkExplicitMixed = true;
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Functions/data/dump-functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace DumpFunctions;

use function PHPStan\dumpType;
use function PHPStan\dumpNativeType;
use function PHPStan\dumpPhpdocType;

class HelloWorld
{
public function sayHello(): void
{
dumpType();
dumpNativeType();
dumpPhpdocType();

dumpType(1, 2, 3);
dumpNativeType(1, 2, 3);
dumpPhpdocType(1, 2, 3);
}
}
Loading