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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

86.27
/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 PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
19
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocTextNode;
20
use PHPStan\PhpDocParser\Lexer\Lexer;
21
use PHPStan\PhpDocParser\Parser\ConstExprParser;
22
use PHPStan\PhpDocParser\Parser\PhpDocParser;
23
use PHPStan\PhpDocParser\Parser\TokenIterator;
24
use PHPStan\PhpDocParser\Parser\TypeParser;
25
use PHPStan\PhpDocParser\ParserConfig;
26

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

37
    /** @var array<string, PhpDocNode> */
38
    private array $docBlocks = [];
39

40
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated)
41
    {
42
        $phpDocParser = null;
588✔
43
        $lexer = null;
588✔
44
        if (class_exists(PhpDocParser::class) && class_exists(ParserConfig::class)) {
588✔
45
            $config = new ParserConfig([]);
588✔
46
            $phpDocParser = new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
588✔
47
            $lexer = new Lexer($config);
588✔
48
        } elseif (class_exists(PhpDocParser::class)) {
×
49
            $phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser()); // @phpstan-ignore-line
×
50
            $lexer = new Lexer(); // @phpstan-ignore-line
×
51
        }
52
        $this->phpDocParser = $phpDocParser;
588✔
53
        $this->lexer = $lexer;
588✔
54
    }
55

56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function create(string $resourceClass): ResourceMetadataCollection
60
    {
61
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
102✔
62

63
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
102✔
64
            if (null !== $resourceMetadata->getDescription()) {
100✔
65
                continue;
4✔
66
            }
67

68
            $description = $this->getShortDescription($resourceClass);
98✔
69

70
            if (!$description) {
98✔
71
                return $resourceMetadataCollection;
90✔
72
            }
73

74
            $resourceMetadataCollection[$key] = $resourceMetadata->withDescription($description);
16✔
75

76
            $operations = $resourceMetadata->getOperations() ?? new Operations();
16✔
77
            foreach ($operations as $operationName => $operation) {
16✔
78
                if (null !== $operation->getDescription()) {
16✔
79
                    continue;
×
80
                }
81

82
                $operations->add($operationName, $operation->withDescription($description));
16✔
83
            }
84

85
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withOperations($operations);
16✔
86

87
            if (!$resourceMetadata->getGraphQlOperations()) {
16✔
88
                continue;
8✔
89
            }
90

91
            foreach ($graphQlOperations = $resourceMetadata->getGraphQlOperations() as $operationName => $operation) {
16✔
92
                if (null !== $operation->getDescription()) {
16✔
93
                    continue;
16✔
94
                }
95

96
                $graphQlOperations[$operationName] = $operation->withDescription($description);
16✔
97
            }
98

99
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withGraphQlOperations($graphQlOperations);
16✔
100
        }
101

102
        return $resourceMetadataCollection;
32✔
103
    }
104

105
    /**
106
     * Gets the short description of the class.
107
     */
108
    private function getShortDescription(string $class): ?string
109
    {
110
        if (!$docBlock = $this->getDocBlock($class)) {
98✔
111
            return null;
90✔
112
        }
113

114
        foreach ($docBlock->children as $docChild) {
16✔
115
            if ($docChild instanceof PhpDocTextNode && !empty($docChild->text)) {
16✔
116
                return $docChild->text;
16✔
117
            }
118
        }
119

120
        return null;
×
121
    }
122

123
    private function getDocBlock(string $class): ?PhpDocNode
124
    {
125
        if (isset($this->docBlocks[$class])) {
98✔
126
            return $this->docBlocks[$class];
8✔
127
        }
128

129
        try {
130
            $reflectionClass = new \ReflectionClass($class);
98✔
131
        } catch (\ReflectionException) {
×
132
            return null;
×
133
        }
134

135
        $rawDocNode = $reflectionClass->getDocComment();
98✔
136

137
        if (!$rawDocNode) {
98✔
138
            return null;
90✔
139
        }
140

141
        $tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
16✔
142
        $phpDocNode = $this->phpDocParser->parse($tokens);
16✔
143
        $tokens->consumeTokenType(Lexer::TOKEN_END);
16✔
144

145
        return $this->docBlocks[$class] = $phpDocNode;
16✔
146
    }
147
}
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