PrioritizedList   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
eloc 13
dl 0
loc 48
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 16 4
A add() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
11
 *  - (c) John MacFarlane
12
 *
13
 * For the full copyright and license information, please view the LICENSE
14
 * file that was distributed with this source code.
15
 */
16
17
namespace League\CommonMark\Util;
18
19
/**
20
 * @internal
21
 *
22
 * @phpstan-template T
23
 * @phpstan-implements \IteratorAggregate<T>
24
 */
25
final class PrioritizedList implements \IteratorAggregate
26
{
27
    /**
28
     * @var array<int, array<mixed>>
29
     *
30
     * @phpstan-var array<int, array<T>>
31
     */
32
    private $list = [];
33
34
    /**
35
     * @var \Traversable<mixed>|null
36
     *
37
     * @phpstan-var \Traversable<T>|null
38
     */
39
    private $optimized;
40
41
    /**
42
     * @param mixed $item
43
     *
44
     * @phpstan-param T $item
45
     */
46 2889
    public function add($item, int $priority): void
47
    {
48 2889
        $this->list[$priority][] = $item;
49 2889
        $this->optimized         = null;
50 2889
    }
51
52
    /**
53
     * @return \Traversable<int, mixed>
54
     *
55
     * @phpstan-return \Traversable<int, T>
56
     */
57 2898
    public function getIterator(): \Traversable
58
    {
59 2898
        if ($this->optimized === null) {
60 2898
            \krsort($this->list);
61
62 2898
            $sorted = [];
63 2898
            foreach ($this->list as $group) {
64 2874
                foreach ($group as $item) {
65 2874
                    $sorted[] = $item;
66
                }
67
            }
68
69 2898
            $this->optimized = new \ArrayIterator($sorted);
70
        }
71
72 2898
        return $this->optimized;
73
    }
74
}
75