Skip to content

Commit be9169f

Browse files
committed
fix tests
1 parent eebc4fb commit be9169f

8 files changed

Lines changed: 40 additions & 284 deletions

File tree

baseline.xml

Lines changed: 10 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@
5454
<InvalidOperand>
5555
<code><![CDATA[$guessers]]></code>
5656
</InvalidOperand>
57+
<MixedReturnStatement>
58+
<code><![CDATA[(new ReflectionClass($class))->newLazyProxy(
59+
function () use ($metadata, $data): object {
60+
return $this->doHydrate($metadata, $data);
61+
},
62+
)]]></code>
63+
</MixedReturnStatement>
5764
<PossiblyInvalidArgument>
5865
<code><![CDATA[[
5966
...$guessers,
@@ -67,21 +74,7 @@
6774
<code><![CDATA[$result[$field]]]></code>
6875
</MixedAssignment>
6976
</file>
70-
<file src="src/Normalizer/EnumNormalizer.php">
71-
<DeprecatedClass>
72-
<code><![CDATA[ReflectionTypeUtil::classStringInstanceOf($reflectionType, BackedEnum::class)]]></code>
73-
</DeprecatedClass>
74-
<DeprecatedInterface>
75-
<code><![CDATA[EnumNormalizer]]></code>
76-
</DeprecatedInterface>
77-
</file>
7877
<file src="src/Normalizer/ObjectNormalizer.php">
79-
<DeprecatedClass>
80-
<code><![CDATA[ReflectionTypeUtil::classString($reflectionType)]]></code>
81-
</DeprecatedClass>
82-
<DeprecatedInterface>
83-
<code><![CDATA[ObjectNormalizer]]></code>
84-
</DeprecatedInterface>
8578
<MixedArgument>
8679
<code><![CDATA[$value]]></code>
8780
</MixedArgument>
@@ -121,15 +114,9 @@
121114
</InvalidArgument>
122115
</file>
123116
<file src="tests/Unit/Fixture/IdNormalizer.php">
124-
<DeprecatedClass>
125-
<code><![CDATA[ReflectionTypeUtil::classStringInstanceOf(
126-
$reflectionType,
127-
Id::class,
128-
)]]></code>
129-
</DeprecatedClass>
130-
<DeprecatedInterface>
131-
<code><![CDATA[IdNormalizer]]></code>
132-
</DeprecatedInterface>
117+
<PropertyTypeCoercion>
118+
<code><![CDATA[$type->getClassName()]]></code>
119+
</PropertyTypeCoercion>
133120
</file>
134121
<file src="tests/Unit/Metadata/AttributeMetadataFactoryTest.php">
135122
<ArgumentTypeCoercion>
@@ -163,33 +150,4 @@
163150
<code><![CDATA[['foo' => $normalizer]]]></code>
164151
</InvalidArgument>
165152
</file>
166-
<file src="tests/Unit/Normalizer/ReflectionTypeUtilTest.php">
167-
<DeprecatedClass>
168-
<code><![CDATA[ReflectionTypeUtil::classString($this->reflectionType($object, 'notAObject'))]]></code>
169-
<code><![CDATA[ReflectionTypeUtil::classString($this->reflectionType($object, 'object'))]]></code>
170-
<code><![CDATA[ReflectionTypeUtil::classString($this->reflectionType($object, 'objectNullable'))]]></code>
171-
<code><![CDATA[ReflectionTypeUtil::classString($this->reflectionType($object, 'objectUnionNullable'))]]></code>
172-
<code><![CDATA[ReflectionTypeUtil::classStringInstanceOf(
173-
$this->reflectionType($object, 'object'),
174-
ProfileCreated::class,
175-
)]]></code>
176-
<code><![CDATA[ReflectionTypeUtil::classStringInstanceOf(
177-
$this->reflectionType($object, 'objectNullable'),
178-
ProfileCreated::class,
179-
)]]></code>
180-
<code><![CDATA[ReflectionTypeUtil::classStringInstanceOf(
181-
$this->reflectionType($object, 'objectUnionNullable'),
182-
ProfileCreated::class,
183-
)]]></code>
184-
<code><![CDATA[ReflectionTypeUtil::classStringInstanceOf(
185-
$this->reflectionType($object, 'object'),
186-
ChildDto::class,
187-
)]]></code>
188-
<code><![CDATA[ReflectionTypeUtil::type($this->reflectionType($object, 'intersection'))]]></code>
189-
<code><![CDATA[ReflectionTypeUtil::type($this->reflectionType($object, 'nullableString'))]]></code>
190-
<code><![CDATA[ReflectionTypeUtil::type($this->reflectionType($object, 'string'))]]></code>
191-
<code><![CDATA[ReflectionTypeUtil::type($this->reflectionType($object, 'union'))]]></code>
192-
<code><![CDATA[ReflectionTypeUtil::type($this->reflectionType($object, 'unionNullableString'))]]></code>
193-
</DeprecatedClass>
194-
</file>
195153
</files>

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,3 @@ parameters:
101101
identifier: assign.propertyType
102102
count: 1
103103
path: src/Normalizer/ObjectNormalizer.php
104-
105-
-
106-
message: '#^Method Patchlevel\\Hydrator\\Normalizer\\ReflectionTypeUtil\:\:classStringInstanceOf\(\) should return class\-string\<T\> but returns class\-string\<T\>\|\(string&T\)\.$#'
107-
identifier: return.type
108-
count: 1
109-
path: src/Normalizer/ReflectionTypeUtil.php

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ parameters:
55
level: max
66
paths:
77
- src
8+
reportUnmatchedIgnoredErrors: false

src/Normalizer/InvalidType.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
use InvalidArgumentException;
88
use Patchlevel\Hydrator\HydratorException;
9+
use Symfony\Component\TypeInfo\Type;
910

1011
use function sprintf;
1112

1213
final class InvalidType extends InvalidArgumentException implements HydratorException
1314
{
14-
public static function unsupportedType(string $expectedType, string $name): self
15+
public static function unsupportedType(Type $expectedType, Type $name): self
1516
{
1617
return new self(sprintf('Unsupported type "%s", expected "%s".', $name, $expectedType));
1718
}

tests/Unit/Fixture/IdNormalizer.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
use Patchlevel\Hydrator\Normalizer\InvalidArgument;
99
use Patchlevel\Hydrator\Normalizer\InvalidType;
1010
use Patchlevel\Hydrator\Normalizer\Normalizer;
11-
use Patchlevel\Hydrator\Normalizer\ReflectionTypeAwareNormalizer;
12-
use Patchlevel\Hydrator\Normalizer\ReflectionTypeUtil;
13-
use ReflectionType;
11+
use Patchlevel\Hydrator\Normalizer\TypeAwareNormalizer;
12+
use Symfony\Component\TypeInfo\Type;
13+
use Symfony\Component\TypeInfo\Type\NullableType;
14+
use Symfony\Component\TypeInfo\Type\ObjectType;
1415

1516
use function is_string;
1617

1718
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
18-
final class IdNormalizer implements Normalizer, ReflectionTypeAwareNormalizer
19+
final class IdNormalizer implements Normalizer, TypeAwareNormalizer
1920
{
2021
public function __construct(
2122
/** @var class-string<Id>|null */
@@ -53,16 +54,24 @@ public function denormalize(mixed $value): Id|null
5354
return $class::fromString($value);
5455
}
5556

56-
public function handleReflectionType(ReflectionType|null $reflectionType): void
57+
public function handleType(Type|null $type): void
5758
{
58-
if ($this->idClass !== null || $reflectionType === null) {
59+
if ($this->idClass !== null || $type === null) {
5960
return;
6061
}
6162

62-
$this->idClass = ReflectionTypeUtil::classStringInstanceOf(
63-
$reflectionType,
64-
Id::class,
65-
);
63+
if ($type instanceof NullableType) {
64+
$type = $type->getWrappedType();
65+
}
66+
67+
if (!$type instanceof ObjectType) {
68+
throw InvalidType::unsupportedType(
69+
new ObjectType(Id::class),
70+
$type,
71+
);
72+
}
73+
74+
$this->idClass = $type->getClassName();
6675
}
6776

6877
/** @return class-string<Id> */

tests/Unit/Normalizer/EnumNormalizerTest.php

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@
99
use Patchlevel\Hydrator\Normalizer\InvalidArgument;
1010
use Patchlevel\Hydrator\Normalizer\InvalidType;
1111
use Patchlevel\Hydrator\Tests\Unit\Fixture\AnotherEnum;
12-
use Patchlevel\Hydrator\Tests\Unit\Fixture\AutoTypeDto;
1312
use Patchlevel\Hydrator\Tests\Unit\Fixture\Status;
1413
use PHPUnit\Framework\TestCase;
15-
use ReflectionClass;
16-
use ReflectionType;
17-
use RuntimeException;
14+
use Symfony\Component\TypeInfo\Type;
1815

1916
#[Attribute(Attribute::TARGET_PROPERTY)]
2017
final class EnumNormalizerTest extends TestCase
@@ -65,15 +62,15 @@ public function testDenormalizeWithValue(): void
6562
public function testAutoDetect(): void
6663
{
6764
$normalizer = new EnumNormalizer();
68-
$normalizer->handleReflectionType($this->reflectionType(AutoTypeDto::class, 'status'));
65+
$normalizer->handleType(Type::enum(Status::class));
6966

7067
self::assertEquals(Status::class, $normalizer->getEnum());
7168
}
7269

7370
public function testAutoDetectOverrideNotPossible(): void
7471
{
7572
$normalizer = new EnumNormalizer(AnotherEnum::class);
76-
$normalizer->handleReflectionType($this->reflectionType(AutoTypeDto::class, 'status'));
73+
$normalizer->handleType(Type::enum(Status::class));
7774

7875
self::assertEquals(AnotherEnum::class, $normalizer->getEnum());
7976
}
@@ -91,23 +88,8 @@ public function testAutoDetectMissingTypeBecauseNull(): void
9188
$this->expectException(InvalidType::class);
9289

9390
$normalizer = new EnumNormalizer();
94-
$normalizer->handleReflectionType(null);
91+
$normalizer->handleType(null);
9592

9693
$normalizer->getEnum();
9794
}
98-
99-
/** @param class-string $class */
100-
private function reflectionType(string $class, string $property): ReflectionType
101-
{
102-
$reflection = new ReflectionClass($class);
103-
$property = $reflection->getProperty($property);
104-
105-
$type = $property->getType();
106-
107-
if (!$type instanceof ReflectionType) {
108-
throw new RuntimeException('no type');
109-
}
110-
111-
return $type;
112-
}
11395
}

tests/Unit/Normalizer/ObjectNormalizerTest.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileCreated;
1616
use Patchlevel\Hydrator\Tests\Unit\Fixture\ProfileId;
1717
use PHPUnit\Framework\TestCase;
18-
use ReflectionClass;
19-
use ReflectionType;
20-
use RuntimeException;
18+
use Symfony\Component\TypeInfo\Type;
2119

2220
use function serialize;
2321
use function unserialize;
@@ -133,7 +131,7 @@ public function testAutoDetect(): void
133131

134132
$normalizer = new ObjectNormalizer();
135133
$normalizer->setHydrator($hydrator);
136-
$normalizer->handleReflectionType($this->reflectionType(AutoTypeDto::class, 'profileCreated'));
134+
$normalizer->handleType(Type::object(ProfileCreated::class));
137135

138136
self::assertEquals(ProfileCreated::class, $normalizer->getClassName());
139137
}
@@ -144,7 +142,7 @@ public function testAutoDetectOverrideNotPossible(): void
144142

145143
$normalizer = new ObjectNormalizer(AutoTypeDto::class);
146144
$normalizer->setHydrator($hydrator);
147-
$normalizer->handleReflectionType($this->reflectionType(AutoTypeDto::class, 'profileCreated'));
145+
$normalizer->handleType(Type::object(ProfileCreated::class));
148146

149147
self::assertEquals(AutoTypeDto::class, $normalizer->getClassName());
150148
}
@@ -169,7 +167,7 @@ public function testAutoDetectMissingTypeBecauseNull(): void
169167

170168
$normalizer = new ObjectNormalizer();
171169
$normalizer->setHydrator($hydrator);
172-
$normalizer->handleReflectionType(null);
170+
$normalizer->handleType(null);
173171

174172
$normalizer->getClassName();
175173
}
@@ -188,19 +186,4 @@ public function testSerialize(): void
188186
self::assertInstanceOf(ObjectNormalizer::class, $normalizer2);
189187
self::assertEquals(new ObjectNormalizer(ProfileCreated::class), $normalizer2);
190188
}
191-
192-
/** @param class-string $class */
193-
private function reflectionType(string $class, string $property): ReflectionType
194-
{
195-
$reflection = new ReflectionClass($class);
196-
$property = $reflection->getProperty($property);
197-
198-
$type = $property->getType();
199-
200-
if (!$type instanceof ReflectionType) {
201-
throw new RuntimeException('no type');
202-
}
203-
204-
return $type;
205-
}
206189
}

0 commit comments

Comments
 (0)