Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
63 changes: 37 additions & 26 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,44 @@

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()) {

Check warning on line 742 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $arrayArg = $expr->expr->getArgs()[0]->value; $arrayType = $scope->getType($arrayArg); - if ($arrayType->isArray()->yes()) { + if (!$arrayType->isArray()->no()) { if ($context->true()) { $specifiedTypes = $specifiedTypes->unionWith( $this->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope),

Check warning on line 742 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $arrayArg = $expr->expr->getArgs()[0]->value; $arrayType = $scope->getType($arrayArg); - if ($arrayType->isArray()->yes()) { + if (!$arrayType->isArray()->no()) { if ($context->true()) { $specifiedTypes = $specifiedTypes->unionWith( $this->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope),
if ($context->true()) {

Check warning on line 743 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ $arrayType = $scope->getType($arrayArg); if ($arrayType->isArray()->yes()) { - if ($context->true()) { + if ($context->truthy()) { $specifiedTypes = $specifiedTypes->unionWith( $this->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope), );

Check warning on line 743 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrueTruthyFalseFalseyTypeSpecifierContextMutator": @@ @@ $arrayType = $scope->getType($arrayArg); if ($arrayType->isArray()->yes()) { - if ($context->true()) { + if ($context->truthy()) { $specifiedTypes = $specifiedTypes->unionWith( $this->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope), );
$specifiedTypes = $specifiedTypes->unionWith(
$this->create($arrayArg, new NonEmptyArrayType(), TypeSpecifierContext::createTrue(), $scope),
);
$isNonEmpty = true;
} else {
$isNonEmpty = $arrayType->isIterableAtLeastOnce()->yes();

Check warning on line 749 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ ); $isNonEmpty = true; } else { - $isNonEmpty = $arrayType->isIterableAtLeastOnce()->yes(); + $isNonEmpty = !$arrayType->isIterableAtLeastOnce()->no(); } if ($isNonEmpty) {

Check warning on line 749 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ ); $isNonEmpty = true; } else { - $isNonEmpty = $arrayType->isIterableAtLeastOnce()->yes(); + $isNonEmpty = !$arrayType->isIterableAtLeastOnce()->no(); } if ($isNonEmpty) {
}

if ($isNonEmpty) {
$dimFetch = new ArrayDimFetch($arrayArg, $expr->var);
$iterableValueType = $expr->expr->name->toLowerString() === 'array_key_first'
Comment thread
VincentLanglet marked this conversation as resolved.
Outdated
? $arrayType->getIterableValueType()
: $arrayType->getIterableValueType();

$specifiedTypes = $specifiedTypes->unionWith(
$this->create($dimFetch, $iterableValueType, 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 +792,6 @@
}
}

// 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 +819,6 @@
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
68 changes: 68 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14081.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?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); // could be int<1, max>
assertType('list<string>', $array); // could be non-empty-list<string>
Comment thread
VincentLanglet marked this conversation as resolved.
Outdated
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); // could be int<1, max>
Comment thread
VincentLanglet marked this conversation as resolved.
Outdated
assertType('list<string>', $array); // could be non-empty-list<string>
assertType('string', $array[$key]);
return $array[$key];
}
return null;
}
Loading