• 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

58.97
/src/Doctrine/Common/State/LinksHandlerTrait.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\Doctrine\Common\State;
15

16
use ApiPlatform\Metadata\Exception\OperationNotFoundException;
17
use ApiPlatform\Metadata\Exception\RuntimeException;
18
use ApiPlatform\Metadata\GraphQl\Operation as GraphQlOperation;
19
use ApiPlatform\Metadata\GraphQl\Query;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
23

24
trait LinksHandlerTrait
25
{
26
    private ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory;
27

28
    /**
29
     * @param array{linkClass?: string, linkProperty?: string}&array<string, mixed> $context
30
     *
31
     * @return \ApiPlatform\Metadata\Link[]
32
     */
33
    private function getLinks(string $resourceClass, Operation $operation, array $context): array
34
    {
UNCOV
35
        $links = $this->getOperationLinks($operation);
260✔
36

UNCOV
37
        if (!($linkClass = $context['linkClass'] ?? false)) {
260✔
UNCOV
38
            return $links;
251✔
39
        }
40

41
        $newLink = null;
20✔
42
        $linkProperty = $context['linkProperty'] ?? null;
20✔
43

44
        foreach ($links as $link) {
20✔
45
            if ($linkClass === $link->getFromClass() && $linkProperty === $link->getFromProperty()) {
18✔
46
                $newLink = $link;
×
47
                break;
×
48
            }
49
        }
50

51
        if ($newLink) {
20✔
52
            return [$newLink];
×
53
        }
54

55
        if (!$this->resourceMetadataCollectionFactory) {
20✔
56
            return [];
×
57
        }
58

59
        // Using GraphQL, it's possible that we won't find a GraphQL Operation of the same type (e.g. it is disabled).
60
        try {
61
            $resourceMetadataCollection = $this->resourceMetadataCollectionFactory->create($linkClass);
20✔
62
            $linkedOperation = $resourceMetadataCollection->getOperation($operation->getName());
20✔
63
        } catch (OperationNotFoundException $e) {
×
64
            if (!$operation instanceof GraphQlOperation) {
×
65
                throw $e;
×
66
            }
67

68
            // Instead, we'll look for the first Query available.
69
            foreach ($resourceMetadataCollection as $resourceMetadata) {
×
70
                foreach ($resourceMetadata->getGraphQlOperations() as $op) {
×
71
                    if ($op instanceof Query) {
×
72
                        $linkedOperation = $op;
×
73
                    }
74
                }
75
            }
76
        }
77

78
        foreach ($this->getOperationLinks($linkedOperation ?? null) as $link) {
20✔
79
            if ($resourceClass === $link->getToClass() && $linkProperty === $link->getFromProperty()) {
20✔
80
                $newLink = $link;
20✔
81
                break;
20✔
82
            }
83
        }
84

85
        if (!$newLink) {
20✔
86
            throw new RuntimeException(\sprintf('The class "%s" cannot be retrieved from "%s".', $resourceClass, $linkClass));
×
87
        }
88

89
        return [$newLink];
20✔
90
    }
91

92
    /**
93
     * @param array<int|string,mixed> $identifiers
94
     */
95
    private function getIdentifierValue(array &$identifiers, ?string $name = null): mixed
96
    {
UNCOV
97
        if (null !== $name && isset($identifiers[$name])) {
260✔
98
            $value = $identifiers[$name];
×
99
            unset($identifiers[$name]);
×
100

101
            return $value;
×
102
        }
103

UNCOV
104
        return array_shift($identifiers);
260✔
105
    }
106

107
    /**
108
     * @return \ApiPlatform\Metadata\Link[]|array
109
     */
110
    private function getOperationLinks(?Operation $operation = null): array
111
    {
UNCOV
112
        if ($operation instanceof GraphQlOperation) {
260✔
113
            return $operation->getLinks() ?? [];
20✔
114
        }
115

UNCOV
116
        if ($operation instanceof HttpOperation) {
251✔
UNCOV
117
            return $operation->getUriVariables() ?? [];
251✔
118
        }
119

120
        return [];
×
121
    }
122
}
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