Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 17 additions & 0 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,23 @@
return null;
}

if ($context->falsey() && $isConstantArray->yes()) {

Check warning on line 1282 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": @@ @@ return null; } - if ($context->falsey() && $isConstantArray->yes()) { + if ($context->falsey() && !$isConstantArray->no()) { $remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType); if (!$remainingSize instanceof NeverType) { $result = $this->specifyTypesForCountFuncCall(

Check warning on line 1282 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": @@ @@ return null; } - if ($context->falsey() && $isConstantArray->yes()) { + if ($context->false() && $isConstantArray->yes()) { $remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType); if (!$remainingSize instanceof NeverType) { $result = $this->specifyTypesForCountFuncCall(

Check warning on line 1282 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": @@ @@ return null; } - if ($context->falsey() && $isConstantArray->yes()) { + if ($context->falsey() && !$isConstantArray->no()) { $remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType); if (!$remainingSize instanceof NeverType) { $result = $this->specifyTypesForCountFuncCall(

Check warning on line 1282 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": @@ @@ return null; } - if ($context->falsey() && $isConstantArray->yes()) { + if ($context->false() && $isConstantArray->yes()) { $remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType); if (!$remainingSize instanceof NeverType) { $result = $this->specifyTypesForCountFuncCall(
$remainingSize = TypeCombinator::remove($type->getArraySize(), $sizeType);
if (!$remainingSize instanceof NeverType) {
$result = $this->specifyTypesForCountFuncCall(
$countFuncCall,
$type,
$remainingSize,
$context->negate(),
$scope,
$rootExpr,
);
if ($result !== null) {
return $result;
}
}
}

$resultTypes = [];
foreach ($type->getArrays() as $arrayType) {
$isSizeSuperTypeOfArraySize = $sizeType->isSuperTypeOf($arrayType->getArraySize());
Expand Down
2 changes: 1 addition & 1 deletion src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -1900,7 +1900,7 @@ private function optimizeScalarType(Type $type): Type
return $types[0];
}

return new UnionType($types);
return new UnionType(array_values($types));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use function array_map;
use function array_values;
use function count;
use function strtolower;
use function strtoupper;
Expand Down Expand Up @@ -130,7 +131,7 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
if (count($types) === 1) {
return $types[0];
}
return new IntersectionType($types);
return new IntersectionType(array_values($types));
}

return $type;
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14297.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types = 1);

namespace Bug14297;

use function PHPStan\Testing\assertType;

function (): void {
$a = [rand(0, 1) ? 'a' : null];
if (rand(0, 1)) {
$a[] = rand(0, 1) ? 'b' : null;
}

$a = array_values(array_filter($a));
if (count($a) === 0) {
return;
}

assertType("non-empty-list{0?: 'a'|'b', 1?: 'b'}", $a);
assertType("int<1, 2>", count($a));

if (count($a) === 2) {
assertType("array{'a'|'b', 'b'}", $a);
} else {
assertType("array{'a'|'b'}", $a);
}
};
10 changes: 5 additions & 5 deletions tests/PHPStan/Analyser/nsrt/list-count.php
Original file line number Diff line number Diff line change
Expand Up @@ -233,13 +233,13 @@ protected function testOptionalKeys($row): void
if (count($row) === 1) {
assertType('array{mixed}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed, string|null}', $row);
}

if (count($row) === 2) {
assertType('array{mixed, string|null}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed}', $row);
}

if (count($row) === 3) {
Expand All @@ -263,13 +263,13 @@ protected function testOptionalKeysInUnion($row): void
if (count($row) === 1) {
assertType('array{mixed}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed, string|null}', $row);
}

if (count($row) === 2) {
assertType('array{mixed, string|null}', $row);
} else {
assertType('array{0: mixed, 1?: string|null}', $row);
assertType('array{mixed}', $row);
}

if (count($row) === 3) {
Expand All @@ -293,7 +293,7 @@ protected function testOptionalKeysInListsOfTaggedUnion($row): void
if (count($row) === 1) {
assertType('array{0: int, 1?: string|null}|array{string}', $row);
} else {
assertType('array{0: int, 1?: string|null}', $row);
assertType('array{int, string|null}', $row);
}

if (count($row) === 2) {
Expand Down
Loading