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
28 changes: 28 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,34 @@ public function specifyTypesInCondition(
if ($context->null()) {
$specifiedTypes = $this->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->expr, $context)->setRootExpr($expr);

// infer $arr[$key] after $key = array_rand($arr)
if (
$expr->expr instanceof FuncCall
&& $expr->expr->name instanceof Name
&& in_array($expr->expr->name->toLowerString(), ['array_rand'], true)
&& count($expr->expr->getArgs()) >= 1
) {
$numArg = null;
$arrayArg = $expr->expr->getArgs()[0]->value;
if (count($expr->expr->getArgs()) > 1) {
$numArg = $expr->expr->getArgs()[1]->value;
}
$one = new ConstantIntegerType(1);
$arrayType = $scope->getType($arrayArg);

if (
$arrayType->isArray()->yes()
&& $arrayType->isIterableAtLeastOnce()->yes()
&& ($numArg === null || $one->isSuperTypeOf($scope->getType($numArg))->yes())
) {
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);

return $specifiedTypes->unionWith(
$this->create($dimFetch, $arrayType->getIterableValueType(), TypeSpecifierContext::createTrue(), $scope),
);
}
}

// infer $arr[$key] after $key = array_key_first/last($arr)
if (
$expr->expr instanceof FuncCall
Expand Down
28 changes: 28 additions & 0 deletions src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
Expand Down Expand Up @@ -129,6 +130,33 @@ public function processNode(Node $node, Scope $scope): array
}
}

if (
$node->dim instanceof Node\Expr\FuncCall
&& $node->dim->name instanceof Node\Name
&& $node->dim->name->toLowerString() === 'array_rand'
&& count($node->dim->getArgs()) >= 1
) {
$numArg = null;
$arrayArg = $node->dim->getArgs()[0]->value;
if (count($node->dim->getArgs()) > 1) {
$numArg = $node->dim->getArgs()[1]->value;
}
$one = new ConstantIntegerType(1);
$arrayType = $scope->getType($arrayArg);

if (
$arrayArg instanceof Node\Expr\Variable
&& $node->var instanceof Node\Expr\Variable
&& is_string($arrayArg->name)
&& $arrayArg->name === $node->var->name
&& $arrayType->isArray()->yes()
&& $arrayType->isIterableAtLeastOnce()->yes()
&& ($numArg === null || $one->isSuperTypeOf($scope->getType($numArg))->yes())
) {
return [];
}
}

if (
$node->dim instanceof Node\Expr\BinaryOp\Minus
&& $node->dim->left instanceof Node\Expr\FuncCall
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private static function findTestFiles(): iterable
}

yield __DIR__ . '/../Rules/Methods/data/bug-6856.php';
yield __DIR__ . '/../Rules/Arrays/data/bug-12981.php';

if (PHP_VERSION_ID < 80000) {
yield __DIR__ . '/data/explode-php74.php';
Expand Down
22 changes: 22 additions & 0 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,26 @@ public function testBug12273(): void
]);
}

public function testBug12981(): void
{
$this->analyse([__DIR__ . '/data/bug-12981.php'], [
[
'Invalid array key type array<int, int|string>.',
31,
],
[
'Invalid array key type array<int, int|string>.',
33,
],
[
'Possibly invalid array key type array<int, int|string>|int|string.',
39,
],
[
'Possibly invalid array key type array<int, int|string>|int|string.',
41,
],
]);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,22 @@ public function testBug12593(): void
$this->analyse([__DIR__ . '/data/bug-12593.php'], []);
}

public function testBug12981(): void
{
$this->reportPossiblyNonexistentGeneralArrayOffset = true;

$this->analyse([__DIR__ . '/data/bug-12981.php'], [
[
'Offset array<int, int|string>|int|string might not exist on non-empty-array<bool|float|int|string>.',
39,
],
[
'Offset array<int, int|string>|int|string might not exist on non-empty-array<bool|float|int|string>.',
41,
],
]);
}

public function testBugObject(): void
{
$this->analyse([__DIR__ . '/data/bug-object.php'], [
Expand Down
60 changes: 60 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/bug-12981.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php declare(strict_types = 1);

namespace Bug12981;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/** @param non-empty-array<scalar> $arr */
public function sayHello(array $arr): void
{
echo $arr[array_rand($arr)];
$randIndex = array_rand($arr);
assertType('bool|float|int|string', $arr[$randIndex]);
echo $arr[$randIndex];
}

/** @param non-empty-array<scalar> $arr */
public function sayHello1(array $arr): void
{
$num = 1;
echo $arr[array_rand($arr, $num)];
$randIndex = array_rand($arr, $num);
echo $arr[$randIndex];
}

/** @param non-empty-array<scalar> $arr */
public function sayHello2(array $arr): void
{
$num = 5;
echo $arr[array_rand($arr, $num)];
$randIndex = array_rand($arr, $num);
echo $arr[$randIndex];
}

/** @param non-empty-array<scalar> $arr */
public function sayHello4(array $arr, int $num): void
{
echo $arr[array_rand($arr, $num)];
$randIndex = array_rand($arr, $num);
echo $arr[$randIndex];
}

public function sayHello5(): void
{
$arr = [
1 => true,
2 => false,
'a' => 'hello',
];
assertType("'hello'|bool", $arr[array_rand($arr)]);

$arr = [
1 => true,
2 => null,
'a' => 'hello',
];
assertType("'hello'|true|null", $arr[array_rand($arr)]);
}
}
Loading