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
5 changes: 5 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,11 @@ services:
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ArrayRandFunctionReturnTypeExtension
tags:
- phpstan.broker.dynamicFunctionReturnTypeExtension

-
class: PHPStan\Type\Php\ArrayReduceFunctionReturnTypeExtension
tags:
Expand Down
63 changes: 63 additions & 0 deletions src/Type/Php/ArrayRandFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php declare(strict_types = 1);

namespace PHPStan\Type\Php;

use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\IntegerType;
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;

class ArrayRandFunctionReturnTypeExtension implements \PHPStan\Type\DynamicFunctionReturnTypeExtension
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prehaps this could also check if the value is bigger than the array size: https://3v4l.org/LYSTs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And how do you get the size of the array ?

{

public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return $functionReflection->getName() === 'array_rand';
}

public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$argsCount = count($functionCall->args);
if (count($functionCall->args) < 1) {
return ParametersAcceptorSelector::selectSingle($functionReflection->getVariants())->getReturnType();
}

$firstArgType = $scope->getType($functionCall->args[0]->value);
$isInteger = (new IntegerType())->isSuperTypeOf($firstArgType->getIterableKeyType());
$isString = (new StringType())->isSuperTypeOf($firstArgType->getIterableKeyType());

if ($isInteger->yes()) {
$valueType = new IntegerType();
} elseif ($isString->yes()) {
$valueType = new StringType();
} else {
$valueType = new UnionType([new IntegerType(), new StringType()]);
}

if ($argsCount < 2) {
return $valueType;
}

$secondArgType = $scope->getType($functionCall->args[1]->value);

if ($secondArgType instanceof ConstantIntegerType) {
if ($secondArgType->getValue() === 1) {
return $valueType;
}

if ($secondArgType->getValue() >= 2) {
return new ArrayType(new IntegerType(), $valueType);
}
}

return TypeCombinator::union($valueType, new ArrayType(new IntegerType(), $valueType));
}

}
64 changes: 64 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5507,6 +5507,70 @@ public function dataArrayFunctions(): array
'\'foo\'',
'$poppedFoo',
],
[
'int',
'array_rand([1 => 1, 2 => "2"])',
],
[
'string',
'array_rand(["a" => 1, "b" => "2"])',
],
[
'int|string',
'array_rand(["a" => 1, 2 => "b"])',
],
[
'int|string',
'array_rand([1 => 1, 2 => "b", $mixed => $mixed])',
],
[
'int',
'array_rand([1 => 1, 2 => "b"], 1)',
],
[
'string',
'array_rand(["a" => 1, "b" => "b"], 1)',
],
[
'int|string',
'array_rand(["a" => 1, 2 => "b"], 1)',
],
[
'int|string',
'array_rand([1 => 1, 2 => "b", $mixed => $mixed], 1)',
],
[
'array<int, int>',
'array_rand([1 => 1, 2 => "b"], 2)',
],
[
'array<int, string>',
'array_rand(["a" => 1, "b" => "b"], 2)',
],
[
'array<int, int|string>',
'array_rand(["a" => 1, 2 => "b"], 2)',
],
[
'array<int, int|string>',
'array_rand([1 => 1, 2 => "2", $mixed => $mixed], 2)',
],
[
'array<int, int>|int',
'array_rand([1 => 1, 2 => "b"], $mixed)',
],
[
'array<int, string>|string',
'array_rand(["a" => 1, "b" => "b"], $mixed)',
],
[
'array<int, int|string>|int|string',
'array_rand(["a" => 1, 2 => "b"], $mixed)',
],
[
'array<int, int|string>|int|string',
'array_rand([1 => 1, 2 => "b", $mixed => $mixed], $mixed)',
],
];
}

Expand Down