Skip to content

Correct return types for PHP 8.2 compatibility #4960

Description

@cziegenberg

API Platform version(s) affected: 3.0.0

Description
The return type definition "object|iterable|null" (used several times in API Platform) results in an error, because in PHP 8.2 "iterable" is an alias for "array|Traversable" and so the return type definition becomes "object|array|Traversable|null", and this is invalid, because "Traversable" is redundant (already contained in "object").

How to reproduce
Use current PHP 8.2RC2

Possible Solution
Replace the return type "iterable" with "array", if also "object" is in the list of allowed return types. Same meaning and valid for PHP 8.1 and PHP 8.2 (but perhaps Traversable should also be listed in the related code documentation to make this clear).

Following some first tests this modification of the return type definition (i.e. in the ProviderInterface) should no break anything in API Platform, because it still results in the same return types allowed.

Additional Context
Reported as a bug in PHP 8.2, but it cannot be fixed, so it's correctly documented as breaking change now:
php/php-src#9556

As confirmed there, "object|array is identical to object|iterable", so it can safely be replaced.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions