php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81593 ReflectionUnionType does not honor order of type
Submitted: 2021-11-05 22:37 UTC Modified: 2021-11-05 22:44 UTC
From: php at vxvr dot de Assigned:
Status: Not a bug Package: Reflection related
PHP Version: 8.1.0RC5 OS: unix / debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at vxvr dot de
New email:
PHP Version: OS:

 

 [2021-11-05 22:37 UTC] 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']

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2021-11-05 22:44 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2021-11-05 22:44 UTC] nikic@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Jul 20 00:01:03 2024 UTC