Skip to content

Commit

Permalink
PHP 8.1: silence the deprecation notice about jsonSerialize() return …
Browse files Browse the repository at this point in the history
…type

As of PHP 8.1, PHP adds return type declarations to the PHP native functions.

For the `JsonSerializable::jsonSerialize()` interface method, the new signature is:
```php
function jsonSerialize(): mixed {}
```

As this libary still supports PHP 5.3, it is not possible to add this return type as:
1. Return types weren't available until PHP 7.0 and
2. the `mixed` return type only became available in PHP 8.0.

For libraries supporting PHP 7.0+, it would have been possible to fix this by adding an `array` return type (higher specificity).

For libraries still supporting PHP < 7.0, there are two choices:
1. Either decouple from the `JsonSerialize` interface.
2. Or use a PHP 8.1 attribute to silence the deprecation notice.

As prior to PHP 8.0, attributes are ignored as if they were comments, it is safe to add the attribute to the library and IMO, this is prefered over decoupling the classes from the `JsonSerializable` interface.

To prevent PHPCS tripping up over "something" existing between the function docblock and the declaration, PHPCS 3.6.0 should be used, which is the first PHPCS version with full PHP 8.0 syntax support in the sniffs (albeit that there are still some small things to fix up in PHPCS).

Refs:
* https://wiki.php.net/rfc/internal_method_return_types
* php/php-src#7051
  • Loading branch information
jrfnl authored and grogy committed Aug 13, 2021
1 parent 3c5c209 commit 49cc975
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"require-dev": {
"nette/tester": "^1.3 || ^2.0",
"php-parallel-lint/php-console-highlighter": "~0.3",
"squizlabs/php_codesniffer": "^3.5"
"squizlabs/php_codesniffer": "^3.6"
},
"suggest": {
"php-parallel-lint/php-console-highlighter": "Highlight syntax in code snippet"
Expand Down
4 changes: 4 additions & 0 deletions src/Error.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace JakubOnderka\PhpParallelLint;

use ReturnTypeWillChange;

class Error implements \JsonSerializable
{
/** @var string */
Expand Down Expand Up @@ -57,6 +59,7 @@ public function getShortFilePath()
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
#[ReturnTypeWillChange]
public function jsonSerialize()
{
return array(
Expand Down Expand Up @@ -87,6 +90,7 @@ class Blame implements \JsonSerializable
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
#[ReturnTypeWillChange]
function jsonSerialize()
{
return array(
Expand Down
3 changes: 3 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace JakubOnderka\PhpParallelLint;

use ReturnTypeWillChange;

class Result implements \JsonSerializable
{
/** @var Error[] */
Expand Down Expand Up @@ -154,6 +156,7 @@ public function getTestTime()
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
*/
#[ReturnTypeWillChange]
function jsonSerialize()
{
return array(
Expand Down
3 changes: 3 additions & 0 deletions src/exceptions.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php
namespace JakubOnderka\PhpParallelLint;

use ReturnTypeWillChange;

class Exception extends \Exception implements \JsonSerializable
{
#[ReturnTypeWillChange]
public function jsonSerialize()
{
return array(
Expand Down

0 comments on commit 49cc975

Please sign in to comment.