PriorityList   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 44
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A insert() 0 11 2
A getIterator() 0 8 3
1
<?php
2
3
/**
4
 * This file is part of GitterBot package.
5
 *
6
 * @author Serafim <[email protected]>
7
 * @date 25.09.2015 14:47
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace Interfaces\Gitter\Support;
14
15
/**
16
 * Class PriorityList
17
 */
18
class PriorityList implements \IteratorAggregate
19
{
20
    /**
21
     * @var []
22
     */
23
    protected $list = [];
24
25
    /**
26
     * @return PriorityList
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
27
     */
28
    public function __construct()
29
    {
30
        $this->list = [];
31
    }
32
33
    /**
34
     * @param $value
35
     * @param int $priority
36
     * @return $this
37
     */
38
    public function insert($value, $priority = 0)
39
    {
40
        if (!array_key_exists($priority, $this->list)) {
41
            $this->list[$priority] = [];
42
            krsort($this->list);
43
        }
44
45
        $this->list[$priority][] = $value;
46
47
        return $this;
48
    }
49
50
    /**
51
     * @return \Generator
52
     */
53
    public function getIterator()
54
    {
55
        foreach ($this->list as $index => $priority) {
56
            foreach ($priority as $item) {
57
                yield $priority => $item;
58
            }
59
        }
60
    }
61
}
62