Without this, reflection-based DI containers, static analyzers, and bootstrap inventory tools cannot introspect union/intersection type hints.
<?php
class C {
public function m(string|int $x, Countable&Traversable $y): void {}
}
$rm = new ReflectionMethod(C::class, 'm');
$union = $rm->getParameters()[0]->getType();
$inter = $rm->getParameters()[1]->getType();
echo get_class($union), "\n";
echo get_class($inter), "\n";
var_export(method_exists($union, 'getTypes'));
var_export(method_exists($inter, 'getTypes'));
// Zend: both true; getTypes() returns ReflectionNamedType[] / nested composites
Category
stdlibProblem
ReflectionUnionTypeandReflectionIntersectionTypeclasses are registered inlib/VM/BuiltinClasses.php, butgetTypes()is not implemented — the method table is empty ([]). Zend returns an array ofReflectionNamedType(or nested union/intersection) objects for composite parameter/return/property types.Without this, reflection-based DI containers, static analyzers, and bootstrap inventory tools cannot introspect union/intersection type hints.
php-src reference
ext/reflection/php_reflection.c—ReflectionUnionType::getTypes,ReflectionIntersectionType::getTypesZend/zend_type.c— composite type listsRepro (failure today)
Save as
repro_reflection_composite_types.php:get_class($union)ReflectionUnionTypemethod_exists($union, 'getTypes')truefalse(empty method table)count($union->getTypes())2Scope (this repo)
ext/standard/VmReflection.php, newReflectionUnionTypeGetTypes.php,ReflectionIntersectionTypeGetTypes.phplib/Compiler.php— type hint metadata on parameters/propertieslib/VM/BuiltinClasses.phptest/compliance/cases/stdlib/reflection_union_intersection_types.phptDone when
method_exists(..., 'getTypes')true for both composite types$union->getTypes()returns twoReflectionNamedTypewith namesstringandint$inter->getTypes()returnsCountableandTraversablenamed types./script/ci-fast.sh --filter reflection_uniongreenLinks