Skip to content

Commit

Permalink
Work around new return type in Symfony 7 normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean85 committed Mar 15, 2024
1 parent 182490a commit 57be670
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
9 changes: 7 additions & 2 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,20 @@ parameters:
path: src/Util/RedeliveryStampExtractor.php

-
message: "#^Method Facile\\\\MongoDbMessenger\\\\Tests\\\\End2End\\\\App\\\\ObjectNormalizerDecorator\\:\\:normalize\\(\\) return type with generic class ArrayObject does not specify its types\\: TKey, TValue$#"
message: "#^Method Facile\\\\MongoDbMessenger\\\\Tests\\\\End2End\\\\App\\\\ObjectNormalizerDecorator\\:\\:doNormalize\\(\\) return type with generic class ArrayObject does not specify its types\\: TKey, TValue$#"
count: 1
path: tests/End2End/App/ObjectNormalizerDecorator.php

-
message: "#^Method Facile\\\\MongoDbMessenger\\\\Tests\\\\End2End\\\\App\\\\ObjectNormalizerDecorator\\:\\:normalize\\(\\) should return \\(ArrayObject&iterable\\)\\|bool\\|float\\|int\\|string\\|null but returns array\\|ArrayObject\\|bool\\|float\\|int\\|string\\|null\\.$#"
message: "#^Method Facile\\\\MongoDbMessenger\\\\Tests\\\\End2End\\\\App\\\\ObjectNormalizerDecorator\\:\\:doNormalize\\(\\) should return \\(ArrayObject&iterable\\)\\|bool\\|float\\|int\\|string\\|null but returns array\\|ArrayObject\\|bool\\|float\\|int\\|string\\|null\\.$#"
count: 1
path: tests/End2End/App/ObjectNormalizerDecorator.php

-
message: "#^Method Facile\\\\MongoDbMessenger\\\\Tests\\\\End2End\\\\App\\\\SuppressDeprecationNormalizer\\:\\:normalize\\(\\) return type with generic class ArrayObject does not specify its types\\: TKey, TValue$#"
count: 2
path: tests/End2End/App/SuppressDeprecationNormalizer.php

-
message: "#^Parameter \\#1 \\$id of method Facile\\\\MongoDbMessenger\\\\Transport\\\\MongoDbUnresettableTransport\\:\\:find\\(\\) expects MongoDB\\\\BSON\\\\ObjectId\\|string, mixed given\\.$#"
count: 1
Expand Down
10 changes: 6 additions & 4 deletions tests/End2End/App/ObjectNormalizerDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct()
*
* @return mixed
*/
public function denormalize($data, $type, $format = null, array $context = [])
protected function doDenormalize($data, $type, $format = null, array $context = [])
{
// inside RedeliveryStamp
$context[ObjectNormalizer::IGNORED_ATTRIBUTES][] = 'exceptionMessage';
Expand All @@ -45,8 +45,9 @@ public function denormalize($data, $type, $format = null, array $context = [])
* @param mixed $data
* @param string $type
* @param string|null $format
* @param array<string, mixed> $context
*/
public function supportsDenormalization($data, $type, $format = null): bool
public function supportsDenormalization($data, $type, $format = null, array $context = []): bool
{
return class_exists($type)
&& in_array($type, [
Expand All @@ -62,7 +63,7 @@ public function supportsDenormalization($data, $type, $format = null): bool
*
* @return mixed[]|string|int|float|bool|\ArrayObject|null
*/
public function normalize($object, $format = null, array $context = [])
protected function doNormalize($object, $format = null, array $context = [])
{
// inside RedeliveryStamp
$context[ObjectNormalizer::IGNORED_ATTRIBUTES][] = 'exceptionMessage';
Expand All @@ -74,8 +75,9 @@ public function normalize($object, $format = null, array $context = [])
/**
* @param mixed $data
* @param string|null $format
* @param array<string, mixed> $context
*/
public function supportsNormalization($data, $format = null): bool
public function supportsNormalization($data, $format = null, array $context = []): bool
{
return $data instanceof FlattenException
|| $data instanceof RedeliveryStamp
Expand Down
46 changes: 46 additions & 0 deletions tests/End2End/App/SuppressDeprecationNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ public function getSupportedTypes(?string $format): array
RedeliveryStamp::class => true,
];
}

/**
* @param mixed $data Data to restore
* @param string $type
* @param string|null $format
* @param array<string, mixed>&array{ignored_attributes?: string[]} $context
*/
public function denormalize($data, $type, $format = null, array $context = []): mixed
{
return parent::doDenormalize($data, $type, $format, $context); // TODO: Change the autogenerated stub
}

/**
* @param mixed $object
* @param string|null $format
* @param array<string, mixed>&array{ignored_attributes?: string[]} $context
*
* @return array<mixed>|string|int|float|bool|\ArrayObject|null
*/
public function normalize($object, $format = null, array $context = []): \ArrayObject|array|string|int|float|bool|null
{
return $this->doNormalize($object, $format, $context);
}
}
} else {
class SuppressDeprecationNormalizer extends ObjectNormalizerDecorator implements CacheableSupportsMethodInterface
Expand All @@ -29,5 +52,28 @@ public function hasCacheableSupportsMethod(): bool
{
return $this->objectNormalizer->hasCacheableSupportsMethod();
}

/**
* @param mixed $data Data to restore
* @param string $type
* @param string|null $format
* @param array<string, mixed>&array{ignored_attributes?: string[]} $context
*/
public function denormalize($data, $type, $format = null, array $context = [])
{
return parent::doDenormalize($data, $type, $format, $context); // TODO: Change the autogenerated stub
}

/**
* @param mixed $object
* @param string|null $format
* @param array<string, mixed>&array{ignored_attributes?: string[]} $context
*
* @return array<mixed>|string|int|float|bool|\ArrayObject|null
*/
public function normalize($object, $format = null, array $context = [])
{
return $this->doNormalize($object, $format, $context);
}
}
}

0 comments on commit 57be670

Please sign in to comment.