Skip to content

Commit 3351c97

Browse files
committed
ArrayValuesRule - tip message about treatPhpDocTypesAsCertain
1 parent 17e5bac commit 3351c97

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

src/Rules/Functions/ArrayValuesRule.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,45 @@ public function processNode(Node $node, Scope $scope): array
7070
return [];
7171
}
7272

73-
if ($this->treatPhpDocTypesAsCertain === true) {
73+
if ($this->treatPhpDocTypesAsCertain) {
7474
$arrayType = $scope->getType($args[0]->value);
7575
} else {
7676
$arrayType = $scope->getNativeType($args[0]->value);
7777
}
7878

7979
if ($arrayType->isIterableAtLeastOnce()->no()) {
8080
$message = 'Parameter #1 $array (%s) to function array_values is empty, call has no effect.';
81+
$errorBuilder = RuleErrorBuilder::message(sprintf(
82+
$message,
83+
$arrayType->describe(VerbosityLevel::value()),
84+
));
85+
if ($this->treatPhpDocTypesAsCertain) {
86+
$nativeArrayType = $scope->getNativeType($args[0]->value);
87+
if (!$nativeArrayType->isIterableAtLeastOnce()->no()) {
88+
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
89+
}
90+
}
8191

8292
return [
83-
RuleErrorBuilder::message(sprintf(
84-
$message,
85-
$arrayType->describe(VerbosityLevel::value()),
86-
))->build(),
93+
$errorBuilder->build(),
8794
];
8895
}
8996

9097
if ($arrayType->isList()->yes()) {
9198
$message = 'Parameter #1 $array (%s) of array_values is already a list, call has no effect.';
99+
$errorBuilder = RuleErrorBuilder::message(sprintf(
100+
$message,
101+
$arrayType->describe(VerbosityLevel::value()),
102+
));
103+
if ($this->treatPhpDocTypesAsCertain) {
104+
$nativeArrayType = $scope->getNativeType($args[0]->value);
105+
if (!$nativeArrayType->isList()->yes()) {
106+
$errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.');
107+
}
108+
}
92109

93110
return [
94-
RuleErrorBuilder::message(sprintf(
95-
$message,
96-
$arrayType->describe(VerbosityLevel::value()),
97-
))->build(),
111+
$errorBuilder->build(),
98112
];
99113
}
100114

tests/PHPStan/Rules/Functions/ArrayValuesRuleTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ protected function getRule(): Rule
2121

2222
public function testFile(): void
2323
{
24+
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.';
2425
$expectedErrors = [
2526
[
2627
'Parameter #1 $array (array{0, 1, 3}) of array_values is already a list, call has no effect.',
@@ -41,6 +42,7 @@ public function testFile(): void
4142
[
4243
'Parameter #1 $array (list<int>) of array_values is already a list, call has no effect.',
4344
14,
45+
$tipText,
4446
],
4547
[
4648
'Parameter #1 $array (array{0}) of array_values is already a list, call has no effect.',
@@ -58,12 +60,18 @@ public function testFile(): void
5860
'Parameter #1 $array (array{}) to function array_values is empty, call has no effect.',
5961
21,
6062
],
63+
[
64+
'Parameter #1 $array (array{}) to function array_values is empty, call has no effect.',
65+
25,
66+
$tipText,
67+
],
6168
];
6269

6370
if (PHP_VERSION_ID >= 80000) {
6471
$expectedErrors[] = [
6572
'Parameter #1 $array (list<int>) of array_values is already a list, call has no effect.',
66-
24,
73+
28,
74+
$tipText,
6775
];
6876
}
6977

tests/PHPStan/Rules/Functions/data/array_values_list.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,9 @@
2020
array_values([null, 0]);
2121
array_values([]);
2222

23+
/** @var array{} $empty */
24+
$empty = doFoo();
25+
array_values($empty);
26+
2327
array_values(unused: true, array: $array);
2428
array_values(unused: true, array: $list);

0 commit comments

Comments
 (0)