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
8 changes: 4 additions & 4 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,13 @@ parameters:
path: tests/Unit/Fixture/DtoWithHooks.php

-
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:454\:\:postHydrate\(\) is unused\.$#'
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:477\:\:postHydrate\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:454\:\:preExtract\(\) is unused\.$#'
message: '#^Method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:477\:\:preExtract\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Expand All @@ -193,13 +193,13 @@ parameters:
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:482\:\:postHydrate\(\) is unused\.$#'
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:505\:\:postHydrate\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

-
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:482\:\:preExtract\(\) is unused\.$#'
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:505\:\:preExtract\(\) is unused\.$#'
identifier: method.unused
count: 1
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Expand Down
11 changes: 6 additions & 5 deletions src/Metadata/AttributeMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,14 @@ private function getPropertyMetadataList(ReflectionClass $reflectionClass): arra
);
}

$type = $this->typeResolver->resolve($reflectionProperty);

$properties[$fieldName] = new PropertyMetadata(
$reflectionProperty,
$fieldName,
$this->getNormalizer($reflectionProperty),
$this->getNormalizer($reflectionProperty, $type),
...$this->getPersonalData($reflectionProperty),
type: $type,
);
}

Expand Down Expand Up @@ -352,18 +355,16 @@ private function validate(ClassMetadata $metadata): void
}
}

private function getNormalizer(ReflectionProperty $reflectionProperty): Normalizer|null
private function getNormalizer(ReflectionProperty $reflectionProperty, Type $type): Normalizer|null
{
$normalizer = $this->findNormalizerOnProperty($reflectionProperty);
$type = null;

if (!$normalizer) {
$type = $this->typeResolver->resolve($reflectionProperty);
$normalizer = $this->inferNormalizerByType($type);
}

if ($normalizer instanceof TypeAwareNormalizer) {
$normalizer->handleType($type ?? $this->typeResolver->resolve($reflectionProperty));
$normalizer->handleType($this->typeResolver->resolve($reflectionProperty));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the type already provided?

Suggested change
$normalizer->handleType($this->typeResolver->resolve($reflectionProperty));
$normalizer->handleType($type);

}

if ($normalizer instanceof ReflectionTypeAwareNormalizer) {
Expand Down
2 changes: 2 additions & 0 deletions src/Metadata/PropertyMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use Patchlevel\Hydrator\Normalizer\Normalizer;
use ReflectionProperty;
use Symfony\Component\TypeInfo\Type;

use function str_starts_with;

Expand Down Expand Up @@ -40,6 +41,7 @@ public function __construct(
public readonly mixed $personalDataFallback = null,
public readonly mixed $personalDataFallbackCallable = null,
public array $extras = [],
public readonly Type|null $type = null,
) {
$this->propertyName = $reflection->getName();

Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/Metadata/AttributeMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
use Patchlevel\Hydrator\Tests\Unit\Fixture\Wrapper;
use PHPUnit\Framework\TestCase;
use Symfony\Component\TypeInfo\Type;

final class AttributeMetadataFactoryTest extends TestCase
{
Expand Down Expand Up @@ -91,6 +92,7 @@ public function testWithProperties(): void

self::assertSame('name', $propertyMetadata->propertyName());
self::assertSame('name', $propertyMetadata->fieldName());
self::assertEquals(Type::nullable(Type::string()), $propertyMetadata->type);
self::assertNull($propertyMetadata->normalizer());
}

Expand Down Expand Up @@ -136,6 +138,7 @@ public function __construct(

self::assertSame('name', $propertyMetadata->propertyName());
self::assertSame('name', $propertyMetadata->fieldName());
self::assertEquals(Type::string(), $propertyMetadata->type);
self::assertNull($propertyMetadata->normalizer());
}

Expand Down Expand Up @@ -184,6 +187,7 @@ public function __construct(

self::assertSame('email', $propertyMetadata->propertyName());
self::assertSame('email', $propertyMetadata->fieldName());
self::assertEquals(Type::object(Email::class), $propertyMetadata->type);
self::assertInstanceOf(EmailNormalizer::class, $propertyMetadata->normalizer());
}

Expand All @@ -208,6 +212,7 @@ public function __construct(

self::assertSame('status', $propertyMetadata->propertyName());
self::assertSame('status', $propertyMetadata->fieldName());
self::assertEquals(Type::enum(Status::class), $propertyMetadata->type);

$normalizer = $propertyMetadata->normalizer();

Expand Down Expand Up @@ -245,6 +250,10 @@ public function testInferNormalizerWithGeneric(): void

$propertyMetadata = $metadata->propertyForField('email');
self::assertEquals(new ObjectNormalizer(Wrapper::class), $propertyMetadata->normalizer());
self::assertEquals(
Type::generic(Type::object(Wrapper::class), Type::object(Email::class)),
$propertyMetadata->type,
);
}

public function testInferNormalizerWithTemplate(): void
Expand All @@ -259,9 +268,23 @@ public function testInferNormalizerWithTemplate(): void

$propertyMetadata = $metadata->propertyForField('object');
self::assertEquals(new ObjectNormalizer(Wrapper::class), $propertyMetadata->normalizer());
self::assertEquals(
Type::generic(Type::object(Wrapper::class), Type::object(Email::class)),
$propertyMetadata->type,
);

$propertyMetadata = $metadata->propertyForField('scalar');
self::assertEquals(new ObjectNormalizer(Wrapper::class), $propertyMetadata->normalizer());

self::assertEquals(
Type::nullable(
Type::generic(
Type::object(Wrapper::class),
Type::string(),
),
),
$propertyMetadata->type,
);
}

public function testExtends(): void
Expand Down
Loading