Skip to content

Commit d23b74e

Browse files
committed
bug symfony#39735 [Serializer] Rename normalize param (VincentLanglet)
This PR was merged into the 4.4 branch. Discussion ---------- [Serializer] Rename normalize param | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT The ProblemNormalizer is the only one Normalizer which - Use a different param name `$exception` instead of the one in the interface `$object`. - Doesn't type check the param. The first point lead to an unfixable error with Psalm when extending the ProblemNormalizer - If the variable is named `$object` it does not match with the parent - If the variable is named `$exception` it does not match with the interface Commits ------- 7e6eee2 Rename normalize param
2 parents d4c70a5 + 7e6eee2 commit d23b74e

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

src/Symfony/Component/Serializer/Normalizer/ProblemNormalizer.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Serializer\Normalizer;
1313

1414
use Symfony\Component\ErrorHandler\Exception\FlattenException;
15+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1516

1617
/**
1718
* Normalizes errors according to the API Problem spec (RFC 7807).
@@ -40,20 +41,24 @@ public function __construct(bool $debug = false, array $defaultContext = [])
4041
*
4142
* @return array
4243
*/
43-
public function normalize($exception, $format = null, array $context = [])
44+
public function normalize($object, $format = null, array $context = [])
4445
{
46+
if (!$object instanceof FlattenException) {
47+
throw new InvalidArgumentException(sprintf('The object must implement "%s".', FlattenException::class));
48+
}
49+
4550
$context += $this->defaultContext;
4651
$debug = $this->debug && ($context['debug'] ?? true);
4752

4853
$data = [
4954
'type' => $context['type'],
5055
'title' => $context['title'],
51-
'status' => $context['status'] ?? $exception->getStatusCode(),
52-
'detail' => $debug ? $exception->getMessage() : $exception->getStatusText(),
56+
'status' => $context['status'] ?? $object->getStatusCode(),
57+
'detail' => $debug ? $object->getMessage() : $object->getStatusText(),
5358
];
5459
if ($debug) {
55-
$data['class'] = $exception->getClass();
56-
$data['trace'] = $exception->getTrace();
60+
$data['class'] = $object->getClass();
61+
$data['trace'] = $object->getTrace();
5762
}
5863

5964
return $data;

0 commit comments

Comments
 (0)