Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# IDE Shizzle; it is recommended to use a global .gitignore for this but since this is an OSS project we want to make
# it easy to contribute
.idea
.vscode
/nbproject/private/
.buildpath
.project
Expand Down
2 changes: 1 addition & 1 deletion src/PseudoTypes/ShapeItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function isOptional(): bool

public function __toString(): string
{
if ($this->key !== null) {
if ($this->key !== null && $this->key !== '') {
return sprintf(
'%s%s: %s',
$this->key,
Expand Down
2 changes: 1 addition & 1 deletion src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public function createType(?TypeNode $type, Context $context): Type
...array_map(
function (ArrayShapeItemNode $item) use ($context): ArrayShapeItem {
return new ArrayShapeItem(
(string) $item->keyName,
$item->keyName !== null ? (string) $item->keyName : null,
$this->createType($item->valueType, $context),
$item->optional
);
Expand Down
39 changes: 39 additions & 0 deletions tests/unit/PseudoTypes/ArrayShapeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,43 @@ public function testExposeItems(): void

$this->assertSame([$item1, $item2], $arrayShape->getItems());
}

/**
* @dataProvider provideToStringData
* @covers ::__toString
*/
public function testToString(string $expectedResult, ArrayShape $arrayShape): void
{
$this->assertSame($expectedResult, (string) $arrayShape);
}

/**
* @return array<string, array{string, ArrayShape}>
*/
public static function provideToStringData(): array
{
return [
'with keys' => [
'array{foo: true, bar?: false}',
new ArrayShape(
new ArrayShapeItem('foo', new True_(), false),
new ArrayShapeItem('bar', new False_(), true)
),
],
'with empty keys' => [
'array{true, false}',
new ArrayShape(
new ArrayShapeItem('', new True_(), false),
new ArrayShapeItem('', new False_(), false)
),
],
'without keys' => [
'array{true, false}',
new ArrayShape(
new ArrayShapeItem(null, new True_(), false),
new ArrayShapeItem(null, new False_(), false)
),
],
];
}
}
7 changes: 7 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1150,6 +1150,13 @@ public function shapeStructures(): array
new ArrayShapeItem('bar', new Integer(), false)
),
],
[
'array{string, int}',
new ArrayShape(
new ArrayShapeItem(null, new String_(), false),
new ArrayShapeItem(null, new Integer(), false)
),
],
[
'array{foo?: string, bar: int}',
new ArrayShape(
Expand Down