phpDocumentor/ReflectionDocBlock

Parsing custom tags

cnizzardini opened this issue · 2 comments

Is there a way to parse a custom tag like this:

@SwagQuery string $random some description here

or this:

@SwagQuery(type="string", name="random") some description

I have created a custom tag but phpDocumentor doesn't seem to parse the added variables akin to what you would get out of Doctrine Annotations. Is it possible to break this data into arrays or would that be custom code on my part?

🤔 this is interesting... we had the intention to support custom tags. But you are saying that it doesn't work? Could you provide me some more information on what you tried? Maybe a dummy repo where I can have a look to reproduce this issue?

The Parentheses notation is not supported it think. I do need to check this. But any input from your side is welcome. I would be happy to assist you on this.

Thanks for replying. This was from a while ago and admittedly I was still learning some things. I ended up using the Doctrine Annotation library, but I set a debugger on my unit tests and did notice that phpDocumentor\Reflection\DocBlock does indeed grab custom tags. Here is some output:

Documentor\Reflection\DocBlock Object
(
    [summary:phpDocumentor\Reflection\DocBlock:private] => Gets Departments
    [description:phpDocumentor\Reflection\DocBlock:private] => phpDocumentor\Reflection\DocBlock\Description Object
        (
            [bodyTemplate:phpDocumentor\Reflection\DocBlock\Description:private] => 
            [tags:phpDocumentor\Reflection\DocBlock\Description:private] => Array
                (
                )

        )

    [tags:phpDocumentor\Reflection\DocBlock:private] => Array
        (
            [0] => phpDocumentor\Reflection\DocBlock\Tags\Generic Object
                (
                    [name:protected] => Swag\SwagPaginator
                    [description:protected] => 
                )

            [1] => phpDocumentor\Reflection\DocBlock\Tags\Generic Object
                (
                    [name:protected] => Swag\SwagQuery
                    [description:protected] => phpDocumentor\Reflection\DocBlock\Description Object
                        (
                            [bodyTemplate:phpDocumentor\Reflection\DocBlock\Description:private] => (name="random", type="boolean", required=true)
                            [tags:phpDocumentor\Reflection\DocBlock\Description:private] => Array
                                (
                                )

                        )

                )

So your library works as you intended and my query was in error. It even grabs the Parentheses notation which could be easily handled through a custom userland parser. I wish I had known/seen this back in April, very odd as it seems so evident now.

For any future readers there is nothing fancy here, \phpDocumentor\Reflection\DocBlock was just instantiated like so:

    private function getDocBlock(string $fullyQualifiedNameSpace, string $methodName): DocBlock
    {
        $emptyDocBlock = DocBlockFactory::createInstance()->create('/**  */');

        if (!class_exists($fullyQualifiedNameSpace)) {
            return $emptyDocBlock;
        }

        try {
            return DocBlockUtility::getMethodDocBlock(new $fullyQualifiedNameSpace(), $methodName) ?? $emptyDocBlock;
        } catch (Exception $e) {
            return $emptyDocBlock;
        }
    }

Thanks for responding and hopefully this assists someone down the line.