Skip to content

ReflectionFunction::getNamespaceName() is broken for closures with PHP8.4 #16122

Closed

Description

@takaram

Description

The following code:

<?php

namespace Foo;

class Bar
{
    public static function testClass(): \Closure
    {
        return function(){};
    }
}

function testFunc(): \Closure
{
    return function(){};
}

var_dump((new \ReflectionFunction(Bar::testClass()))->getNamespaceName());
var_dump((new \ReflectionFunction(testFunc()))->getNamespaceName());

Resulted in this output:

string(12) "{closure:Foo"
string(12) "{closure:Foo"

But I expected this output instead:

string(3) "Foo"
string(3) "Foo"

https://3v4l.org/QjjJK/rfc

PHP Version

PHP 8.4.0RC1

Operating System

No response

Activity

  1. iluuu1994 commented on Sep 29, 2024

    @iluuu1994
    Member

    The output is definitely bad, but closures don't have a namespace. Not sure what the correct output would be. Related to #12907.

  2. iluuu1994 commented on Sep 29, 2024

    @iluuu1994
    Member

    Oh, I see this only changed in 8.4.

  3. iluuu1994 commented on Sep 29, 2024

    @iluuu1994
    Member

    The name was previously Foo\{closure}, and is now {closure:Foo\Bar::testClass():9}. It seems this is related to #13550. Hence, the logic in ReflectionFunctionAbstract::getNamespaceName() is now broken, as it's looking for the first \ and, if present, returning everything up to it. /cc @TimWolla

    zend_string *name = fptr->common.function_name;
    const char *backslash = zend_memrchr(ZSTR_VAL(name), '\\', ZSTR_LEN(name));
    if (backslash) {
    RETURN_STRINGL(ZSTR_VAL(name), backslash - ZSTR_VAL(name));
    }
    RETURN_EMPTY_STRING();

  4. TimWolla commented on Sep 29, 2024

    @TimWolla
    Member

    Related to #14001 (which fixed it for getShortName()) and #14087 which follow-up fixed that for FCC. I'll send a PR tomorrow.

  5. self-assigned this
    on Sep 29, 2024
  6. TimWolla commented on Sep 30, 2024

    @TimWolla
    Member

    This also affects inNamespace(). My PR will fix both.

  7. added 2 commits that reference this issue on Sep 30, 2024
    754061c
    a1cc091
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions