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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

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

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 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
    private array $parameters = [];
26

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

UNCOV
37
            $this->parameters[] = [$parameterName, $parameter];
3✔
38
        }
39

UNCOV
40
        $this->sort();
42✔
41
    }
42

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

55
    public function add(string $key, Parameter $value): self
56
    {
UNCOV
57
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
638✔
UNCOV
58
            if ($parameterName === $key && $value::class === $parameter::class) {
637✔
UNCOV
59
                $this->parameters[$i] = [$key, $value];
635✔
60

UNCOV
61
                return $this;
635✔
62
            }
63
        }
64

UNCOV
65
        $this->parameters[] = [$key, $value];
13✔
66

UNCOV
67
        return $this;
13✔
68
    }
69

70
    /**
71
     * @param class-string $parameterClass
72
     */
73
    public function remove(string $key, string $parameterClass): self
74
    {
UNCOV
75
        foreach ($this->parameters as $i => [$parameterName, $parameter]) {
3✔
UNCOV
76
            if ($parameterName === $key && $parameterClass === $parameter::class) {
3✔
UNCOV
77
                unset($this->parameters[$i]);
3✔
78

UNCOV
79
                return $this;
3✔
80
            }
81
        }
82

83
        throw new RuntimeException(\sprintf('Could not remove parameter "%s".', $key));
×
84
    }
85

86
    /**
87
     * @param class-string $parameterClass
88
     */
89
    public function get(string $key, string $parameterClass): ?Parameter
90
    {
91
        foreach ($this->parameters as [$parameterName, $parameter]) {
×
92
            if ($parameterName === $key && $parameterClass === $parameter::class) {
×
93
                return $parameter;
×
94
            }
95
        }
96

97
        return null;
×
98
    }
99

100
    /**
101
     * @param class-string $parameterClass
102
     */
103
    public function has(string $key, string $parameterClass): bool
104
    {
UNCOV
105
        foreach ($this->parameters as [$parameterName, $parameter]) {
3✔
UNCOV
106
            if ($parameterName === $key && $parameterClass === $parameter::class) {
3✔
UNCOV
107
                return true;
3✔
108
            }
109
        }
110

UNCOV
111
        return false;
3✔
112
    }
113

114
    public function count(): int
115
    {
UNCOV
116
        return \count($this->parameters);
34✔
117
    }
118

119
    public function sort(): self
120
    {
UNCOV
121
        usort($this->parameters, fn ($a, $b): int|float => $b[1]->getPriority() - $a[1]->getPriority());
42✔
122

UNCOV
123
        return $this;
42✔
124
    }
125
}
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