spatie / async

Easily run code asynchronously
https://spatie.be/en/opensource/php
MIT License
2.65k stars 179 forks source link

Set return types for ArrayAccess functions #184

Closed matthi4s closed 2 years ago

matthi4s commented 2 years ago

In PHP 8.1 the functions required by the ArrayAccess interface require a return type or they will log a deprecation warning:

Return type of Spatie\Async\Pool::offsetGet($offset) should either be compatible with 
ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute 
should be used to temporarily suppress the notice

(see https://php.watch/versions/8.1/internal-method-return-types)

I've added a return type to the ArrayAccess functions offsetExists(), offsetSet(), offsetGet() and offsetUnset() in the Pool class. Even though most of them aren't actually implemented, they still need a return type to avoid the deprecation warning. The offsetGet() function returns the Runnable type instead of the default mixed because that wouldn't be compatible with PHP 7.4.

Another solution would be to add the #[ReturnTypeWillChange] attribute, but that will probably break in PHP 9.0. The current solution is backwards compatible with PHP 7.4 and PHP 8.0, but will break for anyone that has extended the Pool class and overwrites the ArrayAccess functions without giving a proper return type. I think it's highly unlikely that anybody does that, but if you prefer, I can also just add the #[ReturnTypeWillChange] attribute.

spatie-bot commented 2 years ago

Dear contributor,

because this pull request seems to be inactive for quite some time now, I've automatically closed it. If you feel this pull request deserves some attention from my human colleagues feel free to reopen it.

alexongh commented 2 years ago

@freekmurze could you review this merge request?

freekmurze commented 2 years ago

Thanks!

bvandevliet commented 2 years ago

Thanks! Errors in php 8.1 are gone :)