Skip to content

Commit

Permalink
fix: allow trailing comma in shaped array
Browse files Browse the repository at this point in the history
Allows the following syntax:

```php
/**
 * @var array{
 * 	   foo: string,
 *     bar: int,
 * }
 */
 ```
  • Loading branch information
romm committed Aug 30, 2022
1 parent 8c7568c commit bf445b5
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Type/Parser/Lexer/Token/ArrayToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ private function shapedArrayType(TokenStream $stream): ShapedArrayType
throw new ShapedArrayCommaMissing($elements);
}

if ($stream->next() instanceof ClosingCurlyBracketToken) {
$stream->forward();
break;
}

$optional = false;

if ($stream->next() instanceof UnknownSymbolToken) {
Expand Down
3 changes: 3 additions & 0 deletions src/Type/Parser/Lexer/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function __construct(Token ...$tokens)
$this->tokens = $tokens;
}

/** @phpstan-impure */
public function read(): Type
{
if ($this->done()) {
Expand Down Expand Up @@ -54,6 +55,7 @@ public function read(): Type
return $type;
}

/** @phpstan-impure */
public function next(): Token
{
$peek = $this->peek + 1;
Expand All @@ -65,6 +67,7 @@ public function next(): Token
return $this->tokens[$peek];
}

/** @phpstan-impure */
public function forward(): Token
{
$this->peek++;
Expand Down
15 changes: 15 additions & 0 deletions tests/Functional/Type/Parser/Lexer/NativeLexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ public function parse_valid_types_returns_valid_result_data_provider(): array
'transformed' => 'array{foo: string, bar?: int}',
'type' => ShapedArrayType::class,
],
'Shaped array with trailing comma' => [
'raw' => 'array{foo: string, bar: int,}',
'transformed' => 'array{foo: string, bar: int}',
'type' => ShapedArrayType::class,
],
'Shaped array with reserved keyword as key' => [
'raw' => 'array{string: string}',
'transformed' => 'array{string: string}',
Expand Down Expand Up @@ -693,6 +698,16 @@ public function parse_valid_types_returns_valid_result_data_provider(): array
'transformed' => 'class-string|int',
'type' => UnionType::class,
],
'Union type with shaped array' => [
'raw' => 'array{foo: string, bar: int}|string',
'transformed' => 'array{foo: string, bar: int}|string',
'type' => UnionType::class,
],
'Union type with shaped array with trailing comma' => [
'raw' => 'array{foo: string, bar: int,}|string',
'transformed' => 'array{foo: string, bar: int}|string',
'type' => UnionType::class,
],
'Union type followed by description' => [
'raw' => 'int|float lorem ipsum',
'transformed' => 'int|float',
Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/Mapping/Object/ShapedArrayValuesMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function test_values_are_mapped_properly(): void
'foo' => 'fiz',
'bar' => 42,
],
'shapedArrayOnSeveralLinesWithTrailingComma' => [
'foo' => 'fiz',
'bar' => 42,
],
'advancedShapedArray' => [
'mandatoryString' => 'bar',
1337,
Expand All @@ -55,6 +59,7 @@ public function test_values_are_mapped_properly(): void
self::assertInstanceOf(SimpleObject::class, $result->shapedArrayWithObject['foo']); // @phpstan-ignore-line
self::assertSame($source['shapedArrayWithOptionalValue'], $result->shapedArrayWithOptionalValue);
self::assertSame($source['shapedArrayOnSeveralLines'], $result->shapedArrayOnSeveralLines);
self::assertSame($source['shapedArrayOnSeveralLinesWithTrailingComma'], $result->shapedArrayOnSeveralLinesWithTrailingComma);
self::assertSame('bar', $result->advancedShapedArray['mandatoryString']);
self::assertSame(1337, $result->advancedShapedArray[0]);
self::assertSame(42.404, $result->advancedShapedArray[1]);
Expand Down Expand Up @@ -102,6 +107,14 @@ class ShapedArrayValues
*/
public array $shapedArrayOnSeveralLines;

/**
* @var array{
* foo: string,
* bar: int,
* }
*/
public array $shapedArrayOnSeveralLinesWithTrailingComma;

/** @var array{0: int, float, optionalString?: string, mandatoryString: string} */
public array $advancedShapedArray;

Expand All @@ -120,6 +133,10 @@ class ShapedArrayValuesWithConstructor extends ShapedArrayValues
* foo: string,
* bar: int
* } $shapedArrayOnSeveralLines
* @param array{
* foo: string,
* bar: int,
* } $shapedArrayOnSeveralLinesWithTrailingComma
* @param array{0: int, float, optionalString?: string, mandatoryString: string} $advancedShapedArray
* @param array{stdclass: string} $shapedArrayWithClassNameAsKey
*/
Expand All @@ -129,6 +146,7 @@ public function __construct(
array $shapedArrayWithObject,
array $shapedArrayWithOptionalValue,
array $shapedArrayOnSeveralLines,
array $shapedArrayOnSeveralLinesWithTrailingComma,
array $advancedShapedArray,
array $shapedArrayWithClassNameAsKey
) {
Expand All @@ -137,6 +155,7 @@ public function __construct(
$this->shapedArrayWithObject = $shapedArrayWithObject;
$this->shapedArrayWithOptionalValue = $shapedArrayWithOptionalValue;
$this->shapedArrayOnSeveralLines = $shapedArrayOnSeveralLines;
$this->shapedArrayOnSeveralLinesWithTrailingComma = $shapedArrayOnSeveralLinesWithTrailingComma;
$this->advancedShapedArray = $advancedShapedArray;
$this->shapedArrayWithClassNameAsKey = $shapedArrayWithClassNameAsKey;
}
Expand Down

0 comments on commit bf445b5

Please sign in to comment.