Skip to content

Commit 65daf7c

Browse files
phpstan-botclaude
andcommitted
Return NeverType early for constant arrays too small to be callable
When `narrowConstantArrayWithCallable` receives a ConstantArrayType with fewer than 2 keys, or without both offsets 0 and 1, return NeverType immediately instead of constructing an unchanged ConstantArrayType. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent fa7b3c6 commit 65daf7c

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/Type/TypeCombinator.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,10 @@ private static function narrowConstantArrayWithCallable(ConstantArrayType $const
18451845
{
18461846
$keyTypes = $constantArray->getKeyTypes();
18471847
$valueTypes = $constantArray->getValueTypes();
1848-
$newValueTypes = $valueTypes;
1848+
1849+
if (count($keyTypes) < 2) {
1850+
return new NeverType();
1851+
}
18491852

18501853
$offset0Index = null;
18511854
$offset1Index = null;
@@ -1858,15 +1861,18 @@ private static function narrowConstantArrayWithCallable(ConstantArrayType $const
18581861
}
18591862
}
18601863

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();
1869-
}
1864+
if ($offset0Index === null || $offset1Index === null) {
1865+
return new NeverType();
1866+
}
1867+
1868+
$newValueTypes = $valueTypes;
1869+
$newValueTypes[$offset0Index] = self::intersect($valueTypes[$offset0Index], new UnionType([new ClassStringType(), new ObjectWithoutClassType()]));
1870+
if ($newValueTypes[$offset0Index] instanceof NeverType) {
1871+
return new NeverType();
1872+
}
1873+
$newValueTypes[$offset1Index] = self::intersect($valueTypes[$offset1Index], new StringType());
1874+
if ($newValueTypes[$offset1Index] instanceof NeverType) {
1875+
return new NeverType();
18701876
}
18711877

18721878
return new ConstantArrayType(

0 commit comments

Comments
 (0)