Skip to content

Commit b042caa

Browse files
committed
Stop PHPDoc arrays with specified key type being converted to iterables when the native type is iterable
1 parent 207639f commit b042caa

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

phpstan-baseline.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ parameters:
18121812
-
18131813
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantArrayType is error\-prone and deprecated\. Use Type\:\:getConstantArrays\(\) instead\.$#'
18141814
identifier: phpstanApi.instanceofType
1815-
count: 3
1815+
count: 1
18161816
path: src/Type/TypehintHelper.php
18171817

18181818
-

src/Type/TypehintHelper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static function decideType(
101101
if ($phpDocType instanceof UnionType) {
102102
$innerTypes = [];
103103
foreach ($phpDocType->getTypes() as $innerType) {
104-
if ($innerType instanceof ArrayType || $innerType instanceof ConstantArrayType) {
104+
if ($innerType instanceof ArrayType && $innerType->getKeyType()->describe(VerbosityLevel::typeOnly()) === 'mixed') {
105105
$innerTypes[] = new IterableType(
106106
$innerType->getIterableKeyType(),
107107
$innerType->getItemType(),
@@ -111,7 +111,7 @@ public static function decideType(
111111
}
112112
}
113113
$phpDocType = new UnionType($innerTypes);
114-
} elseif ($phpDocType instanceof ArrayType || $phpDocType instanceof ConstantArrayType) {
114+
} elseif ($phpDocType instanceof ArrayType && $phpDocType->getKeyType()->describe(VerbosityLevel::typeOnly()) === 'mixed') {
115115
$phpDocType = new IterableType(
116116
$phpDocType->getKeyType(),
117117
$phpDocType->getItemType(),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug13411;
4+
5+
use SplFixedArray;
6+
use function PHPStan\Testing\assertType;
7+
8+
class Token {}
9+
10+
/** @extends SplFixedArray<Token> */
11+
class Tokens extends SplFixedArray {}
12+
13+
/** @param array<int, Token>|Tokens $tokens */
14+
function x(iterable $tokens): int {
15+
assertType('array<int, Bug13411\\Token>|Bug13411\\Tokens', $tokens);
16+
return count($tokens);
17+
}

0 commit comments

Comments
 (0)