Completed
Push — master ( c68737...8a4625 )
by Colin
02:48 queued 11s
created

PrioritizedList   A

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
final class PrioritizedList implements \IteratorAggregate
18
{
19
    private $list = [];
20
21
    private $optimized;
22
23
    /**
24
     * @param mixed $item
25
     * @param int   $priority
26
     */
27 1989
    public function add($item, $priority)
28
    {
29 1989
        $this->list[$priority][] = $item;
30 1989
        $this->optimized = null;
31 1989
    }
32
33
    /**
34
     * @return \Traversable
35
     */
36 2007
    public function getIterator()
37
    {
38 2007
        if ($this->optimized === null) {
39 2007
            krsort($this->list);
40
41 2007
            $sorted = [];
42 2007
            foreach ($this->list as $group) {
43 1989
                foreach ($group as $item) {
44 1989
                    $sorted[] = $item;
45
                }
46
            }
47
48 2007
            $this->optimized = new \ArrayIterator($sorted);
49
        }
50
51 2007
        return $this->optimized;
52
    }
53
}
54