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

api-platform / core / 17723449516

15 Sep 2025 05:52AM UTC coverage: 0.0% (-22.6%) from 22.578%
17723449516

Pull #7383

github

web-flow
Merge fa5b61e35 into 949c3c975
Pull Request #7383: fix(metadata): compute isWritable during updates

0 of 6 new or added lines in 4 files covered. (0.0%)

11356 existing lines in 371 files now uncovered.

0 of 48868 relevant lines covered (0.0%)

0.0 hits per line

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

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

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

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

UNCOV
68
            $description = $this->getShortDescription($resourceClass);
×
69

UNCOV
70
            if (!$description) {
×
UNCOV
71
                return $resourceMetadataCollection;
×
72
            }
73

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

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

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

UNCOV
85
            $resourceMetadataCollection[$key] = $resourceMetadataCollection[$key]->withOperations($operations);
×
86

UNCOV
87
            if (!$resourceMetadata->getGraphQlOperations()) {
×
UNCOV
88
                continue;
×
89
            }
90

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

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

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

UNCOV
102
        return $resourceMetadataCollection;
×
103
    }
104

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

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

120
        return null;
×
121
    }
122

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

UNCOV
129
        if (!$this->phpDocParser || !$this->lexer) {
×
130
            return null;
×
131
        }
132

133
        try {
UNCOV
134
            $reflectionClass = new \ReflectionClass($class);
×
135
        } catch (\ReflectionException) {
×
136
            return null;
×
137
        }
138

UNCOV
139
        $rawDocNode = $reflectionClass->getDocComment();
×
UNCOV
140
        if (!$rawDocNode) {
×
UNCOV
141
            return null;
×
142
        }
143

UNCOV
144
        $tokens = new TokenIterator($this->lexer->tokenize($rawDocNode));
×
UNCOV
145
        $phpDocNode = $this->phpDocParser->parse($tokens);
×
UNCOV
146
        $tokens->consumeTokenType(Lexer::TOKEN_END);
×
147

UNCOV
148
        return $this->docBlocks[$class] = $phpDocNode;
×
149
    }
150
}
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