• 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

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) {
216✔
34
            // When we use an int-indexed array in the constructor, compute priorities
35
            if (\is_int($operationName) && null === $operation->getPriority()) {
206✔
36
                $operation = $operation->withPriority($operationName);
66✔
37
                $operationName = (string) $operationName;
66✔
38
            }
39

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

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

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

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

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

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

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

74
        return $this;
106✔
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);
28✔
104
    }
105

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

110
        return $this;
216✔
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

© 2025 Coveralls, Inc