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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

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

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

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

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

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

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

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

70
        return $this;
184✔
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
    {
100
        foreach ($this->parameters as [$parameterName, $parameter]) {
14✔
101
            if ($parameterName === $key && $parameterClass === $parameter::class) {
14✔
102
                return $parameter;
14✔
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]) {
4✔
117
            if ($parameterName === $key && $parameterClass === $parameter::class) {
4✔
118
                return true;
2✔
119
            }
120
        }
121

122
        return false;
4✔
123
    }
124

125
    public function count(): int
126
    {
127
        return \count($this->parameters);
286✔
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());
304✔
133

134
        return $this;
304✔
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