-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0b78c55
commit 37f3c82
Showing
10 changed files
with
218 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
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; | ||
use PHPStan\Type\Accessory\AccessoryArrayListType; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function count; | ||
use function sprintf; | ||
use function strtolower; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\FuncCall> | ||
*/ | ||
class ArrayValuesRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private readonly ReflectionProvider $reflectionProvider, | ||
private readonly bool $treatPhpDocTypesAsCertain, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!($node->name instanceof Node\Name)) { | ||
return []; | ||
} | ||
|
||
if (AccessoryArrayListType::isListTypeEnabled() === false) { | ||
return []; | ||
} | ||
|
||
$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope); | ||
|
||
if ($functionName === null || strtolower($functionName) !== 'array_values') { | ||
return []; | ||
} | ||
|
||
$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 === true) { | ||
$arrayType = $scope->getType($args[0]->value); | ||
} else { | ||
$arrayType = $scope->getNativeType($args[0]->value); | ||
} | ||
|
||
if ($arrayType->isIterableAtLeastOnce()->no()) { | ||
$message = 'Parameter #1 $array (%s) to function array_values is empty, call has no effect.'; | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
$message, | ||
$arrayType->describe(VerbosityLevel::value()), | ||
))->build(), | ||
]; | ||
} | ||
|
||
if ($arrayType->isList()->yes()) { | ||
$message = 'Parameter #1 $array (%s) of array_values is already a list, call has no effect.'; | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
$message, | ||
$arrayType->describe(VerbosityLevel::value()), | ||
))->build(), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<ArrayValuesRule> | ||
*/ | ||
class ArrayValuesRuleTest extends RuleTestCase | ||
{ | ||
|
||
private bool $treatPhpDocTypesAsCertain = true; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ArrayValuesRule($this->createReflectionProvider(), $this->treatPhpDocTypesAsCertain); | ||
} | ||
|
||
public function testFile(): void | ||
{ | ||
$expectedErrors = [ | ||
[ | ||
'Parameter #1 $array (array{0, 1, 3}) of array_values is already a list, call has no effect.', | ||
8, | ||
], | ||
[ | ||
'Parameter #1 $array (array{1, 3}) of array_values is already a list, call has no effect.', | ||
9, | ||
], | ||
[ | ||
'Parameter #1 $array (array{\'test\'}) of array_values is already a list, call has no effect.', | ||
10, | ||
], | ||
[ | ||
'Parameter #1 $array (array{\'\', \'test\'}) of array_values is already a list, call has no effect.', | ||
12, | ||
], | ||
[ | ||
'Parameter #1 $array (list<int>) of array_values is already a list, call has no effect.', | ||
14, | ||
], | ||
[ | ||
'Parameter #1 $array (array{0}) of array_values is already a list, call has no effect.', | ||
17, | ||
], | ||
[ | ||
'Parameter #1 $array (array{null, null}) of array_values is already a list, call has no effect.', | ||
19, | ||
], | ||
[ | ||
'Parameter #1 $array (array{null, 0}) of array_values is already a list, call has no effect.', | ||
20, | ||
], | ||
[ | ||
'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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** @var list<int> $list */ | ||
$list = [1, 2, 3]; | ||
/** @var list<int> $list */ | ||
$array = ['a' => 1, 'b' => 2, 'c' => 3]; | ||
|
||
array_values([0,1,3]); | ||
array_values([1,3]); | ||
array_values(['test']); | ||
array_values(['a' => 'test']); | ||
array_values(['', 'test']); | ||
array_values(['a' => '', 'b' => 'test']); | ||
array_values($list); | ||
array_values($array); | ||
|
||
array_values([0]); | ||
array_values(['a' => null, 'b' => null]); | ||
array_values([null, null]); | ||
array_values([null, 0]); | ||
array_values([]); | ||
|
||
array_values(array: $array); | ||
array_values(array: $list); |