Skip to content

Commit

Permalink
Fix for Deprecation warnings (#232)
Browse files Browse the repository at this point in the history
* Fix for Deprecation warnings
  • Loading branch information
divya-intelli authored Sep 26, 2022
1 parent 88b55ec commit 4af100b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ return PhpCsFixer\Config::create()
'no_superfluous_phpdoc_tags' => false,
// Disabled because multiple lines allow code clarity.
'single_line_throw' => false,
// Disabled because phpcs fails for php8.1 if there is '&' in the argument.
// see https://github.com/apigee/apigee-client-php/issues/233
'function_typehint_space' => false,
])
->setFinder($finder);
7 changes: 1 addition & 6 deletions src/Api/Management/Controller/StatsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ class StatsController extends AbstractController implements StatsControllerInter
* Name of the organization that the entities belongs to.
* @param \Apigee\Edge\ClientInterface $client
* Apigee Edge API client.
*
* @psalm-suppress InvalidArgument
* Required since symfony/serializer >= 4.2.0
*
* @see https://github.com/symfony/symfony/pull/28709
*/
public function __construct(string $environment, string $organization, ClientInterface $client)
{
Expand All @@ -67,7 +62,7 @@ public function __construct(string $environment, string $organization, ClientInt
$this->normalizer = new StatsQueryNormalizer();
// Return responses as an associative array instead of in Apigee Edge's mixed object-array structure to
// make developer's life easier.
$this->jsonDecoder = new JsonDecode(true);
$this->jsonDecoder = new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
}

/**
Expand Down
7 changes: 1 addition & 6 deletions src/Api/Monetization/Serializer/EntitySerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class EntitySerializer extends BaseEntitySerializer
* EntitySerializer constructor.
*
* @param array $normalizers
*
* @psalm-suppress InvalidArgument
* Required since symfony/serializer >= 4.2.0
*
* @see https://github.com/symfony/symfony/pull/28709
*/
public function __construct($normalizers = [])
{
Expand All @@ -45,7 +40,7 @@ public function __construct($normalizers = [])
[
// Apigee Edge's default timezone is UTC, let's pass it as
// timezone instead of user's current timezone.
new DateTimeNormalizer(EntityInterface::DATE_FORMAT, new \DateTimeZone('UTC')),
new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => EntityInterface::DATE_FORMAT, DateTimeNormalizer::TIMEZONE_KEY => new \DateTimeZone('UTC')]),
new DateTimeZoneDenormalizer(),
new DateTimeZoneNormalizer(),
new EntityNormalizer(),
Expand Down
11 changes: 3 additions & 8 deletions src/Api/Monetization/Utility/TimezoneFixerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ protected function fixTimeZoneOnNormalization($object, $normalized, \DateTimeZon
// timezone if it is different than the default PHP timezone.
if (date_default_timezone_get() !== $orgTimezone->getName()) {
$ro = new \ReflectionObject($object);
// @psalm-suppress required since symfony/serializer >= 4.2.0
// @see https://github.com/symfony/symfony/pull/28709
/** @psalm-suppress InvalidArgument */
$dateDenormalizer = new DateTimeNormalizer(EntityInterface::DATE_FORMAT, $orgTimezone);
$dateDenormalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => EntityInterface::DATE_FORMAT, DateTimeNormalizer::TIMEZONE_KEY => $orgTimezone]);
foreach ($ro->getProperties() as $property) {
$property->setAccessible(true);
$value = $property->getValue($object);
Expand Down Expand Up @@ -81,10 +78,8 @@ protected function fixTimeZoneOnDenormalization($object, $denormalized, \DateTim

if (date_default_timezone_get() !== $orgTimezone->getName()) {
$ro = new \ReflectionObject($denormalized);
// @psalm-suppress required since symfony/serializer >= 4.2.0
// @see https://github.com/symfony/symfony/pull/28709
/** @psalm-suppress InvalidArgument */
$dateDenormalizer = new DateTimeNormalizer(EntityInterface::DATE_FORMAT, $orgTimezone);

$dateDenormalizer = new DateTimeNormalizer([DateTimeNormalizer::FORMAT_KEY => EntityInterface::DATE_FORMAT, DateTimeNormalizer::TIMEZONE_KEY => $orgTimezone]);
foreach ($object as $prop_name => $prop_value) {
if ($ro->hasProperty($prop_name)) {
$property = $ro->getProperty($prop_name);
Expand Down
38 changes: 30 additions & 8 deletions src/Serializer/JsonDecode.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@
*/
final class JsonDecode extends BaseJsonDecode
{
/**
* True to return the result as an associative array, false for a nested stdClass hierarchy.
*/
public const ASSOCIATIVE = 'json_decode_associative';

public const OPTIONS = 'json_decode_options';

/**
* Specifies the recursion depth.
*/
public const RECURSION_DEPTH = 'json_decode_recursion_depth';

private $defaultContext = [
self::ASSOCIATIVE => false,
self::OPTIONS => 0,
self::RECURSION_DEPTH => 512,
];

/**
* @var int
*/
Expand All @@ -35,17 +53,21 @@ final class JsonDecode extends BaseJsonDecode
/**
* JsonDecode constructor.
*
* @param bool $associative
* @param array $defaultContext
* @param int $depth
*
* @psalm-suppress InvalidArgument
* Required since symfony/serializer >= 4.2.0
*
* @see https://github.com/symfony/symfony/pull/28709
*/
public function __construct(bool $associative = false, int $depth = 512)
public function __construct($defaultContext = [], int $depth = 512)
{
parent::__construct($associative, $depth);
if (!\is_array($defaultContext)) {
@trigger_error(sprintf('Using constructor parameters that are not a default context is deprecated since Symfony 4.2, use the "%s" and "%s" keys of the context instead.', self::ASSOCIATIVE, self::RECURSION_DEPTH), \E_USER_DEPRECATED);

$defaultContext = [
self::ASSOCIATIVE => (bool) $defaultContext,
self::RECURSION_DEPTH => $depth,
];
}

parent::__construct($defaultContext, $depth);
// Following the same logic as in JsonEcode.
$this->options = JSON_PRESERVE_ZERO_FRACTION;
}
Expand Down
9 changes: 2 additions & 7 deletions src/Serializer/JsonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,10 @@ final class JsonEncoder extends BaseJsonEncoder
* The encoder implementation is intentionally not swappable.
*
* @param \Apigee\Edge\Serializer\JsonDecode|null $decodingImpl
*
* @psalm-suppress InvalidArgument
* Required since symfony/serializer >= 4.2.0
*
* @see https://github.com/symfony/symfony/pull/28709
*/
public function __construct(JsonDecode $decodingImpl = null)
{
$decodingImpl = $decodingImpl ?: new JsonDecode(true);
parent::__construct(new JsonEncode(JSON_PRESERVE_ZERO_FRACTION), $decodingImpl);
$decodingImpl = $decodingImpl ?: new JsonDecode([JsonDecode::ASSOCIATIVE => true]);
parent::__construct(new JsonEncode([JsonEncode::OPTIONS => JSON_PRESERVE_ZERO_FRACTION]), $decodingImpl);
}
}

0 comments on commit 4af100b

Please sign in to comment.