Skip to content

Commit 14b4672

Browse files
committed
Fix plus operator with plus
1 parent 8bb2a20 commit 14b4672

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Reflection/InitializerExprTypeResolver.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
3535
use PHPStan\Type\Accessory\AccessoryNumericStringType;
3636
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
37+
use PHPStan\Type\Accessory\HasOffsetValueType;
3738
use PHPStan\Type\Accessory\NonEmptyArrayType;
3839
use PHPStan\Type\ArrayType;
3940
use PHPStan\Type\BenevolentUnionType;
@@ -1078,6 +1079,16 @@ public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback):
10781079
TypeCombinator::union($leftType->getIterableValueType(), $rightType->getIterableValueType()),
10791080
);
10801081

1082+
foreach ($leftType->getConstantArrays() as $type) {
1083+
foreach ($type->getKeyTypes() as $i => $offsetType) {
1084+
if ($type->isOptionalKey($i)) {
1085+
continue;
1086+
}
1087+
$valueType = $type->getValueTypes()[$i];
1088+
$arrayType = TypeCombinator::intersect($arrayType, new HasOffsetValueType($offsetType, $valueType));
1089+
}
1090+
}
1091+
10811092
if ($leftType->isIterableAtLeastOnce()->yes() || $rightType->isIterableAtLeastOnce()->yes()) {
10821093
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
10831094
}

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,6 +1516,12 @@ public function testBug13310(): void
15161516
$this->assertNoErrors($errors);
15171517
}
15181518

1519+
public function testBug11912(): void
1520+
{
1521+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-11912.php');
1522+
$this->assertNoErrors($errors);
1523+
}
1524+
15191525
/**
15201526
* @param string[]|null $allAnalysedFiles
15211527
* @return Error[]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug11912;
4+
5+
/**
6+
* @param array<string, mixed> $results
7+
* @param list<string> $names
8+
*/
9+
function appendResults(array $results, array $names): bool {
10+
// Make sure 'names' comes first in array
11+
$results = ['names' => $names] + $results;
12+
\PHPStan\Testing\assertType("list<string>", $results['names']);
13+
return true;
14+
}

0 commit comments

Comments
 (0)