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

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

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

93.94
/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) {
411✔
36
            if ($parameter->getKey()) {
36✔
37
                $parameterName = $parameter->getKey();
2✔
38
            }
39

40
            $key = \sprintf('%s.%s', $parameter::class, $parameterName);
36✔
41

42
            $this->parameters[$key] = [$parameterName, $parameter];
36✔
43
        }
44

45
        $this->parameters = array_values($this->parameters);
411✔
46

47
        $this->sort();
411✔
48
    }
49

50
    /**
51
     * @return \ArrayIterator<string, Parameter>
52
     */
53
    public function getIterator(): \Traversable
54
    {
55
        return (function (): \Generator {
723✔
56
            foreach ($this->parameters as [$parameterName, $parameter]) {
723✔
57
                yield $parameterName => $parameter;
600✔
58
            }
59
        })();
723✔
60
    }
61

62
    public function add(string $key, Parameter $value): self
63
    {
64
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
276✔
65
            if ($parameterName === $key && $value::class === $parameter::class) {
54✔
66
                $this->parameters[$i] = [$key, $value];
46✔
67

68
                return $this;
46✔
69
            }
70
        }
71

72
        $this->parameters[] = [$key, $value];
254✔
73

74
        return $this;
254✔
75
    }
76

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

88
                return $this;
2✔
89
            }
90
        }
91

92
        throw new RuntimeException(\sprintf('Could not remove parameter "%s".', $key));
×
93
    }
94

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

110
        return null;
×
111
    }
112

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

126
        return false;
4✔
127
    }
128

129
    public function count(): int
130
    {
131
        return \count($this->parameters);
344✔
132
    }
133

134
    public function sort(): self
135
    {
136
        usort($this->parameters, static fn (array $a, array $b): int => $b[1]->getPriority() - $a[1]->getPriority());
411✔
137

138
        return $this;
411✔
139
    }
140
}
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