Skip to content

Generics containing spaces not parsed correctly #186

@gronostajo

Description

@gronostajo

In v4.3.2 calling \phpDocumentor\Reflection\DocBlockFactory::create for this phpDoc:

/**
 * @return array<string, Channel>
 */

will cause a RuntimeException from \phpDocumentor\Reflection\TypeResolver::parseTypes:263 with message 'A type is missing in a collection expression'.

The bug seems to be in \phpDocumentor\Reflection\DocBlock\Tags\Return_::create:50. This split:

$parts = preg_split('/\s+/Su', $body, 2);

doesn't take generics into account. array<string, is being read as type and Channel> becomes the description. Removing the space after comma solves the problem.

I've tried to determine if those spaces are actually a valid syntax and it seems like they kind-of-are:

  1. Grammar on phpdoc.org doesn't allow for them (on a side note, the "phpDocumentor" link in top left leads to a 404)
  2. PSR-5 grammar currently doesn't support generics too
  3. However the PSR-5 draft did allow for them before Oct 16, 2018 and spaces were allowed.
  4. The file that throws exception phpdocumentor/type-resolver/src/TypeResolver.php does itself use spaces on lines 64-65:
    /**
     * @var array<string, string> List of recognized keywords and unto which Value Object they map
     * @psalm-var array<string, class-string<Type>>
     */
    

So the implementation seems to be inconsistent with grammar in PSR and usage in phpDocumentor/TypeResolver.

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