Skip to content

Incorrect type resolution (parentheses, generic, etc.) #49

@rainbow-alex

Description

@rainbow-alex
<?php

require "vendor/autoload.php";

$comment = '/**
 * @return (string|Apple)
 */';

$block = new \phpDocumentor\Reflection\DocBlock($comment);
var_dump($block->getTagsByName("return")[0]->getType()); // \(string|\Apple)

$block = new \phpDocumentor\Reflection\DocBlock($comment, new \phpDocumentor\Reflection\DocBlock\Context("A"));
var_dump($block->getTagsByName("return")[0]->getType()); // \A\(string|\A\Apple)

Generic types are parsed incorrectly as well; and there may be other issues. Since PSR-5 is still a draft, I didn't expect 100% compliance, but the values returned now are pretty useless. Not getting correctly parsed types blocks my project, so I'd gladly help out with this.

The current API for getting types is getType: string and getTypes: Type\Collection (the latter basically being string[]). Unfortunately, PSR-5's types are more complicated... As a consumer I want an API that allows me to handle complex types. Something like:

interface TypeInterface
class ScalarType implements TypeInterface {
    public function getType() // 'string', 'int', etc.
}
class ArrayOfType implements TypeInterface {
    public function getItemType() // TypeInterface of the items
}
class GenericType implements TypeInterface {
    public function getTemplateType()
    public function getParameterTypes()
}
...

I think this belongs in the ReflectionDocBlock library, but I'd like some feedback before I implement this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions