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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 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
    {
UNCOV
35
        foreach ($parameters as $parameterName => $parameter) {
72✔
UNCOV
36
            if ($parameter->getKey()) {
15✔
UNCOV
37
                $parameterName = $parameter->getKey();
2✔
38
            }
39

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

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

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

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

UNCOV
64
                return $this;
377✔
65
            }
66
        }
67

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

UNCOV
70
        return $this;
14✔
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
    {
UNCOV
80
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
2✔
UNCOV
81
            if ($parameterName === $key && $parameterClass === $parameter::class) {
2✔
UNCOV
82
                unset($this->parameters[$i]);
2✔
83

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

UNCOV
122
        return false;
3✔
123
    }
124

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

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

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

© 2025 Coveralls, Inc