Skip to content

Commit fa7b3c6

Browse files
phpstan-botclaude
andcommitted
Require both offsets 0 and 1 before narrowing ConstantArrayType with callable
Instead of narrowing keys 0 and 1 independently, first verify both are present — a valid callable array requires both elements. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0f240cc commit fa7b3c6

1 file changed

Lines changed: 16 additions & 8 deletions

File tree

src/Type/TypeCombinator.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,17 +1847,25 @@ private static function narrowConstantArrayWithCallable(ConstantArrayType $const
18471847
$valueTypes = $constantArray->getValueTypes();
18481848
$newValueTypes = $valueTypes;
18491849

1850+
$offset0Index = null;
1851+
$offset1Index = null;
1852+
18501853
foreach ($keyTypes as $k => $keyType) {
18511854
if ((new ConstantIntegerType(0))->isSuperTypeOf($keyType)->yes()) {
1852-
$newValueTypes[$k] = self::intersect($valueTypes[$k], new UnionType([new ClassStringType(), new ObjectWithoutClassType()]));
1853-
if ($newValueTypes[$k] instanceof NeverType) {
1854-
return new NeverType();
1855-
}
1855+
$offset0Index = $k;
18561856
} elseif ((new ConstantIntegerType(1))->isSuperTypeOf($keyType)->yes()) {
1857-
$newValueTypes[$k] = self::intersect($valueTypes[$k], new StringType());
1858-
if ($newValueTypes[$k] instanceof NeverType) {
1859-
return new NeverType();
1860-
}
1857+
$offset1Index = $k;
1858+
}
1859+
}
1860+
1861+
if ($offset0Index !== null && $offset1Index !== null) {
1862+
$newValueTypes[$offset0Index] = self::intersect($valueTypes[$offset0Index], new UnionType([new ClassStringType(), new ObjectWithoutClassType()]));
1863+
if ($newValueTypes[$offset0Index] instanceof NeverType) {
1864+
return new NeverType();
1865+
}
1866+
$newValueTypes[$offset1Index] = self::intersect($valueTypes[$offset1Index], new StringType());
1867+
if ($newValueTypes[$offset1Index] instanceof NeverType) {
1868+
return new NeverType();
18611869
}
18621870
}
18631871

0 commit comments

Comments
 (0)