Skip to content

Parser does not see docComments between attributes and class declaration #762

@ondrejmirtes

Description

@ondrejmirtes

Given this PHP code:

<?php

#[AttributeStub()]
/**
 * @template T
 */
class ClassStub
{
	/**
	 * @param T $var
	 */
	public function __construct(public $var) {}
}

PHP itself sees both attribute and doc comment: https://3v4l.org/Pe17n

But PHP-Parser doesn't (bin/php-parse output on PHP 7.4):

array(
    0: Stmt_Class(
        attrGroups: array(
            0: AttributeGroup(
                attrs: array(
                    0: Attribute(
                        name: Name(
                            parts: array(
                                0: AttributeStub
                            )
                        )
                        args: array(
                        )
                    )
                )
            )
        )
        flags: 0
        name: Identifier(
            name: ClassStub
        )
        extends: null
        implements: array(
        )
        stmts: array(
            0: Stmt_ClassMethod(
                attrGroups: array(
                )
                flags: MODIFIER_PUBLIC (1)
                byRef: false
                name: Identifier(
                    name: __construct
                )
                params: array(
                    0: Param(
                        attrGroups: array(
                        )
                        flags: MODIFIER_PUBLIC (1)
                        type: null
                        byRef: false
                        variadic: false
                        var: Expr_Variable(
                            name: var
                        )
                        default: null
                    )
                )
                returnType: null
                stmts: array(
                )
                comments: array(
                    0: /**
                     * @param T $var
                     */
                )
            )
        )
    )
)

Original report: phpstan/phpstan#4633

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions