[REQ][PHP] PHP 8.1 Support
Created by: satackey
Is your feature request related to a problem? Please describe.
When I used the generated PHP code with PHP 8.1, I got the following Deprecation notice.
Deprecated: Return type of OpenAPI\Client\Model\FooObject::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /app/php/lib/Model/FooObject.php on line 238
This problem is caused by the following PHP 8.1 incompatible changes, as the generated model classes implement the PHP internal interfaces ArrayAccess and JsonSerializable.
Return Type Compatibility with Internal Classes ¶
Most non-final internal methods now require overriding methods to declare a compatible return type, otherwise a deprecated notice is emitted during inheritance validation. In case the return type cannot be declared for an overriding method due to PHP cross-version compatibility concerns, a #[ReturnTypeWillChange] attribute can be added to silence the deprecation notice. PHP: Backward Incompatible Changes - Manual
Describe the solution you'd like
Add a type hint or #[\ReturnTypeWillChange] attribute to the methods in the template.
I'm going to create a pull request with this fix applied.
Describe alternatives you've considered
I'm using a customized template copied from this repository.
Additional context
I have created a repository for reproduction at https://github.com/satackey/openapi-generator-php81 You can reproduce the problem with the following script.
$ git clone https://github.com/satackey/openapi-generator-php81.git
$ docker run --rm -it -v $PWD:/app -w /app openapitools/openapi-generator-cli:latest generate -i ./openapi.yml -o ./ -g php
$ docker run --rm -it -v $PWD:/app -w /app composer install
$ docker run --rm -it -v $PWD:/app -w /app php:8.1 php index.php
Deprecated: Return type of OpenAPI\Client\Model\FooObject::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /app/lib/Model/FooObject.php on line 238
Deprecated: Return type of OpenAPI\Client\Model\FooObject::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /app/lib/Model/FooObject.php on line 250
Deprecated: Return type of OpenAPI\Client\Model\FooObject::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /app/lib/Model/FooObject.php on line 263
Deprecated: Return type of OpenAPI\Client\Model\FooObject::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /app/lib/Model/FooObject.php on line 279
Deprecated: Return type of OpenAPI\Client\Model\FooObject::jsonSerialize() should either be compatible with JsonSerializable::jsonSerialize(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /app/lib/Model/FooObject.php on line 291