Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
use PHPStan\Type\Accessory\AccessoryNumericStringType;
use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
use PHPStan\Type\Accessory\HasOffsetValueType;
use PHPStan\Type\Accessory\NonEmptyArrayType;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BenevolentUnionType;
Expand Down Expand Up @@ -1078,6 +1079,16 @@ public function getPlusType(Expr $left, Expr $right, callable $getTypeCallback):
TypeCombinator::union($leftType->getIterableValueType(), $rightType->getIterableValueType()),
);

foreach ($leftType->getConstantArrays() as $type) {
foreach ($type->getKeyTypes() as $i => $offsetType) {
if ($type->isOptionalKey($i)) {
continue;
}
$valueType = $type->getValueTypes()[$i];
$arrayType = TypeCombinator::intersect($arrayType, new HasOffsetValueType($offsetType, $valueType));
}
}

if ($leftType->isIterableAtLeastOnce()->yes() || $rightType->isIterableAtLeastOnce()->yes()) {
$arrayType = TypeCombinator::intersect($arrayType, new NonEmptyArrayType());
}
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1516,6 +1516,12 @@ public function testBug13310(): void
$this->assertNoErrors($errors);
}

public function testBug11912(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-11912.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
14 changes: 14 additions & 0 deletions tests/PHPStan/Analyser/data/bug-11912.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Bug11912;

/**
* @param array<string, mixed> $results
* @param list<string> $names
*/
function appendResults(array $results, array $names): bool {
// Make sure 'names' comes first in array
$results = ['names' => $names] + $results;
\PHPStan\Testing\assertType("list<string>", $results['names']);
return true;
}
Loading