Skip to content

PHP 8.2: Issue with type declaration for iterable|object #11797

Closed as not planned
@mpyw

Description

@mpyw

Description

With PHP 8.2, the following changes were applied:

As a result, iterable became an alias for Traversable|array, changing the result of reflection acquisition as mentioned here.

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 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?

class ExampleClass
{
    public function __invoke(iterable|object $arg): void
    {
    }
}
// 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

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions