sdk-php icon indicating copy to clipboard operation
sdk-php copied to clipboard

jsonSerialize missing return type messages in PHP 8.1

Open aetikon opened this issue 3 years ago • 3 comments

PHP 8.1 outputs several deprecation messages because the jsonSerialize() methods implemented by this package do not specify the mixed return type nor use the #[\ReturnTypeWillChange] attribute.

The message doesn't appear in PHP 8.0.

Sample message:

PHP Deprecated: Return type of net\authorize\api\contract\v1\MerchantAuthenticationType::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/vagrant/selfcare/vendor/authorizenet/authorizenet/lib/net/authorize/api/contract/v1/MerchantAuthenticationType.php on line 261

Affected files:

lib/net/authorize/api/contract/v1/*.php

Suggested fix:

Suppress the notice by adding the #[\ReturnTypeWillChange] attribute above the method's implementation since the mixed return type is only available as of PHP 8.0.

#[\ReturnTypeWillChange]
public function jsonSerialize() { ... }

aetikon avatar Mar 24 '22 20:03 aetikon

These deprecation notices were annoying enough that I forked and patched the code on our servers with the #[\ReturnTypeWillChange] attribute, but it should be fixed for everyone.

I can't submit a pull request for this, apparently, as per the project guidelines:

Part of the SDK is auto-generated based on the XML schema. Due to this auto-generation, we cannot merge contributions for request or response classes. You are welcome to open an issue to report problems or suggest improvements. Auto-generated classes include all files inside contract/v1 and controller folders, except controller/base.

Temporary fixes:

  • There are 175 jsonSerialize() method signatures, so if you want to patch your own fork, here's a regex to fix them all quickly:

    • Find: \n(\s+)(.+)function jsonSerialize
    • Replace: \n$1#[\\ReturnTypeWillChange]\n$1$2function jsonSerialize
  • Or you can use our fork by updating composer.json:

    "require": {
      ...
      "authorizenet/authorizenet": "dev-php8_1-jsonSerialize-deprecation-fix#b53902ebcd7d095cf5271da69db02d823ddc23d3",
      ...
    },
    ...
    "repositories": [
      {
        "type": "vcs",
        "url": "https://github.com/BrownBoots/authorizenet-sdk-php.git"
      }
    ]
    

LordPachelbel avatar Jun 20 '22 22:06 LordPachelbel

@LordPachelbel thank you for fork. PR's seem to be stuck for PHP 8.1 issues. Anet team, please get these PHP 8.1 updates pulled in.

Zelfapp avatar Jul 30 '22 14:07 Zelfapp

@Zelfapp To be clear, my fork only suppresses the messages. The AuthorizeNet team might want to instead change the return type to mixed for all of the functions. It depends on how they're going to handle compatibility — that return type didn't exist before PHP 8.0 — i.e. whether they're going to maintain a separate PHP branch, or if they're going to continue with one release branch for all PHP versions.

LordPachelbel avatar Jul 30 '22 14:07 LordPachelbel