Skip to content

Commit

Permalink
Merge pull request #808 from clementbirkle/main
Browse files Browse the repository at this point in the history
Fix Parsing of Optional Types Annotations in DataIterableAnnotation
  • Loading branch information
rubenvanassche authored Jul 25, 2024
2 parents c62de25 + 478c669 commit d1f9731
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Support/Annotations/DataIterableAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ protected function get(
);

preg_match_all(
"/{$kindPattern}(?<collectionClass>{$fqsenPattern})<(?:{$keyPattern}\s*?,\s*?)?(?<dataClass>{$fqsenPattern})>{$parameterPattern}/i",
"/{$kindPattern}(?<collectionClass>{$fqsenPattern})<(?:{$keyPattern}\s*?,\s*?)?(?<dataClass>{$fqsenPattern})>(?:{$typesPattern})*{$parameterPattern}/i",
$comment,
$collectionMatches,
);
Expand Down
8 changes: 8 additions & 0 deletions tests/Fakes/CollectionDataAnnotationsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* @property array<\Spatie\LaravelData\Tests\Fakes\SimpleData> $propertyQ
* @property \Spatie\LaravelData\Tests\Fakes\SimpleData[] $propertyR
* @property array<SimpleData> $propertyS
* @property \Illuminate\Support\Collection<\Spatie\LaravelData\Tests\Fakes\SimpleData>|null $propertyT
*/
class CollectionDataAnnotationsData
{
Expand Down Expand Up @@ -67,6 +68,11 @@ class CollectionDataAnnotationsData

public array $propertyS;

public ?array $propertyT;

/** @var \Illuminate\Support\Collection<\Spatie\LaravelData\Tests\Fakes\SimpleData>|null */
public ?array $propertyU;

/**
* @param \Spatie\LaravelData\Tests\Fakes\SimpleData[]|null $paramA
* @param null|\Spatie\LaravelData\Tests\Fakes\SimpleData[] $paramB
Expand All @@ -78,6 +84,7 @@ class CollectionDataAnnotationsData
* @param array<SimpleData> $paramH
* @param array<int,SimpleData> $paramJ
* @param array<int, SimpleData> $paramI
* @param \Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\SimpleData>|null $paramK
*/
public function method(
array $paramA,
Expand All @@ -89,6 +96,7 @@ public function method(
array $paramG,
array $paramJ,
array $paramI,
?array $paramK,
) {

}
Expand Down
8 changes: 8 additions & 0 deletions tests/Fakes/CollectionNonDataAnnotationsData.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* @property \Spatie\LaravelData\Tests\Fakes\Enums\DummyBackedEnum[] $propertyM
* @property array<\Spatie\LaravelData\Tests\Fakes\Enums\DummyBackedEnum> $propertyN
* @property array<DummyBackedEnum> $propertyO
* @property array<DummyBackedEnum>|null $propertyQ
*/
class CollectionNonDataAnnotationsData
{
Expand Down Expand Up @@ -58,6 +59,11 @@ class CollectionNonDataAnnotationsData
/** @var \Illuminate\Support\Collection<Error> */
public Collection $propertyP;

public array $propertyQ;

/** @var \Illuminate\Support\Collection<Error>|null */
public ?Collection $propertyR;

/**
* @param \Spatie\LaravelData\Tests\Fakes\Enums\DummyBackedEnum[]|null $paramA
* @param null|\Spatie\LaravelData\Tests\Fakes\Enums\DummyBackedEnum[] $paramB
Expand All @@ -69,6 +75,7 @@ class CollectionNonDataAnnotationsData
* @param array<DummyBackedEnum> $paramH
* @param array<int,DummyBackedEnum> $paramJ
* @param array<int, DummyBackedEnum> $paramI
* @param \Spatie\LaravelData\DataCollection<\Spatie\LaravelData\Tests\Fakes\Enums\DummyBackedEnum>|null $paramK
*/
public function method(
array $paramA,
Expand All @@ -80,6 +87,7 @@ public function method(
array $paramG,
array $paramJ,
array $paramI,
?array $paramK,
) {

}
Expand Down
14 changes: 14 additions & 0 deletions tests/Support/Annotations/DataIterableAnnotationReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ function (string $property, ?DataIterableAnnotation $expected) {
'property' => 'propertyM',
'expected' => new DataIterableAnnotation(SimpleData::class, isData: true),
];

yield 'propertyU' => [
'property' => 'propertyU',
'expected' => new DataIterableAnnotation(SimpleData::class, isData: true),
];
});

it('can get the data class for a data collection by class annotation', function () {
Expand All @@ -93,6 +98,7 @@ function (string $property, ?DataIterableAnnotation $expected) {
new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyQ'),
new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyR'),
new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyS'),
new DataIterableAnnotation(SimpleData::class, isData: true, property: 'propertyT'),
]);
});

Expand All @@ -110,6 +116,7 @@ function (string $property, ?DataIterableAnnotation $expected) {
new DataIterableAnnotation(SimpleData::class, isData: true, property: 'paramH'),
new DataIterableAnnotation(SimpleData::class, isData: true, keyType: 'int', property: 'paramJ'),
new DataIterableAnnotation(SimpleData::class, isData: true, keyType: 'int', property: 'paramI'),
new DataIterableAnnotation(SimpleData::class, isData: true, property: 'paramK'),
]);
});

Expand Down Expand Up @@ -185,6 +192,11 @@ function (string $property, ?DataIterableAnnotation $expected) {
'property' => 'propertyP',
'expected' => new DataIterableAnnotation(Error::class, isData: true),
];

yield 'propertyR' => [
'property' => 'propertyR',
'expected' => new DataIterableAnnotation(Error::class, isData: true),
];
});

it('can get the iterable class for a collection by class annotation', function () {
Expand All @@ -194,6 +206,7 @@ function (string $property, ?DataIterableAnnotation $expected) {
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, property: 'propertyM'),
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, property: 'propertyN'),
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, property: 'propertyO'),
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, property: 'propertyQ'),
]);
});

Expand All @@ -211,6 +224,7 @@ function (string $property, ?DataIterableAnnotation $expected) {
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, property: 'paramH'),
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, keyType: 'int', property: 'paramJ'),
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, keyType: 'int', property: 'paramI'),
new DataIterableAnnotation(DummyBackedEnum::class, isData: false, property: 'paramK'),
]);
});

Expand Down

0 comments on commit d1f9731

Please sign in to comment.