Skip to content

[PropertyInfo] PhpStanExtractor does not check for dependency package #49123

@andersonamuller

Description

@andersonamuller

Symfony version(s) affected

6.2.5

Description

The \Symfony\Component\PropertyInfo\PhpStan\NameScopeFactory has a dependency on \phpDocumentor\Reflection\Types\ContextFactory but that is very clear when using the \Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor and it will throw an error when doing it.

The \Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor fails with a more clear exception.

How to reproduce

// First, run "composer require symfony/property-info"
// and "composer require phpstan/phpdoc-parser"
// Then, execute this file:
<?php

require 'vendor/autoload.php';

class Role
{
    public function __construct(
        public readonly string $name,
    ) {
    }
}

class User
{
    /**
     * @param list<Role> $roles
     */
    public function __construct(
        public readonly array $roles,
    ) {
    }
}

$extractor = new \Symfony\Component\PropertyInfo\Extractor\PhpStanExtractor();
$types = $extractor->getTypes(User::class, 'roles');

// It will throw an error about an hidden dependency on \phpDocumentor\Reflection\Types\ContextFactory::class

Possible Solution

Add to the constructor of the PhpStanExtractor a check like in the PhpDocExtractor

final class PhpStanExtractor implements PropertyTypeExtractorInterface, ConstructorArgumentTypeExtractorInterface 
{
...        
    public function __construct(array $mutatorPrefixes = null, array $accessorPrefixes = null, array $arrayMutatorPrefixes = null)
    {
        if (!class_exists(\phpDocumentor\Reflection\Types\ContextFactory::class)) {
            throw new \LogicException(sprintf('Unable to use the "%s" class as the "phpdocumentor/reflection-docblock" package is not installed.', __CLASS__));
        }
...

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions