PrioritizedList   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

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
     * @phpstan-var array<int, array<T>>
30
     */
31
    private $list = [];
32
33
    /**
34
     * @var \Traversable<mixed>|null
35
     * @phpstan-var \Traversable<T>|null
36
     */
37
    private $optimized;
38
39
    /**
40
     * @param mixed $item
41
     *
42
     * @phpstan-param T $item
43
     */
44 3099
    public function add($item, int $priority): void
45
    {
46 3099
        $this->list[$priority][] = $item;
47 3099
        $this->optimized         = null;
48 3099
    }
49
50
    /**
51
     * @return \Traversable<int, mixed>
52
     *
53
     * @phpstan-return \Traversable<int, T>
54
     */
55 3069
    public function getIterator(): \Traversable
56
    {
57 3069
        if ($this->optimized === null) {
58 3069
            \krsort($this->list);
59
60 3069
            $sorted = [];
61 3069
            foreach ($this->list as $group) {
62 3042
                foreach ($group as $item) {
63 3042
                    $sorted[] = $item;
64
                }
65
            }
66
67 3069
            $this->optimized = new \ArrayIterator($sorted);
68
        }
69
70 3069
        return $this->optimized;
71
    }
72
}
73