8000 minor #59306 [Serializer] Document `SerializerInterface` exceptions … · symfony/symfony@cdc1101 · GitHub
[go: up one dir, main page]

Skip to content

Commit cdc1101

Browse files
minor #59306 [Serializer] Document SerializerInterface exceptions (VincentLanglet)
This PR was submitted for the 6.4 branch but it was squashed and merged into the 7.3 branch instead. Discussion ---------- [Serializer] Document `SerializerInterface` exceptions | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | not really, just PHPDoc | New feature? | no | Deprecations? | no | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT This - warns the developper that calls to SerializerInterface::serialize and deserialize might throws exception - avoid PHPStan to report useless try/catch when writing ``` try { $serializer->serialize(...); } catch (NotEncodableValueException) {} // for instance ``` SerializerInterface::serialize can throws - NotEncodableValueException - all exceptions thrown by NormalizerInterface::normalize - all exceptions thrown by EncoderInterface::encode Since it might be a lot of exception and there is no real need to list them all, I simplified with ``@throws` ExceptionInterface`. Commits ------- a6aeb65 [Serializer] Document `SerializerInterface` exceptions
2 parents 18d08ff + a6aeb65 commit cdc1101

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/Symfony/Component/Serializer/SerializerInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111

1212
namespace Symfony\Component\Serializer;
1313

14+
use Symfony\Component\Serializer\Exception\ExceptionInterface;
15+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
16+
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
17+
1418
/**
1519
* @author Jordi Boggiano <j.boggiano@seld.be>
1620
*/
@@ -20,6 +24,10 @@ interface SerializerInterface
2024
* Serializes data in the appropriate format.
2125
*
2226
* @param array<string, mixed> $context Options normalizers/encoders have access to
27+
*
28+
* @throws NotNormalizableValueException Occurs when a value cannot be normalized
29+
* @throws UnexpectedValueException Occurs when a value cannot be encoded
30+
* @throws ExceptionInterface Occurs for all the other cases of serialization-related errors
2331
*/
2432
public function serialize(mixed $data, string $format, array $context = []): string;
2533

@@ -35,6 +43,10 @@ public function serialize(mixed $data, string $format, array $context = []): str
3543
* @psalm-return (TType is class-string<TObject> ? TObject : mixed)
3644
*
3745
* @phpstan-return ($type is class-string<TObject> ? TObject : mixed)
46+
*
47+
* @throws NotNormalizableValueException Occurs when a value cannot be denormalized
48+
* @throws UnexpectedValueException Occurs when a value cannot be decoded
49+
* @throws ExceptionInterface Occurs for all the other cases of serialization-related errors
3850
*/
3951
public function deserialize(mixed $data, string $type, string $format, array $context = []): mixed;
4052
}

0 commit comments

Comments
 (0)
0