Skip to content

Commit 46ad428

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 46ad428

File tree

4 files changed

+5
-1
lines changed

4 files changed

+5
-1
lines changed

composer.json

+1-1
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

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function getShortFilePath()
5757
* @return mixed data which can be serialized by <b>json_encode</b>,
5858
* which is a value of any type other than a resource.
5959
*/
60+
#[ReturnTypeWillChange]
6061
public function jsonSerialize()
6162
{
6263
return array(
@@ -87,6 +88,7 @@ class Blame implements \JsonSerializable
8788
* @return mixed data which can be serialized by <b>json_encode</b>,
8889
* which is a value of any type other than a resource.
8990
*/
91+
#[ReturnTypeWillChange]
9092
function jsonSerialize()
9193
{
9294
return array(

src/Result.php

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ public function getTestTime()
154154
* @return mixed data which can be serialized by <b>json_encode</b>,
155155
* which is a value of any type other than a resource.
156156
*/
157+
#[ReturnTypeWillChange]
157158
function jsonSerialize()
158159
{
159160
return array(

src/exceptions.php

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
class Exception extends \Exception implements \JsonSerializable
55
{
6+
#[ReturnTypeWillChange]
67
public function jsonSerialize()
78
{
89
return array(

0 commit comments

Comments
 (0)