Skip to content

ReflectionProperty does not return the PHPDoc of a property if it contains an attribute with a Closure #20895

Closed

Description

@zavarock

Description

The following code:

<?php
#[\Attribute(\Attribute::TARGET_PROPERTY)]
class Hello {
    public function __construct(
        public ?Closure $world = null,
    ) {
    }
}

class Foo {
    /**
     * @var string
     */
    #[Hello(static function() {
        return null;
    })]
    public $bar;
}

$reflectionProperty = new ReflectionProperty(Foo::class, 'bar');

var_dump($reflectionProperty->getDocComment());

Resulted in this output:

bool(false)

But I expected this output instead:

string(30) "/** * @var string */"

PHP Version

PHP 8.5.0

Operating System

No response

Activity

  1. TimWolla commented on Jan 10, 2026

    @TimWolla
    Member

    The doc comment is incorrectly assigned to the Closure:

    <?php
    #[\Attribute(\Attribute::TARGET_PROPERTY)]
    class Hello {
        public function __construct(
            public ?Closure $world = null,
        ) {
        }
    }
    
    class Foo {
        /**
         * @var string
         */
        #[Hello(static function() {
            return null;
        })]
        public $bar;
    }
    
    $reflectionProperty = new ReflectionProperty(Foo::class, 'bar');
    $closure = $reflectionProperty->getAttributes()[0]->getArguments()[0];
    var_dump((new ReflectionFunction($closure))->getDocComment());

    outputs:

    string(30) "/**
         * @var string
         */"
    
  2. zavarock commented on Jan 10, 2026

    @zavarock
    Author

    Thanks for the feedback.
    I see it now, but in this case, how can we assign the doc to a property instead of the Closure? Assuming this is the order (DOC > Attributes) of things?
    I mean, even libraries such as Symfony Serializer rely on it to determine the type of a property when it is not explicitly specified.

  3. self-assigned this
    on Jan 10, 2026
  4. TimWolla commented on Jan 10, 2026

    @TimWolla
    Member

    I see it now, but in this case, how can we assign the doc to a property instead of the Closure?

    A proposed fix is in #20896.

  5. added 2 commits that reference this issue on Jan 10, 2026
    6c60fbb
    11ae6ad
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