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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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;
1,214✔
48
        if ($docBlockFactory instanceof DocBlockFactoryInterface) {
1,214✔
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)) {
1,214✔
52
            $docBlockFactory = $docBlockFactory ?? DocBlockFactory::createInstance();
1,214✔
53
            $contextFactory = new ContextFactory();
1,214✔
54
        }
55
        $this->docBlockFactory = $docBlockFactory;
1,214✔
56
        $this->contextFactory = $contextFactory;
1,214✔
57
        if (class_exists(DocBlockFactory::class) && !class_exists(PhpDocParser::class)) {
1,214✔
58
            trigger_deprecation('api-platform/core', '3.1', 'Using phpdocumentor/reflection-docblock is deprecated. Require phpstan/phpdoc-parser instead.');
×
59
        }
60
        $phpDocParser = null;
1,214✔
61
        $lexer = null;
1,214✔
62
        if (class_exists(PhpDocParser::class) && class_exists(ParserConfig::class)) {
1,214✔
63
            $config = new ParserConfig([]);
1,214✔
64
            $phpDocParser = new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
1,214✔
65
            $lexer = new Lexer($config);
1,214✔
66
        } elseif (class_exists(PhpDocParser::class)) {
×
67
            $phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser()); // @phpstan-ignore-line
×
68
            $lexer = new Lexer(); // @phpstan-ignore-line
×
69
        }
70
        $this->phpDocParser = $phpDocParser;
1,214✔
71
        $this->lexer = $lexer;
1,214✔
72
    }
73

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

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

86
            $description = null;
94✔
87

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

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

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

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

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

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

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

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

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

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

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

134
        return $resourceMetadataCollection;
36✔
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)) {
94✔
143
            return null;
79✔
144
        }
145

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

UNCOV
152
        return null;
1✔
153
    }
154

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

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

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

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

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

177
        return $this->docBlocks[$class] = $phpDocNode;
25✔
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