Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP 8.1: silence the deprecation notices about missing return types #64

Merged

Commits on Jul 23, 2021

  1. 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
    jrfnl committed Jul 23, 2021
    Configuration menu
    Copy the full SHA
    f6c4e1c View commit details
    Browse the repository at this point in the history

Commits on Jul 29, 2021

  1. PHP 8.1: silence the deprecation notice about RecursiveFilterIterator…

    … method return types
    
    As of PHP 8.1, PHP adds return type declarations to the PHP native functions.
    
    For the `RecursiveFilterIterator`, the relevant method signatures are:
    * `accept(): bool`
    * `hasChildren(): bool`
    * `getChildren(): ?RecursiveFilterIterator`
    
    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 still supporting PHP < 7.0, there are two choices:
    1. Either decouple from the 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 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
    jrfnl committed Jul 29, 2021
    Configuration menu
    Copy the full SHA
    06af4e4 View commit details
    Browse the repository at this point in the history