Skip to content

Commit f6c4e1c

Browse files
committed
PHP 8.1: silence the deprecation notice about jsonSerialize() return 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
1 parent 3c5c209 commit f6c4e1c

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require-dev": {
2121
"nette/tester": "^1.3 || ^2.0",
2222
"php-parallel-lint/php-console-highlighter": "~0.3",
23-
"squizlabs/php_codesniffer": "^3.5"
23+
"squizlabs/php_codesniffer": "^3.6"
2424
},
2525
"suggest": {
2626
"php-parallel-lint/php-console-highlighter": "Highlight syntax in code snippet"

src/Error.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace JakubOnderka\PhpParallelLint;
33

4+
use ReturnTypeWillChange;
5+
46
class Error implements \JsonSerializable
57
{
68
/** @var string */
@@ -57,6 +59,7 @@ public function getShortFilePath()
5759
* @return mixed data which can be serialized by <b>json_encode</b>,
5860
* which is a value of any type other than a resource.
5961
*/
62+
#[ReturnTypeWillChange]
6063
public function jsonSerialize()
6164
{
6265
return array(
@@ -87,6 +90,7 @@ class Blame implements \JsonSerializable
8790
* @return mixed data which can be serialized by <b>json_encode</b>,
8891
* which is a value of any type other than a resource.
8992
*/
93+
#[ReturnTypeWillChange]
9094
function jsonSerialize()
9195
{
9296
return array(

src/Result.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace JakubOnderka\PhpParallelLint;
33

4+
use ReturnTypeWillChange;
5+
46
class Result implements \JsonSerializable
57
{
68
/** @var Error[] */
@@ -154,6 +156,7 @@ public function getTestTime()
154156
* @return mixed data which can be serialized by <b>json_encode</b>,
155157
* which is a value of any type other than a resource.
156158
*/
159+
#[ReturnTypeWillChange]
157160
function jsonSerialize()
158161
{
159162
return array(

src/exceptions.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
<?php
22
namespace JakubOnderka\PhpParallelLint;
33

4+
use ReturnTypeWillChange;
5+
46
class Exception extends \Exception implements \JsonSerializable
57
{
8+
#[ReturnTypeWillChange]
69
public function jsonSerialize()
710
{
811
return array(

0 commit comments

Comments
 (0)