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

api-platform / core / 15955912273

29 Jun 2025 01:51PM UTC coverage: 22.057% (-0.03%) from 22.082%
15955912273

Pull #7249

github

web-flow
Merge d9904d788 into a42034dc3
Pull Request #7249: chore: solve some phpstan issues

0 of 9 new or added lines in 8 files covered. (0.0%)

11540 existing lines in 372 files now uncovered.

11522 of 52237 relevant lines covered (22.06%)

11.08 hits per line

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

95.74
/src/Metadata/Resource/Factory/FormatsResourceMetadataCollectionFactory.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\CollectionOperationInterface;
17
use ApiPlatform\Metadata\ErrorResource;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use ApiPlatform\Metadata\Exception\ResourceClassNotFoundException;
20
use ApiPlatform\Metadata\HttpOperation;
21
use ApiPlatform\Metadata\Operations;
22
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
23

24
/**
25
 * Normalizes enabled formats.
26
 *
27
 * Formats hierarchy:
28
 * * resource formats
29
 *   * resource input/output formats
30
 *     * operation formats
31
 *       * operation input/output formats
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
final class FormatsResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
36
{
37
    public function __construct(
38
        private readonly ResourceMetadataCollectionFactoryInterface $decorated,
39
        private readonly array $formats,
40
        private readonly array $patchFormats,
41
        private readonly ?array $errorFormats = null,
42
    ) {
UNCOV
43
    }
291✔
44

45
    /**
46
     * Adds the formats attributes.
47
     *
48
     * @see UriTemplateResourceMetadataCollectionFactory
49
     *
50
     * @throws ResourceClassNotFoundException
51
     */
52
    public function create(string $resourceClass): ResourceMetadataCollection
53
    {
UNCOV
54
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
50✔
55

UNCOV
56
        foreach ($resourceMetadataCollection as $index => $resourceMetadata) {
50✔
UNCOV
57
            $rawResourceFormats = $resourceMetadata->getFormats();
49✔
UNCOV
58
            $resourceFormats = null === $rawResourceFormats ? $this->formats : $this->normalizeFormats($rawResourceFormats);
49✔
UNCOV
59
            $resourceInputFormats = $resourceMetadata->getInputFormats() ? $this->normalizeFormats($resourceMetadata->getInputFormats()) : $resourceFormats;
49✔
UNCOV
60
            $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : $resourceFormats;
49✔
UNCOV
61
            if ($resourceMetadata instanceof ErrorResource) {
49✔
UNCOV
62
                $resourceOutputFormats = $resourceMetadata->getOutputFormats() ? $this->normalizeFormats($resourceMetadata->getOutputFormats()) : ($this->errorFormats ?? []);
2✔
63
            }
64

UNCOV
65
            $resourceMetadataCollection[$index] = $resourceMetadataCollection[$index]->withOperations($this->normalize($resourceInputFormats, $resourceOutputFormats, $resourceMetadata->getOperations()));
49✔
66
        }
67

UNCOV
68
        return $resourceMetadataCollection;
50✔
69
    }
70

71
    /**
72
     * @param Operations<HttpOperation> $operations
73
     */
74
    private function normalize(array $resourceInputFormats, array $resourceOutputFormats, Operations $operations): Operations
75
    {
UNCOV
76
        $newOperations = [];
49✔
UNCOV
77
        $patchFormats = null;
49✔
UNCOV
78
        foreach ($operations as $operationName => $operation) {
49✔
UNCOV
79
            if ($operation->getFormats()) {
49✔
UNCOV
80
                $operation = $operation->withFormats($this->normalizeFormats($operation->getFormats()));
1✔
81
            }
82

UNCOV
83
            if (($isPatch = 'PATCH' === $operation->getMethod()) && !$operation->getFormats() && !$operation->getInputFormats()) {
49✔
UNCOV
84
                $operation = $operation->withInputFormats($this->patchFormats);
15✔
85
            }
86

UNCOV
87
            $operation = $operation->withInputFormats($operation->getInputFormats() ? $this->normalizeFormats($operation->getInputFormats()) : $operation->getFormats() ?? $resourceInputFormats);
49✔
UNCOV
88
            $operation = $operation->withOutputFormats($operation->getOutputFormats() ? $this->normalizeFormats($operation->getOutputFormats()) : $operation->getFormats() ?? $resourceOutputFormats);
49✔
89

UNCOV
90
            if ($isPatch) {
49✔
UNCOV
91
                $patchFormats = $operation->getInputFormats();
15✔
92
            }
93

UNCOV
94
            $newOperations[$operationName] = $operation;
49✔
95
        }
96

UNCOV
97
        if (!$patchFormats) {
49✔
UNCOV
98
            return new Operations($newOperations);
43✔
99
        }
100

101
        // Prepare an Accept-Patch header
UNCOV
102
        foreach ($newOperations as $operationName => $operation) {
15✔
UNCOV
103
            if ($operation instanceof CollectionOperationInterface) {
15✔
UNCOV
104
                continue;
15✔
105
            }
106

UNCOV
107
            $patchMimeTypes = [];
15✔
108

UNCOV
109
            foreach ($patchFormats as $mimeTypes) {
15✔
UNCOV
110
                foreach ($mimeTypes as $mimeType) {
15✔
UNCOV
111
                    $patchMimeTypes[] = $mimeType;
15✔
112
                }
113
            }
114

UNCOV
115
            $newOperations[$operationName] = $operation->withAcceptPatch(implode(', ', $patchMimeTypes));
15✔
116
        }
117

UNCOV
118
        return new Operations($newOperations);
15✔
119
    }
120

121
    /**
122
     * @throws InvalidArgumentException
123
     */
124
    private function normalizeFormats(array|string $currentFormats): array
125
    {
UNCOV
126
        $currentFormats = (array) $currentFormats;
19✔
127

UNCOV
128
        $normalizedFormats = [];
19✔
UNCOV
129
        foreach ($currentFormats as $format => $value) {
19✔
UNCOV
130
            if (!is_numeric($format)) {
19✔
UNCOV
131
                $normalizedFormats[$format] = (array) $value;
18✔
UNCOV
132
                continue;
18✔
133
            }
UNCOV
134
            if (!\is_string($value)) {
1✔
135
                throw new InvalidArgumentException(\sprintf("The 'formats' attributes value must be a string when trying to include an already configured format, %s given.", \gettype($value)));
×
136
            }
UNCOV
137
            if (\array_key_exists($value, $this->formats)) {
1✔
UNCOV
138
                $normalizedFormats[$value] = $this->formats[$value];
1✔
UNCOV
139
                continue;
1✔
140
            }
141

142
            throw new InvalidArgumentException(\sprintf("You either need to add the format '%s' to your project configuration or declare a mime type for it in your annotation.", $value));
×
143
        }
144

UNCOV
145
        return $normalizedFormats;
19✔
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