core icon indicating copy to clipboard operation
core copied to clipboard

Correct return types for PHP 8.2 compatibility

Open cziegenberg opened this issue 3 years ago • 1 comments

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: https://github.com/php/php-src/issues/9556

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

cziegenberg avatar Sep 16 '22 12:09 cziegenberg

I could find the following affected files:

  • src\Symfony\Maker\Resources\skeleton\StateProvider.tpl.php
  • src\State\ProviderInterface.php
  • src\State\CallableProvider.php
  • src\GraphQl\Resolver\Stage\ReadStageInterface.php
  • src\GraphQl\Resolver\Stage\ReadStage.php

cziegenberg avatar Sep 16 '22 13:09 cziegenberg

Seems there is already a PR for it, but it's not linked: #5024

SpartakusMd avatar Sep 29 '22 06:09 SpartakusMd