Skip to content

Commit 5d17b84

Browse files
committed
Cast PHPDoc array key type with array key casting rules
1 parent 64d7ac8 commit 5d17b84

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/PhpDoc/TypeNodeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na
456456
new IntegerType(),
457457
new StringType(),
458458
]));
459-
$arrayType = new ArrayType($keyType, $genericTypes[1]);
459+
$arrayType = new ArrayType(!$keyType instanceof NeverType ? ArrayType::castToArrayKeyType($keyType) : $keyType, $genericTypes[1]);
460460
} else {
461461
return new ErrorType();
462462
}

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,4 +382,9 @@ public function testBug3339(): void
382382
$this->analyse([__DIR__ . '/data/bug-3339.php'], []);
383383
}
384384

385+
public function testBug6117(): void
386+
{
387+
$this->analyse([__DIR__ . '/data/bug-6117.php'], []);
388+
}
389+
385390
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug6117;
4+
5+
final class Mapping
6+
{
7+
}
8+
9+
final class Stuff
10+
{
11+
/**
12+
* @var string
13+
*/
14+
const CATEGORY_TYPE_ONE = '44';
15+
16+
/**
17+
* @var string
18+
*/
19+
const CATEGORY_TYPE_TWO = '45';
20+
21+
/**
22+
* @var array<self::CATEGORY_*, Mapping>
23+
*/
24+
private $mappings = [];
25+
26+
public function testMe(): void
27+
{
28+
$this->mappings[self::CATEGORY_TYPE_TWO] = new Mapping();
29+
30+
$this->mappings[(string)self::CATEGORY_TYPE_TWO] = new Mapping();
31+
}
32+
}

0 commit comments

Comments
 (0)