Skip to content
Merged
Show file tree
Hide file tree
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
30 changes: 16 additions & 14 deletions src/Normalizer/AbsolutePercentValueNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@

use AssoConnect\AbsolutePercentValueBundle\Object\AbsolutePercentValue;
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Throwable;

/**
* Normalizes an instance of {@see AbsolutePercentValue} to an array ['type' => ..., 'value' => ...].
Expand All @@ -17,23 +18,23 @@
class AbsolutePercentValueNormalizer implements NormalizerInterface, DenormalizerInterface
{
/**
* @param mixed $object
* @param mixed $data
* @param mixed[] $context
* @return mixed[]
* @throws InvalidArgumentException
*/
public function normalize(mixed $object, ?string $format = null, array $context = []): array
public function normalize(mixed $data, ?string $format = null, array $context = []): array
{
if (!$object instanceof AbsolutePercentValue) {
if (!$data instanceof AbsolutePercentValue) {
throw new InvalidArgumentException(sprintf(
'The object must be an instance of "%s".',
AbsolutePercentValue::class
));
}

return [
'type' => $object->getType(),
'value' => $object->getValue()
'type' => $data->getType(),
'value' => $data->getValue()
];
}

Expand All @@ -47,21 +48,22 @@
}

/**
* @throws NotNormalizableValueException
* @inheritDoc
*/
public function denormalize(

Check failure on line 53 in src/Normalizer/AbsolutePercentValueNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.3, lowest)

Method AssoConnect\AbsolutePercentValueBundle\Normalizer\AbsolutePercentValueNormalizer::denormalize() has parameter $context with no value type specified in iterable type array.

Check failure on line 53 in src/Normalizer/AbsolutePercentValueNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.4, highest)

Return type (AssoConnect\AbsolutePercentValueBundle\Object\AbsolutePercentValue) of method AssoConnect\AbsolutePercentValueBundle\Normalizer\AbsolutePercentValueNormalizer::denormalize() should be covariant with return type (($type is class-string<object> ? object : mixed)) of method Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()

Check failure on line 53 in src/Normalizer/AbsolutePercentValueNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.3, highest)

Return type (AssoConnect\AbsolutePercentValueBundle\Object\AbsolutePercentValue) of method AssoConnect\AbsolutePercentValueBundle\Normalizer\AbsolutePercentValueNormalizer::denormalize() should be covariant with return type (($type is class-string<object> ? object : mixed)) of method Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()

Check failure on line 53 in src/Normalizer/AbsolutePercentValueNormalizer.php

View workflow job for this annotation

GitHub Actions / build (8.4, lowest)

Method AssoConnect\AbsolutePercentValueBundle\Normalizer\AbsolutePercentValueNormalizer::denormalize() has parameter $context with no value type specified in iterable type array.
mixed $data,
string $type,
?string $format = null,
array $context = []
): mixed {
): AbsolutePercentValue {
if ('' === $data || null === $data) {
throw new UnexpectedValueException();
}

try {
if ('' === $data || null === $data) {
throw new NotNormalizableValueException();
}
return new AbsolutePercentValue($data['type'], $data['value']);
} catch (\Exception $e) {
throw new NotNormalizableValueException($e->getMessage(), $e->getCode(), $e);
} catch (Throwable $e) {
throw new UnexpectedValueException(previous: $e);
}
}

Expand All @@ -83,6 +85,6 @@
*/
public function getSupportedTypes(?string $format): array
{
return [AbsolutePercentValue::class => false];
return [AbsolutePercentValue::class => true];
}
}
9 changes: 5 additions & 4 deletions tests/Normalizer/AbsolutePercentValueNormalizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use AssoConnect\AbsolutePercentValueBundle\Object\AbsolutePercentValue;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;

class AbsolutePercentValueNormalizerTest extends TestCase
{
Expand All @@ -26,12 +27,12 @@ public function providerSupportsNormalization(): iterable
}

/**
* @param mixed $object
* @param mixed $data
* @dataProvider providerSupportsNormalization
*/
public function testSupportsNormalization($object, bool $result): void
public function testSupportsNormalization($data, bool $result): void
{
self::assertSame($result, $this->valueNormalizer->supportsNormalization($object));
self::assertSame($result, $this->valueNormalizer->supportsNormalization($data));
}

public function testNormalize(): void
Expand Down Expand Up @@ -67,7 +68,7 @@ public function testSupportsDenormalization(string $type, bool $result): void
*/
public function testDenormalizeFailure(mixed $data): void
{
$this->expectException(NotNormalizableValueException::class);
$this->expectException(UnexpectedValueException::class);
$this->valueNormalizer->denormalize($data, AbsolutePercentValue::class);
}

Expand Down
Loading