Bug #81593 [Opn->Nab]: ReflectionUnionType does not honor order of type
Edit report at https://bugs.php.net/bug.php?id=81593&edit=1
ID: 81593
Updated by: nikic@php.net
Reported by: php at vxvr dot de
Summary: ReflectionUnionType does not honor order of type
-Status: Open
+Status: Not a bug
Type: Bug
Package: Reflection related
Operating System: unix / debian
PHP Version: 8.1.0RC5
Block user comment: N
Private report: N
New Comment:
Working as intended and specified in https://wiki.php.net/rfc/union_types_v2#reflection.
> The getTypes() method returns an array of ReflectionTypes that are part of the union. The types
> may be returned in an arbitrary order that does not match the original type declaration. The types
> may also be subject to equivalence transformations.
Previous Comments:
------------------------------------------------------------------------
[2021-11-05 22:37:05] php at vxvr dot de
Description:
------------
Using the latest PHP version 8.1 RC 5
I would expect the ParameterUnionType to return the ReflectionNamedTypes in order in which they are
defined at the method, but as it seems they are returned in a fixed order.
Test script:
---------------
<?php
class MyClass {
public function __construct(int|string $value)
{
}
}
$reflectionMethod = new ReflectionMethod(MyClass::class, '__construct');
$parameterUnionType = $reflectionMethod->getParameters()[0]->getType();
$types = array_map(fn(ReflectionNamedType $type) => $type->getName(),
$parameterUnionType->getTypes());
// Expected: ['int', 'string']
// Actual: ['string', 'int']
Expected result:
----------------
// Expected: ['int', 'string']
Actual result:
--------------
// Actual: ['string', 'int']
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81593&edit=1
Thread (2 messages)