Skip to content
Draft
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
25 changes: 24 additions & 1 deletion src/Analyser/LegacyTypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\HasOffsetType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\HasPropertyType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
Expand Down Expand Up @@ -92,6 +93,8 @@
final class LegacyTypeSpecifier implements TypeSpecifier
{

private const MAX_ACCESSORIES_LIMIT = 8;

/** @var MethodTypeSpecifyingExtension[][]|null */
private ?array $methodTypeSpecifyingExtensionsByClass = null;

Expand Down Expand Up @@ -1189,7 +1192,27 @@
$builderData[] = [$offsetType, $arrayType->getOffsetValueType($offsetType), !$hasOffset->yes()];
}
} else {
$resultTypes[] = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
$intersection = [];
$intersection[] = $arrayType;
$intersection[] = new NonEmptyArrayType();

$zero = new ConstantIntegerType(0);
$i = 0;
foreach ($builderData as [$offsetType, $valueType]) {
// non-empty-list already implies the offset 0
if ($zero->isSuperTypeOf($offsetType)->yes()) {

Check warning on line 1203 in src/Analyser/LegacyTypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $i = 0; foreach ($builderData as [$offsetType, $valueType]) { // non-empty-list already implies the offset 0 - if ($zero->isSuperTypeOf($offsetType)->yes()) { + if (!$zero->isSuperTypeOf($offsetType)->no()) { continue; } if ($i > self::MAX_ACCESSORIES_LIMIT) {

Check warning on line 1203 in src/Analyser/LegacyTypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $i = 0; foreach ($builderData as [$offsetType, $valueType]) { // non-empty-list already implies the offset 0 - if ($zero->isSuperTypeOf($offsetType)->yes()) { + if ($offsetType->isSuperTypeOf($zero)->yes()) { continue; } if ($i > self::MAX_ACCESSORIES_LIMIT) {

Check warning on line 1203 in src/Analyser/LegacyTypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $i = 0; foreach ($builderData as [$offsetType, $valueType]) { // non-empty-list already implies the offset 0 - if ($zero->isSuperTypeOf($offsetType)->yes()) { + if (!$zero->isSuperTypeOf($offsetType)->no()) { continue; } if ($i > self::MAX_ACCESSORIES_LIMIT) {

Check warning on line 1203 in src/Analyser/LegacyTypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $i = 0; foreach ($builderData as [$offsetType, $valueType]) { // non-empty-list already implies the offset 0 - if ($zero->isSuperTypeOf($offsetType)->yes()) { + if ($offsetType->isSuperTypeOf($zero)->yes()) { continue; } if ($i > self::MAX_ACCESSORIES_LIMIT) {

Check warning on line 1203 in src/Analyser/LegacyTypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $i = 0; foreach ($builderData as [$offsetType, $valueType]) { // non-empty-list already implies the offset 0 - if ($zero->isSuperTypeOf($offsetType)->yes()) { + if (!$zero->isSuperTypeOf($offsetType)->no()) { continue; } if ($i > self::MAX_ACCESSORIES_LIMIT) {

Check warning on line 1203 in src/Analyser/LegacyTypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $i = 0; foreach ($builderData as [$offsetType, $valueType]) { // non-empty-list already implies the offset 0 - if ($zero->isSuperTypeOf($offsetType)->yes()) { + if ($offsetType->isSuperTypeOf($zero)->yes()) { continue; } if ($i > self::MAX_ACCESSORIES_LIMIT) {
continue;
}

if ($i > self::MAX_ACCESSORIES_LIMIT) {
break;
}

$intersection[] = new HasOffsetValueType($offsetType, $valueType);
$i++;
}

$resultTypes[] = TypeCombinator::intersect(...$intersection);
Comment on lines +1195 to +1215
Copy link
Contributor Author

@staabm staabm Dec 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also use a AccessoryArraySize, but this likely would be more involved and require a bigger refactor.

therefore I went with this simple approach which covers most useful use-cases

continue;
}

Expand Down
25 changes: 23 additions & 2 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,10 @@
$knownOffsets[$type->getOffsetType()->getValue()] = true;
}

if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) {

Check warning on line 694 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $knownOffsets[$type->getOffsetType()->getValue()] = true; } - if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { + if ($this->isList()->yes() && !$this->isIterableAtLeastOnce()->no()) { $knownOffsets[0] = true; } if ($knownOffsets !== []) {

Check warning on line 694 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $knownOffsets[$type->getOffsetType()->getValue()] = true; } - if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { + if (!$this->isList()->no() && $this->isIterableAtLeastOnce()->yes()) { $knownOffsets[0] = true; } if ($knownOffsets !== []) {

Check warning on line 694 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $knownOffsets[$type->getOffsetType()->getValue()] = true; } - if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { + if ($this->isList()->yes() && !$this->isIterableAtLeastOnce()->no()) { $knownOffsets[0] = true; } if ($knownOffsets !== []) {

Check warning on line 694 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $knownOffsets[$type->getOffsetType()->getValue()] = true; } - if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { + if (!$this->isList()->no() && $this->isIterableAtLeastOnce()->yes()) { $knownOffsets[0] = true; } if ($knownOffsets !== []) {

Check warning on line 694 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $knownOffsets[$type->getOffsetType()->getValue()] = true; } - if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { + if ($this->isList()->yes() && !$this->isIterableAtLeastOnce()->no()) { $knownOffsets[0] = true; } if ($knownOffsets !== []) {

Check warning on line 694 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } $knownOffsets[$type->getOffsetType()->getValue()] = true; } - if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) { + if (!$this->isList()->no() && $this->isIterableAtLeastOnce()->yes()) { $knownOffsets[0] = true; } if ($knownOffsets !== []) {
$knownOffsets[0] = true;
}

if ($knownOffsets !== []) {
return TypeCombinator::intersect($arraySize, IntegerRangeType::fromInterval(count($knownOffsets), null));
}
Expand Down Expand Up @@ -830,9 +834,26 @@

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) {
if ($this->isList()->yes()) {

Check warning on line 837 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } public function hasOffsetValueType(Type $offsetType): TrinaryLogic { - if ($this->isList()->yes()) { + if (!$this->isList()->no()) { $arrayKeyOffsetType = $offsetType->toArrayKey(); $negative = IntegerRangeType::fromInterval(null, -1); if ($negative->isSuperTypeOf($arrayKeyOffsetType)->yes()) {

Check warning on line 837 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } public function hasOffsetValueType(Type $offsetType): TrinaryLogic { - if ($this->isList()->yes()) { + if (!$this->isList()->no()) { $arrayKeyOffsetType = $offsetType->toArrayKey(); $negative = IntegerRangeType::fromInterval(null, -1); if ($negative->isSuperTypeOf($arrayKeyOffsetType)->yes()) {

Check warning on line 837 in src/Type/IntersectionType.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ } public function hasOffsetValueType(Type $offsetType): TrinaryLogic { - if ($this->isList()->yes()) { + if (!$this->isList()->no()) { $arrayKeyOffsetType = $offsetType->toArrayKey(); $negative = IntegerRangeType::fromInterval(null, -1); if ($negative->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
$arrayKeyOffsetType = $offsetType->toArrayKey();
if ((new ConstantIntegerType(0))->isSuperTypeOf($arrayKeyOffsetType)->yes()) {

$negative = IntegerRangeType::fromInterval(null, -1);
if ($negative->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
return TrinaryLogic::createNo();
}

$size = $this->getArraySize();
if ($size instanceof IntegerRangeType && $size->getMin() !== null) {
$knownOffsets = IntegerRangeType::fromInterval(0, $size->getMin() - 1);
} elseif ($size instanceof ConstantIntegerType) {
$knownOffsets = IntegerRangeType::fromInterval(0, $size->getValue());
} elseif ($this->isIterableAtLeastOnce()->yes()) {
$knownOffsets = new ConstantIntegerType(0);
} else {
$knownOffsets = null;
}

if ($knownOffsets !== null && $knownOffsets->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
return TrinaryLogic::createYes();
}

Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11642.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function doFoo() {
if (count($entries) !== count($payload->ids)) {
exit();
}
assertType('int<1, max>', count($entries));

assertType('non-empty-list<stdClass>', $entries);
if (count($entries) > 3) {
Expand Down
99 changes: 99 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-13747.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php declare(strict_types = 1);

namespace Bug13747;

use function PHPStan\Testing\assertType;

class HelloWorld
{
/** @param list<int> $list */
public function count($list): void
{
if (count($list) === 0) {
return;
}

if (count($list) > 2) {
assertType('false', array_key_exists(-1, $list));
assertType('true', array_key_exists(0, $list));
assertType('true', array_key_exists(1, $list));
assertType('true', array_key_exists(2, $list));
assertType('bool', array_key_exists(3, $list));

assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)', $list);
assertType('int<3, max>', count($list));
} else {
assertType('non-empty-list<int>', $list);
}
assertType('non-empty-list<int>', $list);

if (count($list, COUNT_NORMAL) > 2) {
assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)', $list);
assertType('int<3, max>', count($list, COUNT_NORMAL));
} else {
assertType('non-empty-list<int>', $list);
}

assertType('non-empty-list<int>', $list);
if (count($list, COUNT_RECURSIVE) > 2) { // COUNT_RECURSIVE on non-recursive array
assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)', $list);
assertType('int<3, max>', count($list, COUNT_RECURSIVE));
} else {
assertType('non-empty-list<int>', $list);
}
}

/** @param list<int> $list */
public function doFoo($list): void
{
if (count($list) === 0) {
return;
}

if (count($list) >= 2) {
assertType('non-empty-list<int>&hasOffsetValue(1, int)', $list);
assertType('int<2, max>', count($list));
} else {
assertType('non-empty-list<int>', $list);
}
}

/** @param list<int> $list */
public function doBar($list): void
{
if (count($list) === 0) {
return;
}

if (2 <= count($list)) {
assertType('non-empty-list<int>&hasOffsetValue(1, int)', $list);
assertType('int<2, max>', count($list));
} else {
assertType('non-empty-list<int>', $list);
assertType('1', count($list));
}
}

/** @param list<int> $list */
public function checkLimit($list): void
{
if (count($list) === 0) {
return;
}

if (count($list) > 9) {
assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)&hasOffsetValue(3, int)&hasOffsetValue(4, int)&hasOffsetValue(5, int)&hasOffsetValue(6, int)&hasOffsetValue(7, int)&hasOffsetValue(8, int)&hasOffsetValue(9, int)', $list);
assertType('int<10, max>', count($list));
} else {
assertType('non-empty-list<int>', $list);
}

if (count($list) > 10) {
assertType('non-empty-list<int>&hasOffsetValue(1, int)&hasOffsetValue(2, int)&hasOffsetValue(3, int)&hasOffsetValue(4, int)&hasOffsetValue(5, int)&hasOffsetValue(6, int)&hasOffsetValue(7, int)&hasOffsetValue(8, int)&hasOffsetValue(9, int)', $list);
assertType('int<11, max>', count($list));
} else {
assertType('non-empty-list<int>', $list);
}

}
}
12 changes: 6 additions & 6 deletions tests/PHPStan/Analyser/nsrt/count-recursive.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,18 @@ public function countList($list): void
{
if (count($list) > 2) {
assertType('int<3, max>', count($list));
assertType('int<1, max>', count($list, COUNT_NORMAL));
assertType('int<1, max>', count($list, COUNT_RECURSIVE));
assertType('int<3, max>', count($list, COUNT_NORMAL));
assertType('int<3, max>', count($list, COUNT_RECURSIVE));
}
}

/** @param list<int> $list */
public function countListNormal($list): void
{
if (count($list, COUNT_NORMAL) > 2) {
assertType('int<1, max>', count($list));
assertType('int<3, max>', count($list));
assertType('int<3, max>', count($list, COUNT_NORMAL));
assertType('int<1, max>', count($list, COUNT_RECURSIVE));
assertType('int<3, max>', count($list, COUNT_RECURSIVE));
}
}

Expand Down Expand Up @@ -124,8 +124,8 @@ public function countMixed($arr, $mode): void
public function countListRecursive($list): void
{
if (count($list, COUNT_RECURSIVE) > 2) {
assertType('int<1, max>', count($list));
assertType('int<1, max>', count($list, COUNT_NORMAL));
assertType('int<3, max>', count($list));
assertType('int<3, max>', count($list, COUNT_NORMAL));
assertType('int<3, max>', count($list, COUNT_RECURSIVE));
}
}
Expand Down
Loading