php-src icon indicating copy to clipboard operation
php-src copied to clipboard

ReflectionProperty::getDocComment not respect Enum Cases PHPDoc.

Open michaljusiega opened this issue 4 years ago • 3 comments

Description

The following code:

<?php

enum TestEnum: string {
    /** Description about Case. */
    case ABC = 'abc';
    
    case XYZ = 'xyz';
    
    /** @var string */
    public const QWERTY = 'qwerty';
}

$reflectionClass = new ReflectionClass(TestEnum::class);

var_dump($reflectionClass->getProperties()[0]->getDocComment());
var_dump($reflectionClass->getProperties()[1]->getDocComment());

var_dump($reflectionClass->getReflectionConstants()[0]->getDocComment());
var_dump($reflectionClass->getReflectionConstants()[1]->getDocComment());
var_dump($reflectionClass->getReflectionConstants()[2]->getDocComment());

Resulted in this output:

bool(false)
bool(false)
string(30) "/** Description about Case. */"
bool(false)
string(18) "/** @var string */"

But I expected this output instead:

string(30) "/** Description about Case. */"
bool(false)
string(30) "/** Description about Case. */"
bool(false)
string(18) "/** @var string */"

The link for 3v4l.org: https://3v4l.org/s1Lru

PHP Version

PHP 8.1.0

Operating System

Any

michaljusiega avatar Jan 08 '22 14:01 michaljusiega

Why do you expect the doc comment on the enum case to also appear on the $nameproperty?

nikic avatar Jan 08 '22 15:01 nikic

Your enum roughly gets rewritten to this:

enum TestEnum {
    /** Description about Case. */
    public const ABC = new TestEnum('ABC', 'abc');
    public const XYZ = new TestEnum('XYZ', 'xyz');
    
    /** @var string */
    public const QWERTY = 'qwerty';

    private function __construct(
        public readonly string $name,
        public readonly string $value,
    ) {}
}

As you can see, the comment is stored on the constants, not on the name or value properties. That couldn't work because we have n cases but only 1 name/value property, so which of the n comments should be added?

iluuu1994 avatar Jan 08 '22 15:01 iluuu1994

Ah, I'm still learning this new mechanism.

However I was wondering if this was expected or not with Enum, PHPDoc and Reflection so I reported this issue.

Thanks @iluuu1994 and @nikic for your explanations!

michaljusiega avatar Jan 08 '22 15:01 michaljusiega