Skip to content

Commit

Permalink
non-empty-list always has offset 0
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 24, 2024
1 parent 26e949b commit f4d1d48
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
Expand Down Expand Up @@ -673,6 +674,13 @@ public function isOffsetAccessible(): TrinaryLogic

public function hasOffsetValueType(Type $offsetType): TrinaryLogic
{
if ($this->isList()->yes() && $this->isIterableAtLeastOnce()->yes()) {
$arrayKeyOffsetType = $offsetType->toArrayKey();
if ((new ConstantIntegerType(0))->isSuperTypeOf($arrayKeyOffsetType)->yes()) {
return TrinaryLogic::createYes();
}
}

return $this->intersectResults(static fn (Type $type): TrinaryLogic => $type->hasOffsetValueType($offsetType));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ public function testDimUnion(array $a, string $dim): void
echo $a[$dim];
}

/**
* @param non-empty-list<string> $a
*/
public function nonEmpty(array $a): void
{
echo $a[0];
}

}

0 comments on commit f4d1d48

Please sign in to comment.