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

api-platform / core / 14854964964

06 May 2025 08:21AM UTC coverage: 6.873% (-1.6%) from 8.459%
14854964964

Pull #7122

github

web-flow
Merge c6177db2c into f55606b01
Pull Request #7122: Cherry picks from main (deprecations)

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

2031 existing lines in 148 files now uncovered.

10887 of 158407 relevant lines covered (6.87%)

6.19 hits per line

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

91.67
/src/Metadata/Resource/Factory/InputOutputResourceMetadataCollectionFactory.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\ApiResource;
17
use ApiPlatform\Metadata\HttpOperation;
18
use ApiPlatform\Metadata\Operations;
19
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
20

21
/**
22
 * Transforms the given input/output metadata to a normalized one.
23
 *
24
 * @author Antoine Bluchet <soyuka@gmail.com>
25
 */
26
final class InputOutputResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
27
{
28
    public function __construct(private readonly ResourceMetadataCollectionFactoryInterface $decorated)
29
    {
30
    }
510✔
31

32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function create(string $resourceClass): ResourceMetadataCollection
36
    {
37
        $resourceMetadataCollection = $this->decorated->create($resourceClass);
74✔
38

39
        foreach ($resourceMetadataCollection as $key => $resourceMetadata) {
74✔
40
            $resourceMetadata = $resourceMetadata->withInput($this->transformInputOutput($resourceMetadata->getInput()));
72✔
41
            $resourceMetadata = $resourceMetadata->withOutput($this->transformInputOutput($resourceMetadata->getOutput()));
72✔
42

43
            if ($resourceMetadata->getOperations()) {
72✔
44
                $resourceMetadata = $resourceMetadata->withOperations($this->getTransformedOperations($resourceMetadata->getOperations(), $resourceMetadata));
72✔
45
            }
46

47
            if ($resourceMetadata->getGraphQlOperations()) {
72✔
48
                $resourceMetadata = $resourceMetadata->withGraphQlOperations($this->getTransformedOperations($resourceMetadata->getGraphQlOperations(), $resourceMetadata));
56✔
49
            }
50

51
            $resourceMetadataCollection[$key] = $resourceMetadata;
72✔
52
        }
53

54
        return $resourceMetadataCollection;
74✔
55
    }
56

57
    private function getTransformedOperations(Operations|array $operations, ApiResource $resourceMetadata): Operations|array
58
    {
59
        foreach ($operations as $key => $operation) {
72✔
60
            $operation = $operation->withInput(null !== $operation->getInput() ? $this->transformInputOutput($operation->getInput()) : $resourceMetadata->getInput());
72✔
61
            $operation = $operation->withOutput(null !== $operation->getOutput() ? $this->transformInputOutput($operation->getOutput()) : $resourceMetadata->getOutput());
72✔
62

63
            if (
64
                $operation->getInput()
72✔
65
                && \array_key_exists('class', $operation->getInput())
72✔
66
                && null === $operation->getInput()['class']
72✔
67
            ) {
UNCOV
68
                $operation = $operation->withDeserialize(null === $operation->canDeserialize() ? false : $operation->canDeserialize());
×
UNCOV
69
                $operation = $operation->withValidate(null === $operation->canValidate() ? false : $operation->canValidate());
×
70
            }
71

72
            if (
73
                $operation instanceof HttpOperation
72✔
74
                && $operation->getOutput()
72✔
75
                && \array_key_exists('class', $operation->getOutput())
72✔
76
                && null === $operation->getOutput()['class']
72✔
77
                && null === $operation->getStatus()
72✔
78
            ) {
UNCOV
79
                $operation = $operation->withStatus(204);
×
80
            }
81

82
            $operations instanceof Operations ? $operations->add($key, $operation) : $operations[$key] = $operation;
72✔
83
        }
84

85
        return $operations;
72✔
86
    }
87

88
    private function transformInputOutput(mixed $attribute): ?array
89
    {
90
        if (false === $attribute) {
72✔
91
            return ['class' => null];
30✔
92
        }
93

94
        if (!$attribute) {
72✔
95
            return null;
72✔
96
        }
97

98
        if (\is_string($attribute)) {
8✔
99
            $attribute = ['class' => $attribute];
8✔
100
        }
101

102
        if (!isset($attribute['name']) && isset($attribute['class'])) {
8✔
103
            $attribute['name'] = (new \ReflectionClass($attribute['class']))->getShortName();
8✔
104
        }
105

106
        return $attribute;
8✔
107
    }
108
}
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