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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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: Tue Apr 30 22:01:31 2024 UTC