Skip to content

Commit 431c02d

Browse files
authored
Merge pull request #224 from mspirkov/fix-array-shapes
Fix handling of array shapes without keys
2 parents 7397f0f + 7d6c1b1 commit 431c02d

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# IDE Shizzle; it is recommended to use a global .gitignore for this but since this is an OSS project we want to make
22
# it easy to contribute
33
.idea
4+
.vscode
45
/nbproject/private/
56
.buildpath
67
.project

src/PseudoTypes/ShapeItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function isOptional(): bool
4242

4343
public function __toString(): string
4444
{
45-
if ($this->key !== null) {
45+
if ($this->key !== null && $this->key !== '') {
4646
return sprintf(
4747
'%s%s: %s',
4848
$this->key,

src/TypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public function createType(?TypeNode $type, Context $context): Type
255255
...array_map(
256256
function (ArrayShapeItemNode $item) use ($context): ArrayShapeItem {
257257
return new ArrayShapeItem(
258-
(string) $item->keyName,
258+
$item->keyName !== null ? (string) $item->keyName : null,
259259
$this->createType($item->valueType, $context),
260260
$item->optional
261261
);

tests/unit/PseudoTypes/ArrayShapeTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,43 @@ public function testExposeItems(): void
2323

2424
$this->assertSame([$item1, $item2], $arrayShape->getItems());
2525
}
26+
27+
/**
28+
* @dataProvider provideToStringData
29+
* @covers ::__toString
30+
*/
31+
public function testToString(string $expectedResult, ArrayShape $arrayShape): void
32+
{
33+
$this->assertSame($expectedResult, (string) $arrayShape);
34+
}
35+
36+
/**
37+
* @return array<string, array{string, ArrayShape}>
38+
*/
39+
public static function provideToStringData(): array
40+
{
41+
return [
42+
'with keys' => [
43+
'array{foo: true, bar?: false}',
44+
new ArrayShape(
45+
new ArrayShapeItem('foo', new True_(), false),
46+
new ArrayShapeItem('bar', new False_(), true)
47+
),
48+
],
49+
'with empty keys' => [
50+
'array{true, false}',
51+
new ArrayShape(
52+
new ArrayShapeItem('', new True_(), false),
53+
new ArrayShapeItem('', new False_(), false)
54+
),
55+
],
56+
'without keys' => [
57+
'array{true, false}',
58+
new ArrayShape(
59+
new ArrayShapeItem(null, new True_(), false),
60+
new ArrayShapeItem(null, new False_(), false)
61+
),
62+
],
63+
];
64+
}
2665
}

tests/unit/TypeResolverTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,13 @@ public function shapeStructures(): array
11501150
new ArrayShapeItem('bar', new Integer(), false)
11511151
),
11521152
],
1153+
[
1154+
'array{string, int}',
1155+
new ArrayShape(
1156+
new ArrayShapeItem(null, new String_(), false),
1157+
new ArrayShapeItem(null, new Integer(), false)
1158+
),
1159+
],
11531160
[
11541161
'array{foo?: string, bar: int}',
11551162
new ArrayShape(

0 commit comments

Comments
 (0)