• 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

70.0
/src/Metadata/Operations.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
/**
17
 * An Operation dictionnary.
18
 *
19
 * @template-covariant T of Operation
20
 */
21
final class Operations implements \IteratorAggregate, \Countable
22
{
23
    /**
24
     * @var list<array{0: string, 1: T}>
25
     */
26
    private array $operations = [];
27

28
    /**
29
     * @param list<T>|array<string, T> $operations
30
     */
31
    public function __construct(array $operations = [])
32
    {
33
        foreach ($operations as $operationName => $operation) {
224✔
34
            // When we use an int-indexed array in the constructor, compute priorities
35
            if (\is_int($operationName) && null === $operation->getPriority()) {
214✔
36
                $operation = $operation->withPriority($operationName);
78✔
37
                $operationName = (string) $operationName;
78✔
38
            }
39

40
            if ($operation->getName()) {
214✔
41
                $operationName = $operation->getName();
134✔
42
            }
43

44
            $this->operations[] = [$operationName, $operation];
214✔
45
        }
46

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

50
    /**
51
     * @return \Iterator<string, T>
52
     */
53
    public function getIterator(): \Traversable
54
    {
55
        return (function (): \Generator {
890✔
56
            foreach ($this->operations as [$operationName, $operation]) {
890✔
57
                yield $operationName => $operation;
886✔
58
            }
59
        })();
890✔
60
    }
61

62
    public function add(string $key, Operation $value): self
63
    {
64
        foreach ($this->operations as $i => [$operationName, $operation]) {
128✔
65
            if ($operationName === $key) {
128✔
66
                $this->operations[$i] = [$key, $value];
122✔
67

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

72
        $this->operations[] = [$key, $value];
128✔
73

74
        return $this;
128✔
75
    }
76

77
    public function remove(string $key): self
78
    {
79
        foreach ($this->operations as $i => [$operationName, $operation]) {
×
80
            if ($operationName === $key) {
×
81
                unset($this->operations[$i]);
×
82

83
                return $this;
×
84
            }
85
        }
86

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

90
    public function has(string $key): bool
91
    {
92
        foreach ($this->operations as $i => [$operationName, $operation]) {
×
93
            if ($operationName === $key) {
×
94
                return true;
×
95
            }
96
        }
97

98
        return false;
×
99
    }
100

101
    public function count(): int
102
    {
103
        return \count($this->operations);
60✔
104
    }
105

106
    public function sort(): self
107
    {
108
        usort($this->operations, fn ($a, $b): int => $a[1]->getPriority() - $b[1]->getPriority());
224✔
109

110
        return $this;
224✔
111
    }
112
}
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