Skip to content
Open
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
12 changes: 9 additions & 3 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,15 @@
$sizeType = $leftType;
}

$specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr);
if ($specifiedTypes !== null) {
$result = $result->unionWith($specifiedTypes);
// For range-typed left operands in falsey context ($left <= count($right)
// being false means count < $left), specifyTypesForCountFuncCall would
// incorrectly narrow the array because "count not in sizeType" is stricter
// than "count < some value in sizeType range".
if (!($context->falsey() && $leftType instanceof IntegerRangeType)) {

Check warning on line 296 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": @@ @@ // being false means count < $left), specifyTypesForCountFuncCall would // incorrectly narrow the array because "count not in sizeType" is stricter // than "count < some value in sizeType range". - if (!($context->falsey() && $leftType instanceof IntegerRangeType)) { + if (!($context->false() && $leftType instanceof IntegerRangeType)) { $specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr); if ($specifiedTypes !== null) { $result = $result->unionWith($specifiedTypes);

Check warning on line 296 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": @@ @@ // being false means count < $left), specifyTypesForCountFuncCall would // incorrectly narrow the array because "count not in sizeType" is stricter // than "count < some value in sizeType range". - if (!($context->falsey() && $leftType instanceof IntegerRangeType)) { + if (!($context->false() && $leftType instanceof IntegerRangeType)) { $specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr); if ($specifiedTypes !== null) { $result = $result->unionWith($specifiedTypes);
$specifiedTypes = $this->specifyTypesForCountFuncCall($expr->right, $argType, $sizeType, $context, $scope, $expr);
if ($specifiedTypes !== null) {
$result = $result->unionWith($specifiedTypes);
}
}

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

namespace Bug13705;

use function PHPStan\Testing\assertType;

function whileLoop(): void
{
$quantity = random_int(1, 42);
$codes = [];
while (count($codes) < $quantity) {
assertType('list<non-empty-string>', $codes);
$code = random_bytes(16);
if (!in_array($code, $codes, true)) {
$codes[] = $code;
}
}
}

function doWhileLoop(): void
{
$quantity = random_int(1, 42);
$codes = [];
do {
$code = random_bytes(16);
if (!in_array($code, $codes, true)) {
$codes[] = $code;
}
} while (count($codes) < $quantity);
}

function whileLoopSimple(): void
{
$quantity = random_int(1, 42);
$codes = [];
while (count($codes) < $quantity) {
assertType('list<non-empty-string>', $codes);
$codes[] = random_bytes(16);
}
}
4 changes: 2 additions & 2 deletions tests/PHPStan/Analyser/nsrt/bug-4700.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ function(array $array, int $count): void {
assertType('int<1, 5>', count($a));
assertType('list{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a);
} else {
assertType('0', count($a));
assertType('array{}', $a);
assertType('int<0, 5>', count($a));
assertType('array{}|list{0: mixed~null, 1?: mixed~null, 2?: mixed~null, 3?: mixed~null, 4?: mixed~null}', $a);
}
};

Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug11480.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function intRangeCount($count): void
if (count($x) >= $count) {
assertType("array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
} else {
assertType("array{}", $x);
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
}
assertType("array{}|array{'xy'}|array{0: 'ab', 1?: 'xy'}", $x);
}
Expand Down
Loading