Skip to content

Commit 1e09290

Browse files
committed
forbidEnumInFunctionArguments: skip those detected by PHPStan itself
1 parent 4a8e922 commit 1e09290

File tree

2 files changed

+14
-37
lines changed

2 files changed

+14
-37
lines changed

Diff for: src/Rule/ForbidEnumInFunctionArgumentsRule.php

+2-25
Original file line numberDiff line numberDiff line change
@@ -18,41 +18,26 @@
1818
use function array_key_exists;
1919
use function count;
2020
use function implode;
21-
use const SORT_REGULAR;
2221

2322
/**
2423
* @template-implements Rule<FuncCall>
2524
*/
2625
class ForbidEnumInFunctionArgumentsRule implements Rule
2726
{
2827

29-
private const ANY_ARGUMENT = -1;
30-
31-
private const REASON_IMPLICIT_TO_STRING = 'as the function causes implicit __toString conversion which is not supported for enums';
3228
private const REASON_UNPREDICTABLE_RESULT = 'as the function causes unexpected results'; // https://3v4l.org/YtGVa
33-
private const REASON_SKIPS_ENUMS = 'as the function will skip any enums and produce warning';
3429

3530
/**
3631
* Function name -> [forbidden argument position, reason]]
3732
*/
3833
private const FUNCTION_MAP = [
39-
'array_intersect' => [self::ANY_ARGUMENT, self::REASON_IMPLICIT_TO_STRING],
40-
'array_intersect_assoc' => [self::ANY_ARGUMENT, self::REASON_IMPLICIT_TO_STRING],
41-
'array_diff' => [self::ANY_ARGUMENT, self::REASON_IMPLICIT_TO_STRING],
42-
'array_diff_assoc' => [self::ANY_ARGUMENT, self::REASON_IMPLICIT_TO_STRING],
43-
'array_unique' => [0, self::REASON_IMPLICIT_TO_STRING],
44-
'array_combine' => [0, self::REASON_IMPLICIT_TO_STRING],
4534
'sort' => [0, self::REASON_UNPREDICTABLE_RESULT],
4635
'asort' => [0, self::REASON_UNPREDICTABLE_RESULT],
4736
'arsort' => [0, self::REASON_UNPREDICTABLE_RESULT],
48-
'natsort' => [0, self::REASON_IMPLICIT_TO_STRING],
49-
'natcasesort' => [0, self::REASON_IMPLICIT_TO_STRING],
50-
'array_count_values' => [0, self::REASON_SKIPS_ENUMS],
51-
'array_fill_keys' => [0, self::REASON_IMPLICIT_TO_STRING],
52-
'array_flip' => [0, self::REASON_SKIPS_ENUMS],
37+
38+
// https://github.com/phpstan/phpstan/issues/11883
5339
'array_product' => [0, self::REASON_UNPREDICTABLE_RESULT],
5440
'array_sum' => [0, self::REASON_UNPREDICTABLE_RESULT],
55-
'implode' => [1, self::REASON_IMPLICIT_TO_STRING],
5641
];
5742

5843
private ReflectionProvider $reflectionProvider;
@@ -103,10 +88,6 @@ public function processNode(Node $node, Scope $scope): array
10388
foreach ($funcCall->getArgs() as $position => $argument) {
10489
$argumentType = $scope->getType($argument->value);
10590

106-
if ($functionName === 'array_unique' && $position === 1 && $argumentType->getConstantScalarValues() === [SORT_REGULAR]) {
107-
return []; // this edgecase seems to be working fine
108-
}
109-
11091
if (!$this->matchesPosition((int) $position, $forbiddenArgumentPosition)) {
11192
continue;
11293
}
@@ -130,10 +111,6 @@ public function processNode(Node $node, Scope $scope): array
130111

131112
private function matchesPosition(int $position, int $forbiddenArgumentPosition): bool
132113
{
133-
if ($forbiddenArgumentPosition === self::ANY_ARGUMENT) {
134-
return true;
135-
}
136-
137114
return $position === $forbiddenArgumentPosition;
138115
}
139116

Diff for: tests/Rule/data/ForbidEnumInFunctionArgumentsRule/code.php

+12-12
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ public function testAllFunctions()
2222
$enums2 = [SomeEnum::Bar];
2323
$enums3 = [SomeEnum::Baz];
2424

25-
array_intersect($enums1, $enums2, $enums3); // error: Arguments 1, 2, 3 in array_intersect() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
26-
array_intersect_assoc($enums1, $enums2, $enums3); // error: Arguments 1, 2, 3 in array_intersect_assoc() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
27-
array_diff($enums1, $enums2, $enums3); // error: Arguments 1, 2, 3 in array_diff() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
28-
array_diff_assoc($enums1, $enums2, $enums3); // error: Arguments 1, 2, 3 in array_diff_assoc() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
29-
array_unique($enums1); // error: Argument 1 in array_unique() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
30-
array_combine($enums2, $enums3); // error: Argument 1 in array_combine() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
25+
array_intersect($enums1, $enums2, $enums3); // reported by native PHPStan
26+
array_intersect_assoc($enums1, $enums2, $enums3); // reported by native PHPStan
27+
array_diff($enums1, $enums2, $enums3); // reported by native PHPStan
28+
array_diff_assoc($enums1, $enums2, $enums3); // reported by native PHPStan
29+
array_unique($enums1); // reported by native PHPStan
30+
array_combine($enums2, $enums3); // reported by native PHPStan
3131
sort($enums1); // error: Argument 1 in sort() cannot contain enum as the function causes unexpected results
3232
asort($enums1); // error: Argument 1 in asort() cannot contain enum as the function causes unexpected results
3333
arsort($enums1); // error: Argument 1 in arsort() cannot contain enum as the function causes unexpected results
34-
natsort($enums1); // error: Argument 1 in natsort() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
35-
natcasesort($enums1); // error: Argument 1 in natcasesort() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
36-
array_count_values($enums1); // error: Argument 1 in array_count_values() cannot contain enum as the function will skip any enums and produce warning
37-
array_fill_keys($enums1, 1); // error: Argument 1 in array_fill_keys() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
38-
array_flip($enums1); // error: Argument 1 in array_flip() cannot contain enum as the function will skip any enums and produce warning
34+
natsort($enums1); // reported by native PHPStan
35+
natcasesort($enums1); // reported by native PHPStan
36+
array_count_values($enums1); // reported by native PHPStan
37+
array_fill_keys($enums1, 1); // reported by native PHPStan
38+
array_flip($enums1); // reported by native PHPStan
3939
array_product($enums1); // error: Argument 1 in array_product() cannot contain enum as the function causes unexpected results
4040
array_sum($enums1); // error: Argument 1 in array_sum() cannot contain enum as the function causes unexpected results
41-
implode('', $enums1); // error: Argument 2 in implode() cannot contain enum as the function causes implicit __toString conversion which is not supported for enums
41+
implode('', $enums1); // reported by native PHPStan
4242
in_array(SomeEnum::Bar, $enums1);
4343
}
4444

0 commit comments

Comments
 (0)