• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 13175309672

06 Feb 2025 09:04AM UTC coverage: 7.663% (-0.2%) from 7.841%
13175309672

push

github

web-flow
fix: ensure template files have a tpl file extension (#6826) (#6829)

2 of 5 new or added lines in 4 files covered. (40.0%)

3676 existing lines in 122 files now uncovered.

13073 of 170593 relevant lines covered (7.66%)

27.3 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.09
/src/Metadata/Resource/Factory/PhpDocResourceMetadataCollectionFactory.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Metadata\Resource\Factory;
15

16
use ApiPlatform\Metadata\Operations;
17
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
18
use phpDocumentor\Reflection\DocBlockFactory;
19
use phpDocumentor\Reflection\DocBlockFactoryInterface;
20
use phpDocumentor\Reflection\Types\ContextFactory;
21
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
22
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
23
use PHPStan\PhpDocParser\Lexer\Lexer;
24
use PHPStan\PhpDocParser\Parser\ConstExprParser;
25
use PHPStan\PhpDocParser\Parser\PhpDocParser;
26
use PHPStan\PhpDocParser\Parser\TokenIterator;
27
use PHPStan\PhpDocParser\Parser\TypeParser;
28
use PHPStan\PhpDocParser\ParserConfig;
29

30
/**
31
 * Extracts descriptions from PHPDoc.
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
final class PhpDocResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
36
{
37
    private readonly ?DocBlockFactoryInterface $docBlockFactory;
38
    private readonly ?ContextFactory $contextFactory;
39
    private readonly ?PhpDocParser $phpDocParser;
40
    private readonly ?Lexer $lexer;
41

42
    /** @var array<string, PhpDocNode> */
43
    private array $docBlocks = [];
44

45
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated, ?DocBlockFactoryInterface $docBlockFactory = null)
46
    {
47
        $contextFactory = null;
2,729✔
48
        if ($docBlockFactory instanceof DocBlockFactoryInterface) {
2,729✔
UNCOV
49
            trigger_deprecation('api-platform/core', '3.1', 'Using a 2nd argument to PhpDocResourceMetadataCollectionFactory is deprecated.');
×
50
        }
51
        if (class_exists(DocBlockFactory::class) && class_exists(ContextFactory::class)) {
2,729✔
52
            $docBlockFactory = $docBlockFactory ?? DocBlockFactory::createInstance();
2,729✔
53
            $contextFactory = new ContextFactory();
2,729✔
54
        }
55
        $this->docBlockFactory = $docBlockFactory;
2,729✔
56
        $this->contextFactory = $contextFactory;
2,729✔
57
        if (class_exists(DocBlockFactory::class) && !class_exists(PhpDocParser::class)) {
2,729✔
UNCOV
58
            trigger_deprecation('api-platform/core', '3.1', 'Using phpdocumentor/reflection-docblock is deprecated. Require phpstan/phpdoc-parser instead.');
×
59
        }
60
        $phpDocParser = null;
2,729✔
61
        $lexer = null;
2,729✔
62
        if (class_exists(PhpDocParser::class) && class_exists(ParserConfig::class)) {
2,729✔
63
            $config = new ParserConfig([]);
2,729✔
64
            $phpDocParser = new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
2,729✔
65
            $lexer = new Lexer($config);
2,729✔
UNCOV
66
        } elseif (class_exists(PhpDocParser::class)) {
×
UNCOV
67
            $phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser()); // @phpstan-ignore-line
×
UNCOV
68
            $lexer = new Lexer(); // @phpstan-ignore-line
×
69
        }
70
        $this->phpDocParser = $phpDocParser;
2,729✔
71
        $this->lexer = $lexer;
2,729✔
72
    }
73

74
    /**
75
     * {@inheritdoc}
76
     */
77
    public function create(string $resourceClass): ResourceMetadataCollection
78
    {
79
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
174✔
80

81
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
174✔
82
            if (null !== $resourceMetadata->getDescription()) {
163✔
83
                continue;
3✔
84
            }
85

86
            $description = null;
163✔
87

88
            // Deprecated path. To remove in API Platform 4.
89
            if (!$this->phpDocParser instanceof PhpDocParser && $this->docBlockFactory instanceof DocBlockFactoryInterface && $this->contextFactory) {
163✔
90
                $reflectionClass = new \ReflectionClass($resourceClass);
×
91

92
                try {
UNCOV
93
                    $docBlock = $this->docBlockFactory->create($reflectionClass, $this->contextFactory->createFromReflector($reflectionClass));
×
UNCOV
94
                    $description = $docBlock->getSummary();
×
UNCOV
95
                } catch (\InvalidArgumentException) {
×
96
                    // Ignore empty DocBlocks
97
                }
98
            } else {
99
                $description = $this->getShortDescription($resourceClass);
163✔
100
            }
101

102
            if (!$description) {
163✔
103
                return $resourceMetadataCollection;
116✔
104
            }
105

106
            $resourceMetadataCollection[$key] = $resourceMetadata->withDescription($description);
66✔
107

108
            $operations = $resourceMetadata->getOperations() ?? new Operations();
66✔
109
            foreach ($operations as $operationName => $operation) {
66✔
110
                if (null !== $operation->getDescription()) {
66✔
UNCOV
111
                    continue;
×
112
                }
113

114
                $operations->add($operationName, $operation->withDescription($description));
66✔
115
            }
116

117
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withOperations($operations);
66✔
118

119
            if (!$resourceMetadata->getGraphQlOperations()) {
66✔
120
                continue;
32✔
121
            }
122

123
            foreach ($graphQlOperations = $resourceMetadata->getGraphQlOperations() as $operationName => $operation) {
66✔
124
                if (null !== $operation->getDescription()) {
66✔
125
                    continue;
59✔
126
                }
127

128
                $graphQlOperations[$operationName] = $operation->withDescription($description);
66✔
129
            }
130

131
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withGraphQlOperations($graphQlOperations);
66✔
132
        }
133

134
        return $resourceMetadataCollection;
77✔
135
    }
136

137
    /**
138
     * Gets the short description of the class.
139
     */
140
    private function getShortDescription(string $class): ?string
141
    {
142
        if (!$docBlock = $this->getDocBlock($class)) {
163✔
143
            return null;
116✔
144
        }
145

146
        foreach ($docBlock->children as $docChild) {
66✔
147
            if ($docChild instanceof PhpDocTextNode && !empty($docChild->text)) {
66✔
148
                return $docChild->text;
66✔
149
            }
150
        }
151

152
        return null;
4✔
153
    }
154

155
    private function getDocBlock(string $class): ?PhpDocNode
156
    {
157
        if (isset($this->docBlocks[$class])) {
163✔
158
            return $this->docBlocks[$class];
32✔
159
        }
160

161
        try {
162
            $reflectionClass = new \ReflectionClass($class);
163✔
UNCOV
163
        } catch (\ReflectionException) {
×
UNCOV
164
            return null;
×
165
        }
166

167
        $rawDocNode = $reflectionClass->getDocComment();
163✔
168

169
        if (!$rawDocNode) {
163✔
170
            return null;
116✔
171
        }
172

173
        $tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
66✔
174
        $phpDocNode = $this->phpDocParser->parse($tokens);
66✔
175
        $tokens->consumeTokenType(Lexer::TOKEN_END);
66✔
176

177
        return $this->docBlocks[$class] = $phpDocNode;
66✔
178
    }
179
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc