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

api-platform / core / 14726067612

29 Apr 2025 07:47AM UTC coverage: 23.443% (+15.2%) from 8.252%
14726067612

push

github

web-flow
feat(symfony): Autoconfigure classes using `#[ApiResource]` attribute (#6943)

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

3578 existing lines in 159 files now uncovered.

11517 of 49127 relevant lines covered (23.44%)

54.29 hits per line

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

83.87
/src/Metadata/Parameters.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;
15

16
use ApiPlatform\Metadata\Exception\RuntimeException;
17

18
/**
19
 * A parameter dictionnary.
20
 *
21
 * @implements \IteratorAggregate<string, Parameter>
22
 */
23
final class Parameters implements \IteratorAggregate, \Countable
24
{
25
    /**
26
     * @var array<int, array{0: string, 1: Parameter}>
27
     */
28
    private array $parameters = [];
29

30
    /**
31
     * @param array<int|string, Parameter> $parameters
32
     */
33
    public function __construct(array $parameters = [])
34
    {
35
        foreach ($parameters as $parameterName => $parameter) {
61✔
36
            if ($parameter->getKey()) {
3✔
37
                $parameterName = $parameter->getKey();
2✔
38
            }
39

40
            $this->parameters[] = [$parameterName, $parameter];
3✔
41
        }
42

43
        $this->sort();
61✔
44
    }
45

46
    /**
47
     * @return \ArrayIterator<string, Parameter>
48
     */
49
    public function getIterator(): \Traversable
50
    {
51
        return (function (): \Generator {
841✔
52
            foreach ($this->parameters as [$parameterName, $parameter]) {
841✔
53
                yield $parameterName => $parameter;
808✔
54
            }
55
        })();
841✔
56
    }
57

58
    public function add(string $key, Parameter $value): self
59
    {
60
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
495✔
61
            if ($parameterName === $key && $value::class === $parameter::class) {
495✔
62
                $this->parameters[$i] = [$key, $value];
493✔
63

64
                return $this;
493✔
65
            }
66
        }
67

68
        $this->parameters[] = [$key, $value];
21✔
69

70
        return $this;
21✔
71
    }
72

73
    /**
74
     * @template T of Parameter
75
     *
76
     * @param class-string<T> $parameterClass
77
     */
78
    public function remove(string $key, string $parameterClass = QueryParameter::class): self
79
    {
80
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
2✔
81
            if ($parameterName === $key && $parameterClass === $parameter::class) {
2✔
82
                unset($this->parameters[$i]);
2✔
83

84
                return $this;
2✔
85
            }
86
        }
87

88
        throw new RuntimeException(\sprintf('Could not remove parameter "%s".', $key));
×
89
    }
90

91
    /**
92
     * @template T of Parameter
93
     *
94
     * @param class-string<T> $parameterClass
95
     *
96
     * @return T|null
97
     */
98
    public function get(string $key, string $parameterClass = QueryParameter::class): ?Parameter
99
    {
UNCOV
100
        foreach ($this->parameters as [$parameterName, $parameter]) {
×
UNCOV
101
            if ($parameterName === $key && $parameterClass === $parameter::class) {
×
UNCOV
102
                return $parameter;
×
103
            }
104
        }
105

106
        return null;
×
107
    }
108

109
    /**
110
     * @template T of Parameter
111
     *
112
     * @param class-string<T> $parameterClass
113
     */
114
    public function has(string $key, string $parameterClass = QueryParameter::class): bool
115
    {
116
        foreach ($this->parameters as [$parameterName, $parameter]) {
2✔
117
            if ($parameterName === $key && $parameterClass === $parameter::class) {
2✔
118
                return true;
2✔
119
            }
120
        }
121

122
        return false;
2✔
123
    }
124

125
    public function count(): int
126
    {
127
        return \count($this->parameters);
56✔
128
    }
129

130
    public function sort(): self
131
    {
132
        usort($this->parameters, static fn (array $a, array $b): int => $b[1]->getPriority() - $a[1]->getPriority());
61✔
133

134
        return $this;
61✔
135
    }
136
}
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

© 2026 Coveralls, Inc