Skip to content

Commit

Permalink
Changes for symfony 6.2 upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
divya-intelli committed May 16, 2023
1 parent d9ebe72 commit 8fdd5c9
Show file tree
Hide file tree
Showing 17 changed files with 66 additions and 44 deletions.
6 changes: 5 additions & 1 deletion src/Api/ApigeeX/Normalizer/ApiProductNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public function normalize($object, $format = null, array $context = [])
{
$normalized = (array) parent::normalize($object, $format, $context);

return (object) $normalized;
//convert to ArrayObject as symfony normalizer throws error for std object.
//set ARRAY_AS_PROPS flag as we need entries to be accessed as properties.
$array_as_props = \ArrayObject::ARRAY_AS_PROPS;
$normalized = new \ArrayObject($normalized, $array_as_props);
return ($normalized);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public function normalize($object, $format = null, array $context = [])
$normalized['developer'][] = (object) ['email' => $member, 'role' => $role];
}

return (object) $normalized;
//convert to ArrayObject as symfony normalizer throws error for std object.
//set ARRAY_AS_PROPS flag as we need entries to be accessed as properties.
$array_as_props = \ArrayObject::ARRAY_AS_PROPS;
$normalized = new \ArrayObject($normalized, $array_as_props);
return ($normalized);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Api/Monetization/NameConverter/NameConverterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ abstract class NameConverterBase implements NameConverterInterface
/**
* {@inheritdoc}
*/
public function normalize($propertyName)
public function normalize($propertyName): string
{
if ($externalPropertyName = array_search($propertyName, $this->getExternalToLocalMapping())) {
return $externalPropertyName;
Expand All @@ -37,7 +37,7 @@ public function normalize($propertyName)
/**
* {@inheritdoc}
*/
public function denormalize($propertyName)
public function denormalize($propertyName): string
{
if (array_key_exists($propertyName, $this->getExternalToLocalMapping())) {
return $this->getExternalToLocalMapping()[$propertyName];
Expand Down
8 changes: 6 additions & 2 deletions src/Api/Monetization/Normalizer/ApiPackageNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,14 @@ public function normalize($object, $format = null, array $context = [])
// Do not send redundant API product information to Apigee Edge, the
// id of a referenced API product is enough.
foreach ($normalized['product'] as $id => $data) {
$normalized['product'][$id] = (object) ['id' => $data->id];
$normalized['product'][$id] = (object) ['id' => $data['id']];
}

return (object) $normalized;
//convert to ArrayObject as symfony normalizer throws error for std object.
//set ARRAY_AS_PROPS flag as we need entries to be accessed as properties.
$array_as_props = \ArrayObject::ARRAY_AS_PROPS;
$normalized = new \ArrayObject($normalized, $array_as_props);
return ($normalized);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Api/Monetization/Normalizer/EntityNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ public function normalize($object, $format = null, array $context = [])
}
}

return (object) $normalized;
//convert to ArrayObject as symfony normalizer throws error for std object.
//set ARRAY_AS_PROPS flag as we need entries to be accessed as properties.
$array_as_props = \ArrayObject::ARRAY_AS_PROPS;
$normalized = new \ArrayObject($normalized, $array_as_props);
return ($normalized);
}

/**
Expand Down
9 changes: 7 additions & 2 deletions src/Api/Monetization/Normalizer/ReportCriteriaNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,19 @@ public function normalize($object, $format = null, array $context = [])
// According to the API documentation it is always UTC.
// https://docs.apigee.com/api-platform/monetization/create-reports#createreportdefapi
$this->fixTimeZoneOnNormalization($object, $normalized, new \DateTimeZone('UTC'));

$arr_empty = [];
// Just in case, do not send empty array values either to this API.
foreach ($normalized as $property => $value) {
if (is_array($value) && empty($value)) {
unset($normalized->{$property});
//Get all the array which is empty
$arr_empty[] = $property;
}
}

foreach ($arr_empty as $val) {
unset($normalized->{$val});
}

return $normalized;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,9 @@ public static function getEntityTypeSpecificDefaultNormalizers(): array
/**
* {@inheritdoc}
*/
public function deserialize($data, $type, $format, array $context = [])
public function deserialize($data, $type, $format, array $context = []): mixed
{
$context['json_decode_associative'] = false;
// Because of the decorator pattern in the EntitySerializer we have to
// repeat this in here as well.
if ($this->supportsDecoding($format)) {
Expand Down
5 changes: 3 additions & 2 deletions src/Denormalizer/EdgeDateDenormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ public function denormalize($data, $type, $format = null, array $context = [])
}
$context[$this->normalizer::FORMAT_KEY] = 'U';
$context[$this->normalizer::TIMEZONE_KEY] = new \DateTimeZone('UTC');

return $this->normalizer->denormalize(intval($data / 1000), $type, $format, $context);
//convert data in string format for denormaliser.
$data = (string) intval($data / 1000);
return $this->normalizer->denormalize($data, $type, $format, $context);
}

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,11 @@ public function normalize($object, $format = null, array $context = [])
return !is_null($value);
});
ksort($asArray);

return (object) $asArray;
//Need to convert to ArrayObject as symfony normalizer throws error for std object.
//Need to set ARRAY_AS_PROPS flag as we need Entries to be accessed as properties.
$array_as_props = \ArrayObject::ARRAY_AS_PROPS;
$asArray = new \ArrayObject($asArray, $array_as_props);
return ($asArray);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion src/Normalizer/PropertiesPropertyNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ public function normalize($object, $format = null, array $context = [])
'property' => parent::normalize($object, $format, $context),
];

return (object) $return;
//convert to ArrayObject as symfony normalizer throws error for std object.
//set ARRAY_AS_PROPS flag as we need entries to be accessed as properties.
$array_as_props = \ArrayObject::ARRAY_AS_PROPS;
$return = new \ArrayObject($return, $array_as_props);
return ($return);
}

/**
Expand Down
31 changes: 11 additions & 20 deletions src/PropertyAccess/PropertyAccessorDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function setValue(&$objectOrArray, $propertyPath, $value): void
/**
* {@inheritdoc}
*/
public function getValue($objectOrArray, $propertyPath)
public function getValue($objectOrArray, $propertyPath): mixed
{
try {
$value = $this->propertyAccessor->getValue($objectOrArray, $propertyPath);
Expand All @@ -112,15 +112,15 @@ public function getValue($objectOrArray, $propertyPath)
/**
* {@inheritdoc}
*/
public function isWritable($objectOrArray, $propertyPath)
public function isWritable($objectOrArray, $propertyPath): bool
{
return $this->propertyAccessor->isWritable($objectOrArray, $propertyPath);
}

/**
* {@inheritdoc}
*/
public function isReadable($objectOrArray, $propertyPath)
public function isReadable($objectOrArray, $propertyPath): bool
{
return $this->propertyAccessor->isReadable($objectOrArray, $propertyPath);
}
Expand Down Expand Up @@ -174,37 +174,28 @@ private static function processTypeErrorOnGetValue($object, string $property, \T
* @param $message
* @param $trace
* @param $i
* @param $propertyPath
* @param $previous
*
* @see \Symfony\Component\PropertyAccess\PropertyAccessor::throwInvalidArgumentException()
*
* @psalm-suppress PossiblyFalseOperand
* @psalm-suppress PossiblyFalseArgument
*/
private static function processTypeErrorOnSetValue($message, $trace, $i, $previous = null): void
private static function processTypeErrorOnSetValue($message, $trace, $i, string $propertyPath, $previous = null): void
{
if (!isset($trace[$i]['file']) || __FILE__ !== $trace[$i]['file']) {
return;
}
if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
[, $expectedType, $actualType] = $matches;

if (\PHP_VERSION_ID < 80000) {
if (0 !== strpos($message, 'Argument ')) {
return;
}

$pos = strpos($message, $delim = 'must be of the type ') ?: (strpos($message, $delim = 'must be an instance of ') ?: strpos($message, $delim = 'must implement interface '));
$pos += \strlen($delim);
$j = strpos($message, ',', $pos);
$type = substr($message, 2 + $j, strpos($message, ' given', $j) - $j - 2);
$message = substr($message, $pos, $j - $pos);

throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given.', $message, 'NULL' === $type ? 'null' : $type), 0, $previous);
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
if (preg_match('/^Cannot assign (\S+) to property \S+::\$\S+ of type (\S+)$/', $message, $matches)) {
[, $actualType, $expectedType] = $matches;

if (preg_match('/^\S+::\S+\(\): Argument #\d+ \(\$\S+\) must be of type (\S+), (\S+) given/', $message, $matches)) {
list(, $expectedType, $actualType) = $matches;

throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given.', $expectedType, 'NULL' === $actualType ? 'null' : $actualType), 0, $previous);
throw new InvalidArgumentException(sprintf('Expected argument of type "%s", "%s" given at property path "%s".', $expectedType, 'NULL' === $actualType ? 'null' : $actualType, $propertyPath), 0, $previous);
}
}
}
9 changes: 5 additions & 4 deletions src/Serializer/EntitySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function supportsNormalization($data, $format = null)
/**
* {@inheritdoc}
*/
public function serialize($data, $format, array $context = [])
public function serialize($data, $format, array $context = []): string
{
if (!$this->supportsEncoding($format)) {
throw new NotEncodableValueException(sprintf('Serialization for the format %s is not supported. Only %s supported.', $format, $this->format));
Expand All @@ -115,12 +115,13 @@ public function serialize($data, $format, array $context = [])
/**
* {@inheritdoc}
*/
public function deserialize($data, $type, $format, array $context = [])
public function deserialize($data, $type, $format, array $context = []): mixed
{
if (!$this->supportsDecoding($format)) {
throw new NotEncodableValueException(sprintf('Deserialization for the format %s is not supported. Only %s is supported.', $format, $this->format));
}

$context['json_decode_associative'] = false;
return $this->serializer->deserialize($data, $type, $format, $context);
}

Expand Down Expand Up @@ -202,15 +203,15 @@ public function supportsDecoding($format)
/**
* {@inheritdoc}
*/
public function encode($data, $format, array $context = [])
public function encode($data, $format, array $context = []): string
{
return $this->serializer->encode($data, $format, $context = []);
}

/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
public function supportsEncoding($format): bool
{
return $this->format === $format && $this->serializer->supportsEncoding($format);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function __construct($defaultContext = [], int $depth = 512)
/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = [])
public function decode($data, $format, array $context = []): mixed
{
$context['json_decode_options'] = empty($context['json_decode_options']) ? $this->options : $context['json_decode_options'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function validate(\stdClass $input, \stdClass $output, EntityInterface $e
return;
}

Assert::assertEquals($output->{static::validatedProperty()}, (object) ['id' => $entity->getOrganization()->id()]);
Assert::assertEquals((object) ['id' => $output->{static::validatedProperty()}->id], (object) ['id' => $entity->getOrganization()->id()]);

$actual = json_decode($this->entitySerializer->serialize($entity->getOrganization(), 'json'));
$expected = $input->{static::validatedProperty()};
Expand Down
2 changes: 1 addition & 1 deletion tests/Serializer/EntitySerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function testNormalize()
*
* @param \stdClass $normalized
*/
public function testDenormalize(\stdClass $normalized): void
public function testDenormalize(mixed $normalized): void
{
// Set value of this nullable value to ensure that a special condition is triggered in the EntityDenormalizer.
$normalized->nullable = null;
Expand Down
2 changes: 1 addition & 1 deletion tests/Structure/PropertiesPropertyTransformationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testNormalize()
*
* @param \stdClass $normalized
*/
public function testDenormalize(\stdClass $normalized): void
public function testDenormalize(mixed $normalized): void
{
/** @var \Apigee\Edge\Structure\PropertiesProperty $object */
$object = static::$denormalizer->denormalize($normalized, PropertiesProperty::class);
Expand Down
2 changes: 1 addition & 1 deletion tests/Test/HttpClient/FileSystemResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function __construct(AdapterInterface $adapter = null, DecoderInterface $
$adapter = new Local($folder);
}
$this->filesystem = new Filesystem($adapter);
$this->decoder = $decoder ?: new JsonDecode(true);
$this->decoder = $decoder ?: new JsonDecode([true]);
}

/**
Expand Down

0 comments on commit 8fdd5c9

Please sign in to comment.