Skip to content

Commit 4cf4b3b

Browse files
committed
simplify object instantiation
1 parent 9212fd2 commit 4cf4b3b

File tree

8 files changed

+34
-313
lines changed

8 files changed

+34
-313
lines changed

src/Symfony/Component/SerDes/Exception/InvalidConstructorArgumentException.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Symfony/Component/SerDes/Exception/UnexpectedTypeException.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Symfony/Component/SerDes/Instantiator/InstantiatorInterface.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
namespace Symfony\Component\SerDes\Instantiator;
1313

14-
use Symfony\Component\SerDes\Exception\InvalidConstructorArgumentException;
15-
use Symfony\Component\SerDes\Exception\UnexpectedTypeException;
14+
use Symfony\Component\SerDes\Exception\UnexpectedValueException;
1615

1716
/**
1817
* @author Mathias Arlaud <[email protected]>
@@ -30,8 +29,7 @@ interface InstantiatorInterface
3029
*
3130
* @return T
3231
*
33-
* @throws InvalidConstructorArgumentException
34-
* @throws UnexpectedTypeException
32+
* @throws UnexpectedValueException
3533
*/
3634
public function __invoke(\ReflectionClass $class, array $propertiesValues, array $context): object;
3735
}

src/Symfony/Component/SerDes/Internal/Deserialize/Deserializer.php

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Symfony\Component\SerDes\Exception\LogicException;
1515
use Symfony\Component\SerDes\Exception\UnexpectedValueException;
1616
use Symfony\Component\SerDes\Exception\UnsupportedTypeException;
17-
use Symfony\Component\SerDes\Instantiator\InstantiatorInterface;
1817
use Symfony\Component\SerDes\Internal\Type;
1918
use Symfony\Component\SerDes\Internal\TypeFactory;
2019
use Symfony\Component\SerDes\Internal\UnionType;
@@ -42,7 +41,6 @@ public function __construct(
4241
private readonly DecoderInterface $decoder,
4342
private readonly ListSplitterInterface $listSplitter,
4443
private readonly DictSplitterInterface $dictSplitter,
45-
private readonly InstantiatorInterface $instantiator,
4644
) {
4745
}
4846

@@ -91,13 +89,17 @@ private function deserializeScalar(bool $lazy, mixed $resourceOrData, Type $type
9189
return null;
9290
}
9391

94-
return match ($type->name()) {
95-
'int' => (int) $data,
96-
'float' => (float) $data,
97-
'string' => (string) $data,
98-
'bool' => (bool) $data,
99-
default => throw new LogicException(sprintf('Cannot cast scalar to "%s".', $type->name())),
100-
};
92+
try {
93+
return match ($type->name()) {
94+
'int' => (int) $data,
95+
'float' => (float) $data,
96+
'string' => (string) $data,
97+
'bool' => (bool) $data,
98+
default => throw new LogicException(sprintf('Unhandled "%s" scalar cast', $type->name())),
99+
};
100+
} catch (\Throwable) {
101+
throw new UnexpectedValueException(sprintf('Cannot cast "%s" to "%s"', get_debug_type($data), (string) $type));
102+
}
101103
}
102104

103105
/**
@@ -279,6 +281,22 @@ function (string $type, array $context) use ($v, $resourceOrData, $lazy): mixed
279281
return $context['instantiator']($reflection, $propertiesValues, $context);
280282
}
281283

282-
return ($this->instantiator)($reflection, $propertiesValues, $context);
284+
$object = new ($reflection->getName())();
285+
286+
foreach ($propertiesValues as $property => $value) {
287+
try {
288+
$object->{$property} = $value();
289+
} catch (\TypeError|UnexpectedValueException $e) {
290+
$exception = new UnexpectedValueException($e->getMessage(), previous: $e);
291+
292+
if (!($context['collect_errors'] ?? false)) {
293+
throw $exception;
294+
}
295+
296+
$context['collected_errors'][] = $exception;
297+
}
298+
}
299+
300+
return $object;
283301
}
284302
}

src/Symfony/Component/SerDes/Internal/Deserialize/DeserializerFactory.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ private static function json(bool $validate): Deserializer
5353
decoder: new JsonDecoder(),
5454
listSplitter: new JsonListSplitter($lexer),
5555
dictSplitter: new JsonDictSplitter($lexer),
56-
instantiator: new Instantiator(),
5756
);
5857
}
5958
}

src/Symfony/Component/SerDes/Internal/Deserialize/Instantiator.php

Lines changed: 0 additions & 95 deletions
This file was deleted.

src/Symfony/Component/SerDes/Tests/Internal/Deserialize/DeserializeTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
namespace Symfony\Component\SerDes\Tests\Internal\Deserialize;
1313

1414
use PHPUnit\Framework\TestCase;
15-
use Symfony\Component\SerDes\Exception\InvalidConstructorArgumentException;
1615
use Symfony\Component\SerDes\Exception\InvalidResourceException;
1716
use Symfony\Component\SerDes\Exception\PartialDeserializationException;
18-
use Symfony\Component\SerDes\Exception\UnexpectedTypeException;
17+
use Symfony\Component\SerDes\Exception\UnexpectedValueException;
1918
use Symfony\Component\SerDes\Exception\UnsupportedFormatException;
2019
use Symfony\Component\SerDes\Tests\Fixtures\Dto\ClassicDummy;
21-
use Symfony\Component\SerDes\Tests\Fixtures\Dto\DummyWithConstructorWithRequiredValues;
2220
use Symfony\Component\SerDes\Tests\Fixtures\Enum\DummyBackedEnum;
2321

2422
use function Symfony\Component\SerDes\deserialize;
@@ -100,9 +98,9 @@ public function testThrowOnUnknownFormat()
10098

10199
public function testThrowWhenNotCollecting()
102100
{
103-
$this->expectException(InvalidConstructorArgumentException::class);
101+
$this->expectException(UnexpectedValueException::class);
104102

105-
$this->deserializeString('{}', DummyWithConstructorWithRequiredValues::class);
103+
$this->deserializeString('{"name": {"foo": "bar"}}', ClassicDummy::class);
106104
}
107105

108106
public function testThrowPartialWhenCollecting()
@@ -133,7 +131,7 @@ public function testThrowPartialWhenCollecting()
133131
$this->assertEquals([$okDummy, $koDummy, $okDummy, $koDummy], $e->deserialized);
134132

135133
$this->assertCount(2, $e->errors);
136-
$this->assertContainsOnlyInstancesOf(UnexpectedTypeException::class, $e->errors);
134+
$this->assertContainsOnlyInstancesOf(UnexpectedValueException::class, $e->errors);
137135
}
138136
}
139137

0 commit comments

Comments
 (0)