PrioritizedList   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 0
dl 0
loc 37
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

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