Skip to content

Commit 5570289

Browse files
committed
Retain list type when assigning to offset 1 of non-empty-list
1 parent 970117e commit 5570289

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/Type/IntersectionType.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,14 @@ public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $uni
756756
);
757757
});
758758
}
759-
return $this->intersectTypes(static fn (Type $type): Type => $type->setOffsetValueType($offsetType, $valueType, $unionValues));
759+
760+
$result = $this->intersectTypes(static fn (Type $type): Type => $type->setOffsetValueType($offsetType, $valueType, $unionValues));
761+
762+
if ($offsetType !== null && $this->isList()->yes() && $this->isIterableAtLeastOnce()->yes() && (new ConstantIntegerType(1))->isSuperTypeOf($offsetType)->yes()) {
763+
$result = TypeCombinator::intersect($result, new AccessoryArrayListType());
764+
}
765+
766+
return $result;
760767
}
761768

762769
public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,4 +692,10 @@ public function testBug11617(): void
692692
]);
693693
}
694694

695+
public function testBug12131(): void
696+
{
697+
$this->checkExplicitMixed = true;
698+
$this->analyse([__DIR__ . '/data/bug-12131.php'], []);
699+
}
700+
695701
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1); // lint >= 7.4
2+
3+
namespace Bug12131;
4+
5+
class Test
6+
{
7+
/**
8+
* @var non-empty-list<int>
9+
*/
10+
public array $array;
11+
12+
public function __construct()
13+
{
14+
$this->array = array_fill(0, 10, 1);
15+
}
16+
17+
public function setAtZero(): void
18+
{
19+
$this->array[0] = 1;
20+
}
21+
22+
public function setAtOne(): void
23+
{
24+
$this->array[1] = 1;
25+
}
26+
}
27+
28+
$a = new Test();
29+
$a->setAtOne();

0 commit comments

Comments
 (0)