Skip to content

Commit

Permalink
Remove unnecessary array_values usage
Browse files Browse the repository at this point in the history
  • Loading branch information
kamil-zacek committed Feb 14, 2024
1 parent 1ed0eac commit fc57783
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
4 changes: 4 additions & 0 deletions build/spl-autoload-functions-pre-php-7.neon
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ parameters:
message: "#^PHPDoc tag @var with type array\\<callable\\(\\)\\: mixed\\>\\|false is not subtype of native type list\\<callable\\(string\\)\\: void\\>\\|false\\.$#"
count: 2
path: ../src/Command/CommandHelper.php

-
message: '#^Parameter \#1 \$array \(list<PHPStan\\Type\\Type>\) of array_values is already a list, call has no effect\.$#'
path: ../src/Type/TypeCombinator.php
23 changes: 20 additions & 3 deletions src/Rules/Functions/ArrayValuesRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -48,13 +50,28 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$args = $node->getArgs();
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$functionReflection->getVariants(),
null,
);

$normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);

if ($normalizedFuncCall === null) {
return [];
}

$args = $normalizedFuncCall->getArgs();

if (count($args) !== 1) {
return [];
}

if ($this->treatPhpDocTypesAsCertain) {
if ($this->treatPhpDocTypesAsCertain === true) {
$arrayType = $scope->getType($args[0]->value);
} else {
$arrayType = $scope->getNativeType($args[0]->value);
Expand All @@ -71,7 +88,7 @@ public function processNode(Node $node, Scope $scope): array
];
}

if ($arrayType->isList()->yes() && $arrayType->isIterable()->yes()) {
if ($arrayType->isList()->yes()) {
$message = 'Parameter #1 $array (%s) of array_values is already a list, call has no effect.';

return [
Expand Down
2 changes: 1 addition & 1 deletion src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public function getAllArrays(): array
$keys = array_merge($requiredKeys, $combination);
sort($keys);

if ($this->isList->yes() && array_keys($keys) !== array_values($keys)) {
if ($this->isList->yes() && array_keys($keys) !== $keys) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1003,7 +1003,7 @@ public static function intersect(Type ...$types): Type

if ($hasOffsetValueTypeCount > 32) {
$newTypes[] = new OversizedArrayType();
$types = array_values($newTypes);
$types = $newTypes;
$typesCount = count($types);
}

Expand Down
9 changes: 8 additions & 1 deletion tests/PHPStan/Rules/Functions/ArrayValuesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,16 @@ public function testFile(): void
[
'Parameter #1 $array (array{}) to function array_values is empty, call has no effect.',
21,
],
]
];

if (PHP_VERSION_ID >= 80000) {
$expectedErrors[] = [
'Parameter #1 $array (list<int>) of array_values is already a list, call has no effect.',
24,
];
}

$this->analyse([__DIR__ . '/data/array_values_list.php'], $expectedErrors);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/PHPStan/Rules/Functions/data/array_values_list.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@
array_values([null, null]);
array_values([null, 0]);
array_values([]);

array_values(array: $array);
array_values(array: $list);

0 comments on commit fc57783

Please sign in to comment.