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

api-platform / core / 8518363434

02 Apr 2024 06:23AM UTC coverage: 57.102% (-0.01%) from 57.113%
8518363434

push

github

web-flow
fix(metadata): index operations (#6272)

3 of 3 new or added lines in 1 file covered. (100.0%)

7 existing lines in 2 files now uncovered.

9906 of 17348 relevant lines covered (57.1%)

47.4 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
final class Operations implements \IteratorAggregate, \Countable
20
{
21
    private array $operations = [];
22

23
    /**
24
     * @param array<string|int, Operation> $operations
25
     */
26
    public function __construct(array $operations = [])
27
    {
28
        foreach ($operations as $operationName => $operation) {
684✔
29
            // When we use an int-indexed array in the constructor, compute priorities
30
            if (\is_int($operationName) && null === $operation->getPriority()) {
664✔
31
                $operation = $operation->withPriority($operationName);
64✔
32
                $operationName = (string) $operationName;
64✔
33
            }
34

35
            if ($operation->getName()) {
664✔
36
                $operationName = $operation->getName();
192✔
37
            }
38

39
            $this->operations[] = [$operationName, $operation];
664✔
40
        }
41

42
        $this->sort();
684✔
43
    }
44

45
    public function getIterator(): \Traversable
46
    {
47
        return (function (): \Generator {
808✔
48
            foreach ($this->operations as [$operationName, $operation]) {
808✔
49
                yield $operationName => $operation;
800✔
50
            }
51
        })();
808✔
52
    }
53

54
    public function add(string $key, Operation $value): self
55
    {
56
        foreach ($this->operations as $i => [$operationName, $operation]) {
52✔
57
            if ($operationName === $key) {
52✔
58
                $this->operations[$i] = [$key, $value];
40✔
59

60
                return $this;
40✔
61
            }
62
        }
63

64
        $this->operations[] = [$key, $value];
52✔
65

66
        return $this;
52✔
67
    }
68

69
    public function remove(string $key): self
70
    {
UNCOV
71
        foreach ($this->operations as $i => [$operationName, $operation]) {
×
UNCOV
72
            if ($operationName === $key) {
×
UNCOV
73
                unset($this->operations[$i]);
×
74

UNCOV
75
                return $this;
×
76
            }
77
        }
78

79
        throw new \RuntimeException(sprintf('Could not remove operation "%s".', $key));
×
80
    }
81

82
    public function has(string $key): bool
83
    {
84
        foreach ($this->operations as $i => [$operationName, $operation]) {
×
85
            if ($operationName === $key) {
×
86
                return true;
×
87
            }
88
        }
89

90
        return false;
×
91
    }
92

93
    public function count(): int
94
    {
95
        return \count($this->operations);
32✔
96
    }
97

98
    public function sort(): self
99
    {
100
        usort($this->operations, fn ($a, $b): int|float => $a[1]->getPriority() - $b[1]->getPriority());
684✔
101

102
        return $this;
684✔
103
    }
104
}
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