--TEST-- Generics extension: union types with generics --DESCRIPTION-- Tests that union types work correctly with generic syntax. Union types with generics should have the generic portion stripped while maintaining the union type syntax (|). --SKIPIF-- --FILE-- name = $name; } } class Collection {} // Function parameters with union types and generics function processData(array|null $items): void { if ($items === null) { echo "null\n"; } else { echo "array with " . count($items) . " items\n"; } } function handleWidgets(Collection|array $widgets): string { if (is_array($widgets)) { return "array"; } return "collection"; } function mixedTypes(array|false $data): string { if ($data === false) { return "false"; } return "array"; } // Return types with union types and generics function getOptionalWidgets(): array|null { return null; } function getWidgetsOrCollection(): array|Collection { return []; } function fetchData(): array|false { return false; } // Class methods with union types and generics class DataRepository { public function find(int $id): array|null { return null; } public function findAll(): array|Collection { return [new Widget('test')]; } private function getData(): array|false { return ['key' => 42]; } } // Properties with union types and generics class Container { public array|null $widgets = null; private Collection|array $items; } // Constructor promotion with union types and generics class Service { public function __construct( public array|null $widgets, private Collection|array $ids = [], ) {} } // Test execution processData([new Widget('foo'), new Widget('bar')]); processData(null); echo handleWidgets([new Widget('test')]) . "\n"; echo handleWidgets(new Collection()) . "\n"; echo mixedTypes(['a' => 1]) . "\n"; echo mixedTypes(false) . "\n"; var_dump(getOptionalWidgets()); var_dump(is_array(getWidgetsOrCollection())); var_dump(fetchData()); $repo = new DataRepository(); var_dump($repo->find(1)); var_dump(is_array($repo->findAll())); $container = new Container(); var_dump($container->widgets); $service = new Service([new Widget('service')]); var_dump(count($service->widgets)); echo "All union type tests passed\n"; ?> --EXPECT-- array with 2 items null array collection array false NULL bool(true) bool(false) NULL bool(true) NULL int(1) All union type tests passed