Skip to content

Commit 21e4d12

Browse files
authored
Merge pull request #170 from patchlevel/add-type-in-property-metadata
2 parents 62ff552 + 0eb7670 commit 21e4d12

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

phpstan-baseline.neon

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,13 @@ parameters:
175175
path: tests/Unit/Fixture/DtoWithHooks.php
176176

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

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

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

201201
-
202-
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:482\:\:preExtract\(\) is unused\.$#'
202+
message: '#^Static method class@anonymous/tests/Unit/Metadata/AttributeMetadataFactoryTest\.php\:505\:\:preExtract\(\) is unused\.$#'
203203
identifier: method.unused
204204
count: 1
205205
path: tests/Unit/Metadata/AttributeMetadataFactoryTest.php

src/Metadata/AttributeMetadataFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,14 @@ private function getPropertyMetadataList(ReflectionClass $reflectionClass): arra
155155
);
156156
}
157157

158+
$type = $this->typeResolver->resolve($reflectionProperty);
159+
158160
$properties[$fieldName] = new PropertyMetadata(
159161
$reflectionProperty,
160162
$fieldName,
161-
$this->getNormalizer($reflectionProperty),
163+
$this->getNormalizer($reflectionProperty, $type),
162164
...$this->getPersonalData($reflectionProperty),
165+
type: $type,
163166
);
164167
}
165168

@@ -352,18 +355,16 @@ private function validate(ClassMetadata $metadata): void
352355
}
353356
}
354357

355-
private function getNormalizer(ReflectionProperty $reflectionProperty): Normalizer|null
358+
private function getNormalizer(ReflectionProperty $reflectionProperty, Type $type): Normalizer|null
356359
{
357360
$normalizer = $this->findNormalizerOnProperty($reflectionProperty);
358-
$type = null;
359361

360362
if (!$normalizer) {
361-
$type = $this->typeResolver->resolve($reflectionProperty);
362363
$normalizer = $this->inferNormalizerByType($type);
363364
}
364365

365366
if ($normalizer instanceof TypeAwareNormalizer) {
366-
$normalizer->handleType($type ?? $this->typeResolver->resolve($reflectionProperty));
367+
$normalizer->handleType($this->typeResolver->resolve($reflectionProperty));
367368
}
368369

369370
if ($normalizer instanceof ReflectionTypeAwareNormalizer) {

src/Metadata/PropertyMetadata.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use InvalidArgumentException;
99
use Patchlevel\Hydrator\Normalizer\Normalizer;
1010
use ReflectionProperty;
11+
use Symfony\Component\TypeInfo\Type;
1112

1213
use function str_starts_with;
1314

@@ -40,6 +41,7 @@ public function __construct(
4041
public readonly mixed $personalDataFallback = null,
4142
public readonly mixed $personalDataFallbackCallable = null,
4243
public array $extras = [],
44+
public readonly Type|null $type = null,
4345
) {
4446
$this->propertyName = $reflection->getName();
4547

tests/Unit/Metadata/AttributeMetadataFactoryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
3636
use Patchlevel\Hydrator\Tests\Unit\Fixture\Wrapper;
3737
use PHPUnit\Framework\TestCase;
38+
use Symfony\Component\TypeInfo\Type;
3839

3940
final class AttributeMetadataFactoryTest extends TestCase
4041
{
@@ -91,6 +92,7 @@ public function testWithProperties(): void
9192

9293
self::assertSame('name', $propertyMetadata->propertyName());
9394
self::assertSame('name', $propertyMetadata->fieldName());
95+
self::assertEquals(Type::nullable(Type::string()), $propertyMetadata->type);
9496
self::assertNull($propertyMetadata->normalizer());
9597
}
9698

@@ -136,6 +138,7 @@ public function __construct(
136138

137139
self::assertSame('name', $propertyMetadata->propertyName());
138140
self::assertSame('name', $propertyMetadata->fieldName());
141+
self::assertEquals(Type::string(), $propertyMetadata->type);
139142
self::assertNull($propertyMetadata->normalizer());
140143
}
141144

@@ -184,6 +187,7 @@ public function __construct(
184187

185188
self::assertSame('email', $propertyMetadata->propertyName());
186189
self::assertSame('email', $propertyMetadata->fieldName());
190+
self::assertEquals(Type::object(Email::class), $propertyMetadata->type);
187191
self::assertInstanceOf(EmailNormalizer::class, $propertyMetadata->normalizer());
188192
}
189193

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

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

212217
$normalizer = $propertyMetadata->normalizer();
213218

@@ -245,6 +250,10 @@ public function testInferNormalizerWithGeneric(): void
245250

246251
$propertyMetadata = $metadata->propertyForField('email');
247252
self::assertEquals(new ObjectNormalizer(Wrapper::class), $propertyMetadata->normalizer());
253+
self::assertEquals(
254+
Type::generic(Type::object(Wrapper::class), Type::object(Email::class)),
255+
$propertyMetadata->type,
256+
);
248257
}
249258

250259
public function testInferNormalizerWithTemplate(): void
@@ -259,9 +268,23 @@ public function testInferNormalizerWithTemplate(): void
259268

260269
$propertyMetadata = $metadata->propertyForField('object');
261270
self::assertEquals(new ObjectNormalizer(Wrapper::class), $propertyMetadata->normalizer());
271+
self::assertEquals(
272+
Type::generic(Type::object(Wrapper::class), Type::object(Email::class)),
273+
$propertyMetadata->type,
274+
);
262275

263276
$propertyMetadata = $metadata->propertyForField('scalar');
264277
self::assertEquals(new ObjectNormalizer(Wrapper::class), $propertyMetadata->normalizer());
278+
279+
self::assertEquals(
280+
Type::nullable(
281+
Type::generic(
282+
Type::object(Wrapper::class),
283+
Type::string(),
284+
),
285+
),
286+
$propertyMetadata->type,
287+
);
265288
}
266289

267290
public function testExtends(): void

0 commit comments

Comments
 (0)