Skip to content

[Serializer] Rename normalize param #39735

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Serializer\Normalizer;

use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;

/**
* Normalizes errors according to the API Problem spec (RFC 7807).
Expand Down Expand Up @@ -40,20 +41,24 @@ public function __construct(bool $debug = false, array $defaultContext = [])
*
* @return array
*/
public function normalize($exception, $format = null, array $context = [])
public function normalize($object, $format = null, array $context = [])
{
if (!$object instanceof FlattenException) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe could you just add a @param tag here to make Psalm happy?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove these lines if preferred ; this does not have impact psalm for developer which extends this class.
My main concern was about the name of the param.

This method is public, and the interface is allowing object ; so theorically this method should handle every possible object and not restricting the param directly in the phpdoc.

I agree that this seems duplicate with the supportNormalization method. We could consider that this is similar to Voter

* It is safe to assume that $attribute and $subject already passed the "supports()" method check.
.

But when I look at others Normalizer, it seems that checking the param was the strategy adopted

So I added consistency

throw new InvalidArgumentException(sprintf('The object must implement "%s".', FlattenException::class));
}

$context += $this->defaultContext;
$debug = $this->debug && ($context['debug'] ?? true);

$data = [
'type' => $context['type'],
'title' => $context['title'],
'status' => $context['status'] ?? $exception->getStatusCode(),
'detail' => $debug ? $exception->getMessage() : $exception->getStatusText(),
'status' => $context['status'] ?? $object->getStatusCode(),
'detail' => $debug ? $object->getMessage() : $object->getStatusText(),
];
if ($debug) {
$data['class'] = $exception->getClass();
$data['trace'] = $exception->getTrace();
$data['class'] = $object->getClass();
$data['trace'] = $object->getTrace();
}

return $data;
Expand Down