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
60 changes: 34 additions & 26 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,41 @@ public function specifyTypesInCondition(

if ($context->null()) {
$specifiedTypes = $this->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->expr, $context)->setRootExpr($expr);
} else {
$specifiedTypes = $this->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->var, $context)->setRootExpr($expr);
}

// infer $arr[$key] after $key = array_key_first/last($arr)
if (
$expr->expr instanceof FuncCall
&& $expr->expr->name instanceof Name
&& in_array($expr->expr->name->toLowerString(), ['array_key_first', 'array_key_last'], true)
&& count($expr->expr->getArgs()) >= 1
) {
$arrayArg = $expr->expr->getArgs()[0]->value;
$arrayType = $scope->getType($arrayArg);

if ($arrayType->isArray()->yes()) {
if ($context->true()) {
$specifiedTypes = $specifiedTypes->unionWith(
$this->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope),
);
$isNonEmpty = true;
} else {
$isNonEmpty = $arrayType->isIterableAtLeastOnce()->yes();
}

if ($isNonEmpty) {
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);

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

if ($context->null()) {
// infer $arr[$key] after $key = array_rand($arr)
if (
$expr->expr instanceof FuncCall
Expand Down Expand Up @@ -755,30 +789,6 @@ public function specifyTypesInCondition(
}
}

// infer $arr[$key] after $key = array_key_first/last($arr)
if (
$expr->expr instanceof FuncCall
&& $expr->expr->name instanceof Name
&& in_array($expr->expr->name->toLowerString(), ['array_key_first', 'array_key_last'], true)
&& count($expr->expr->getArgs()) >= 1
) {
$arrayArg = $expr->expr->getArgs()[0]->value;
$arrayType = $scope->getType($arrayArg);
if (
$arrayType->isArray()->yes()
&& $arrayType->isIterableAtLeastOnce()->yes()
) {
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);
$iterableValueType = $expr->expr->name->toLowerString() === 'array_key_first'
? $arrayType->getIterableValueType()
: $arrayType->getIterableValueType();

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

// infer $list[$count] after $count = count($list) - 1
if (
$expr->expr instanceof Expr\BinaryOp\Minus
Expand Down Expand Up @@ -806,8 +816,6 @@ public function specifyTypesInCondition(
return $specifiedTypes;
}

$specifiedTypes = $this->specifyTypesInCondition($scope->exitFirstLevelStatements(), $expr->var, $context)->setRootExpr($expr);

if ($context->true()) {
// infer $arr[$key] after $key = array_search($needle, $arr)
if (
Expand Down
85 changes: 85 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14081.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

declare(strict_types = 1);

namespace Bug14081;

use function PHPStan\Testing\assertType;

/** @param list<string> $array */
function first(array $array): mixed
{
if (($key = array_key_first($array))) {
assertType('int<1, max>', $key);
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}

/** @param list<string> $array */
function last(array $array): mixed
{
if (($key = array_key_last($array))) {
assertType('int<1, max>', $key);
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}

function maybeNonEmpty(): void
{
if (rand(0,1)) {
$array = ['one', 'two'];
} else {
$array = [];
}
assertType("array{}|array{'one', 'two'}", $array);
$key = array_key_last($array);
assertType('0|1|null', $key);
assertType("'one'|'two'", $array[$key]);
}

/** @param list<string> $array */
function firstNotNull(array $array): mixed
{
if (($key = array_key_first($array)) !== null) {
assertType('int<0, max>', $key);
assertType('list<string>', $array); // could be non-empty-list<string>
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}

/** @param list<string> $array */
function lastNotNull(array $array): mixed
{
if (($key = array_key_last($array)) !== null) {
assertType('int<0, max>', $key);
assertType('list<string>', $array); // could be non-empty-list<string>
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}

/** @param list<string> $array */
function noIf(array $array): void
{
$key = array_key_first($array);
assertType('int<0, max>|null', $key);
assertType('list<string>', $array);
assertType('string', $array[$key]);

if ($array === []) {
return;
}
$key = array_key_first($array);
assertType('int<0, max>', $key);
assertType('non-empty-list<string>', $array);
assertType('string', $array[$key]);
}
Loading