Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 12adfbe

Browse files
rvanvelzenondrejmirtes
authored andcommitted
Support literal-string as array key
1 parent ba8af60 commit 12adfbe

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

‎src/Type/ArrayType.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,19 @@ public static function castToArrayKeyType(Type $offsetType): Type
350350
return new UnionType([new ConstantIntegerType(0), new ConstantIntegerType(1)]);
351351
}
352352

353+
if ($offsetType instanceof UnionType) {
354+
return $traverse($offsetType);
355+
}
356+
353357
if ($offsetType instanceof FloatType || $offsetType->isNumericString()->yes()) {
354358
return new IntegerType();
355359
}
356360

357-
if ($offsetTypeinstanceof StringType || $offsetType->isNonEmptyString()->yes()) {
361+
if ($offsetType->isString()->yes()) {
358362
return $offsetType;
359363
}
360364

361-
if ($offsetType instanceof UnionType || $offsetTypeinstanceofIntersectionType) {
365+
if ($offsetType instanceof IntersectionType) {
362366
return $traverse($offsetType);
363367
}
364368

‎tests/PHPStan/Analyser/NodeScopeResolverTest.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,7 @@ public function dataFileAsserts(): iterable
910910
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7391.php');
911911
yield from $this->gatherAssertTypes(__DIR__ . '/data/finally-scope.php');
912912
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7387.php');
913+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7353.php');
913914
}
914915

915916
/**
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug7353;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld
8+
{
9+
/** @param array<literal-string, mixed> $data */
10+
public function sayHello(array $data): void
11+
{
12+
assertType('array<literal-string, mixed>', $data);
13+
}
14+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /