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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

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

97.22
/src/Metadata/Resource/ResourceMetadataCollection.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;
15

16
use ApiPlatform\Metadata\ApiResource;
17
use ApiPlatform\Metadata\CollectionOperationInterface;
18
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
19
use ApiPlatform\Metadata\Operation;
20

21
/**
22
 * @extends \ArrayObject<int, ApiResource>
23
 */
24
final class ResourceMetadataCollection extends \ArrayObject
25
{
26
    private const GRAPHQL_PREFIX = 'g_';
27
    private const HTTP_PREFIX = 'h_';
28
    private const FORCE_COLLECTION = 'co_';
29
    private const HTTP_OPERATION = 'ht_';
30

31
    private array $operationCache = [];
32

33
    public function __construct(private readonly string $resourceClass, array $input = [])
34
    {
UNCOV
35
        parent::__construct($input);
1,034✔
36
    }
37

38
    public function getOperation(?string $operationName = null, bool $forceCollection = false, bool $httpOperation = false, bool $forceGraphQl = false): Operation
39
    {
UNCOV
40
        $operationName ??= '';
998✔
UNCOV
41
        $cachePrefix = ($forceCollection ? self::FORCE_COLLECTION : '').($httpOperation ? self::HTTP_OPERATION : '');
998✔
UNCOV
42
        $httpCacheKey = self::HTTP_PREFIX.$cachePrefix.$operationName;
998✔
UNCOV
43
        if (isset($this->operationCache[$httpCacheKey])) {
998✔
UNCOV
44
            return $this->operationCache[$httpCacheKey];
42✔
45
        }
46

UNCOV
47
        $gqlCacheKey = self::GRAPHQL_PREFIX.$cachePrefix.$operationName;
998✔
UNCOV
48
        if (isset($this->operationCache[$gqlCacheKey])) {
998✔
49
            return $this->operationCache[$gqlCacheKey];
×
50
        }
51

UNCOV
52
        $it = $this->getIterator();
998✔
UNCOV
53
        $metadata = null;
998✔
54

UNCOV
55
        while ($it->valid()) {
998✔
56
            /** @var ApiResource $metadata */
UNCOV
57
            $metadata = $it->current();
997✔
58

UNCOV
59
            if (!$forceGraphQl) {
997✔
UNCOV
60
                foreach ($metadata->getOperations() ?? [] as $name => $operation) {
997✔
UNCOV
61
                    $isCollection = $operation instanceof CollectionOperationInterface;
994✔
UNCOV
62
                    $method = $operation->getMethod() ?? 'GET';
994✔
UNCOV
63
                    $isGetOperation = 'GET' === $method || 'OPTIONS' === $method || 'HEAD' === $method;
994✔
UNCOV
64
                    if ('' === $operationName && $isGetOperation && ($forceCollection ? $isCollection : !$isCollection)) {
994✔
UNCOV
65
                        return $this->operationCache[$httpCacheKey] = $operation;
892✔
66
                    }
67

UNCOV
68
                    if ($name === $operationName) {
953✔
UNCOV
69
                        return $this->operationCache[$httpCacheKey] = $operation;
850✔
70
                    }
71

UNCOV
72
                    if ($operation->getUriTemplate() === $operationName) {
840✔
UNCOV
73
                        return $this->operationCache[$httpCacheKey] = $operation;
5✔
74
                    }
75
                }
76
            }
77

UNCOV
78
            foreach ($metadata->getGraphQlOperations() ?? [] as $name => $operation) {
222✔
UNCOV
79
                $isCollection = $operation instanceof CollectionOperationInterface;
217✔
UNCOV
80
                if ('' === $operationName && ($forceCollection ? $isCollection : !$isCollection) && false === $httpOperation) {
217✔
81
                    return $this->operationCache[$gqlCacheKey] = $operation;
48✔
82
                }
83

UNCOV
84
                if ($name === $operationName) {
196✔
UNCOV
85
                    return $this->operationCache[$httpCacheKey] = $operation;
146✔
86
                }
87
            }
88

UNCOV
89
            $it->next();
61✔
90
        }
91

92
        // Idea:
93
        // if ($metadata) {
94
        //     return (new class extends HttpOperation {})->withResource($metadata);
95
        // }
96

UNCOV
97
        $this->handleNotFound($operationName, $metadata);
15✔
98
    }
99

100
    /**
101
     * @throws OperationNotFoundException
102
     */
103
    private function handleNotFound(string $operationName, ?ApiResource $metadata): void
104
    {
105
        // Hide the FQDN in the exception message if possible
UNCOV
106
        $shortName = $metadata?->getShortName() ? $metadata->getShortName() : $this->resourceClass;
15✔
UNCOV
107
        if (!$metadata && false !== $pos = strrpos($shortName, '\\')) {
15✔
UNCOV
108
            $shortName = substr($shortName, $pos + 1);
6✔
109
        }
110

UNCOV
111
        throw new OperationNotFoundException(\sprintf('Operation "%s" not found for resource "%s".', $operationName, $shortName));
15✔
112
    }
113
}
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