Skip to content

Commit 13db1a0

Browse files
minor symfony#61279 [Serializer] Allows to instantiate property when creating a NotNormalizableValueException (VincentLanglet)
This PR was merged into the 7.4 branch. Discussion ---------- [Serializer] Allows to instantiate property when creating a `NotNormalizableValueException` | Q | A | ------------- | --- | Branch? | 7.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | Fix #... <!-- prefix each issue number with "Fix #"; no need to create an issue if none exists, explain below --> | License | MIT Currently when writing `throw new NotNormalizableValueException(...)` we cannot set all the existing property of the NotNormalizableValueException. For instance, we cannot set the property path while it would be really useful. Commits ------- 8540f41 [Serializer] Allows to instantiate property when creating a `NotNormalizableValueException`
2 parents a6f969d + 8540f41 commit 13db1a0

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/Symfony/Component/Serializer/Exception/NotNormalizableValueException.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@ class NotNormalizableValueException extends UnexpectedValueException
2121
private ?string $path = null;
2222
private bool $useMessageForUser = false;
2323

24+
/**
25+
* @param list<string|\Stringable>|null $expectedTypes
26+
*/
27+
public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null, ?string $currentType = null, ?array $expectedTypes = null, ?string $path = null, bool $useMessageForUser = false)
28+
{
29+
parent::__construct($message, $code, $previous);
30+
31+
$this->currentType = $currentType;
32+
$this->expectedTypes = $expectedTypes ? array_map(strval(...), $expectedTypes) : $expectedTypes;
33+
$this->path = $path;
34+
$this->useMessageForUser = $useMessageForUser;
35+
}
36+
2437
/**
2538
* @param list<string|\Stringable> $expectedTypes
2639
* @param bool $useMessageForUser If the message passed to this exception is something that can be shown
@@ -29,14 +42,7 @@ class NotNormalizableValueException extends UnexpectedValueException
2942
*/
3043
public static function createForUnexpectedDataType(string $message, mixed $data, array $expectedTypes, ?string $path = null, bool $useMessageForUser = false, int $code = 0, ?\Throwable $previous = null): self
3144
{
32-
$self = new self($message, $code, $previous);
33-
34-
$self->currentType = get_debug_type($data);
35-
$self->expectedTypes = array_map(strval(...), $expectedTypes);
36-
$self->path = $path;
37-
$self->useMessageForUser = $useMessageForUser;
38-
39-
return $self;
45+
return new self($message, $code, $previous, get_debug_type($data), $expectedTypes, $path, $useMessageForUser);
4046
}
4147

4248
public function getCurrentType(): ?string

0 commit comments

Comments
 (0)