Skip to content

Commit

Permalink
Cast PHPDoc array key type with array key casting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 31, 2022
1 parent 64d7ac8 commit 5d17b84
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/PhpDoc/TypeNodeResolver.php
Expand Up @@ -456,7 +456,7 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na
new IntegerType(),
new StringType(),
]));
$arrayType = new ArrayType($keyType, $genericTypes[1]);
$arrayType = new ArrayType(!$keyType instanceof NeverType ? ArrayType::castToArrayKeyType($keyType) : $keyType, $genericTypes[1]);
} else {
return new ErrorType();
}
Expand Down
Expand Up @@ -382,4 +382,9 @@ public function testBug3339(): void
$this->analyse([__DIR__ . '/data/bug-3339.php'], []);
}

public function testBug6117(): void
{
$this->analyse([__DIR__ . '/data/bug-6117.php'], []);
}

}
32 changes: 32 additions & 0 deletions tests/PHPStan/Rules/Properties/data/bug-6117.php
@@ -0,0 +1,32 @@
<?php declare(strict_types = 1);

namespace Bug6117;

final class Mapping
{
}

final class Stuff
{
/**
* @var string
*/
const CATEGORY_TYPE_ONE = '44';

/**
* @var string
*/
const CATEGORY_TYPE_TWO = '45';

/**
* @var array<self::CATEGORY_*, Mapping>
*/
private $mappings = [];

public function testMe(): void
{
$this->mappings[self::CATEGORY_TYPE_TWO] = new Mapping();

$this->mappings[(string)self::CATEGORY_TYPE_TWO] = new Mapping();
}
}

0 comments on commit 5d17b84

Please sign in to comment.