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

api-platform / core / 11035928124

25 Sep 2024 03:10PM UTC coverage: 5.963% (-1.9%) from 7.833%
11035928124

push

github

dunglas
Merge branch '4.0'

2 of 55 new or added lines in 6 files covered. (3.64%)

2414 existing lines in 166 files now uncovered.

9834 of 164915 relevant lines covered (5.96%)

4.85 hits per line

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

82.54
/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

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

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

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

69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function create(string $resourceClass): ResourceMetadataCollection
73
    {
74
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
69✔
75

76
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
69✔
77
            if (null !== $resourceMetadata->getDescription()) {
66✔
UNCOV
78
                continue;
×
79
            }
80

81
            $description = null;
66✔
82

83
            // Deprecated path. To remove in API Platform 4.
84
            if (!$this->phpDocParser instanceof PhpDocParser && $this->docBlockFactory instanceof DocBlockFactoryInterface && $this->contextFactory) {
66✔
85
                $reflectionClass = new \ReflectionClass($resourceClass);
×
86

87
                try {
88
                    $docBlock = $this->docBlockFactory->create($reflectionClass, $this->contextFactory->createFromReflector($reflectionClass));
×
89
                    $description = $docBlock->getSummary();
×
90
                } catch (\InvalidArgumentException) {
×
91
                    // Ignore empty DocBlocks
92
                }
93
            } else {
94
                $description = $this->getShortDescription($resourceClass);
66✔
95
            }
96

97
            if (!$description) {
66✔
98
                return $resourceMetadataCollection;
54✔
99
            }
100

101
            $resourceMetadataCollection[$key] = $resourceMetadata->withDescription($description);
24✔
102

103
            $operations = $resourceMetadata->getOperations() ?? new Operations();
24✔
104
            foreach ($operations as $operationName => $operation) {
24✔
105
                if (null !== $operation->getDescription()) {
24✔
106
                    continue;
×
107
                }
108

109
                $operations->add($operationName, $operation->withDescription($description));
24✔
110
            }
111

112
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withOperations($operations);
24✔
113

114
            if (!$resourceMetadata->getGraphQlOperations()) {
24✔
115
                continue;
12✔
116
            }
117

118
            foreach ($graphQlOperations = $resourceMetadata->getGraphQlOperations() as $operationName => $operation) {
24✔
119
                if (null !== $operation->getDescription()) {
24✔
120
                    continue;
21✔
121
                }
122

123
                $graphQlOperations[$operationName] = $operation->withDescription($description);
24✔
124
            }
125

126
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withGraphQlOperations($graphQlOperations);
24✔
127
        }
128

129
        return $resourceMetadataCollection;
27✔
130
    }
131

132
    /**
133
     * Gets the short description of the class.
134
     */
135
    private function getShortDescription(string $class): ?string
136
    {
137
        if (!$docBlock = $this->getDocBlock($class)) {
66✔
138
            return null;
54✔
139
        }
140

141
        foreach ($docBlock->children as $docChild) {
24✔
142
            if ($docChild instanceof PhpDocTextNode && !empty($docChild->text)) {
24✔
143
                return $docChild->text;
24✔
144
            }
145
        }
146

UNCOV
147
        return null;
×
148
    }
149

150
    private function getDocBlock(string $class): ?PhpDocNode
151
    {
152
        if (isset($this->docBlocks[$class])) {
66✔
153
            return $this->docBlocks[$class];
12✔
154
        }
155

156
        try {
157
            $reflectionClass = new \ReflectionClass($class);
66✔
158
        } catch (\ReflectionException) {
×
159
            return null;
×
160
        }
161

162
        $rawDocNode = $reflectionClass->getDocComment();
66✔
163

164
        if (!$rawDocNode) {
66✔
165
            return null;
54✔
166
        }
167

168
        $tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
24✔
169
        $phpDocNode = $this->phpDocParser->parse($tokens);
24✔
170
        $tokens->consumeTokenType(Lexer::TOKEN_END);
24✔
171

172
        return $this->docBlocks[$class] = $phpDocNode;
24✔
173
    }
174
}
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