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

api-platform / core / 15779211245

20 Jun 2025 12:43PM UTC coverage: 22.501% (-0.006%) from 22.507%
15779211245

push

github

web-flow
fix: getcontainer return type (#7230)

11071 of 49202 relevant lines covered (22.5%)

21.78 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;
562✔
43
        $lexer = null;
562✔
44
        if (class_exists(PhpDocParser::class) && class_exists(ParserConfig::class)) {
562✔
45
            $config = new ParserConfig([]);
562✔
46
            $phpDocParser = new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
562✔
47
            $lexer = new Lexer($config);
562✔
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;
562✔
53
        $this->lexer = $lexer;
562✔
54
    }
55

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

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

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

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

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

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

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

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

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

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

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

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

102
        return $resourceMetadataCollection;
18✔
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)) {
82✔
111
            return null;
78✔
112
        }
113

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

120
        return null;
×
121
    }
122

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

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

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

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

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

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