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
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ parameters:
count: 1
path: src/Metadata/AttributeMetadataFactory.php

-
message: '#^Instanceof between Symfony\\Component\\TypeInfo\\Type\\ObjectType and Symfony\\Component\\TypeInfo\\Type\\ObjectType will always evaluate to true\.$#'
identifier: instanceof.alwaysTrue
count: 1
path: src/Metadata/AttributeMetadataFactory.php

-
message: '#^Parameter \#1 \$objectOrClass of class ReflectionClass constructor expects class\-string\<T of object\>\|T of object, string given\.$#'
identifier: argument.type
Expand Down
11 changes: 10 additions & 1 deletion src/Metadata/AttributeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use ReflectionProperty;
use Symfony\Component\TypeInfo\Type;
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\NullableType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\TypeResolver\TypeResolver;

Expand Down Expand Up @@ -396,13 +397,21 @@ private function findNormalizer(ReflectionProperty $reflectionProperty, Type $ty
return $attributeReflectionList[0]->newInstance();
}

if ($type instanceof NullableType) {
$type = $type->getWrappedType();
}

if ($type instanceof ObjectType) {
return $this->findNormalizerOnClass(new ReflectionClass($type->getClassName()));
}

if ($type instanceof CollectionType && $type->getCollectionValueType() instanceof ObjectType) {
if ($type instanceof CollectionType) {
$valueType = $type->getCollectionValueType();

if ($valueType instanceof NullableType) {
$valueType = $type->getWrappedType();
}

if (!$valueType instanceof ObjectType) {
return null;
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Fixture/InferNormalizerWithNullableDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function __construct(
public DateTimeImmutable|null $dateTimeImmutable,
public DateTime|null $dateTime = null,
public DateTimeZone|null $dateTimeZone = null,
public ProfileId|null $profileId = null,
) {
}
}
22 changes: 22 additions & 0 deletions tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Patchlevel\Hydrator\Tests\Unit\Fixture\MissingSubjectIdDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ParentWithPersonalDataDto;
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId;
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
use PHPUnit\Framework\TestCase;

Expand Down Expand Up @@ -199,6 +200,27 @@ public function __construct(
self::assertSame(Status::class, $normalizer->getEnum());
}

public function testEventWithInferNormalizer(): void
{
$object = new class {
public function __construct(
public ProfileId|null $profileId = null,
) {
}
};

$metadataFactory = new AttributeMetadataFactory();
$metadata = $metadataFactory->metadata($object::class);

$properties = $metadata->properties();

self::assertCount(1, $properties);

$propertyMetadata = $metadata->propertyForField('profileId');

self::assertEquals(new IdNormalizer(ProfileId::class), $propertyMetadata->normalizer());
}

public function testExtends(): void
{
$metadataFactory = new AttributeMetadataFactory();
Expand Down
22 changes: 22 additions & 0 deletions tests/Unit/MetadataHydratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ public function testExtractCircularReference(): void
$this->hydrator->extract($dto1);
}

public function testExtractWithInferNormalizer2(): void
{
$result = $this->hydrator->extract(
new InferNormalizerWithNullableDto(
null,
null,
profileId: ProfileId::fromString('1'),
),
);

self::assertEquals(
[
'status' => null,
'dateTimeImmutable' => null,
'dateTime' => null,
'dateTimeZone' => null,
'profileId' => '1',
],
$result,
);
}

public function testExtractWithInferNormalizerFailed(): void
{
$this->expectException(NormalizationMissing::class);
Expand Down
Loading