[php-src] Issue #11797: PHP 8.2: Issue with type declaration for `iterable|object`
| From: | mpyw | Date: | Wed, 26 Jul 2023 05:01:02 +0000 |
| Subject: | [php-src] Issue #11797: PHP 8.2: Issue with type declaration for `iterable|object` | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-245040@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/11797
Author: mpyw
### Description
With PHP 8.2, the following changes were applied:
- [PHP: rfc:iterator_xyz_accept_array](https://wiki.php.net/rfc/iterator_xyz_accept_array)
- [RFC: Make the iterator_*() family accept all iterables by TimWolla · Pull Request #8819 ·
php/php-src](https://github.com/php/php-src/pull/8819)
As a result,
iterable became an alias for Traversable|array, changing the
result of reflection acquisition as mentioned [here](https://externals.io/message/118939).
However, there's a problem. While Traversable|object becomes an error because
it's redundant, iterable|object compiles without any issue. But if we resolve the
alias of iterable|object, it becomes **Traversable|object|array**, which
is an invalid expression for PHP's type declaration.
In fact, we encountered a bug where creating a mock of a class with a method that takes
iterable|object as an argument in the
[mockery/mockery](https://github.com/mockery/mockery) causes a compile error only in PHP 8.2 or
later. Should this be fixed by prohibiting the declaration of iterable|object itself?
Or is this intended behavior, and should be addressed on the library side?
```php
class ExampleClass
{
public function __invoke(iterable|object $arg): void
{
}
}
```
```php
// Type Traversable|object|array contains both object and a class type, which is redundant
$mock = \Mockery\Mockery::mock(Example::class);
```
### PHP Version
PHP 8.2
### Operating System
_No response_