Completed
Pull Request — master (#10)
by Tomáš
04:50 queued 01:55
created

SourceSet   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 72
ccs 22
cts 24
cp 0.9167
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A containsSource() 0 4 1
A mergeSource() 0 9 2
A allSources() 0 4 1
A updatedSources() 0 6 1
A newSources() 0 4 1
A reset() 0 8 2
1
<?php
2
3
/*
4
 * This file is a part of Sculpin.
5
 *
6
 * (c) Dragonfly Development Inc.
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Symplify\PHP7_Sculpin\Source;
13
14
final class SourceSet
15
{
16
    /**
17
     * @var array
18
     */
19
    private $sources = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $newSources = [];
25
26 6
    public function __construct(array $sources = [])
27
    {
28 6
        foreach ($sources as $source) {
29 4
            $this->sources[$source->sourceId()] = $source;
30
        }
31 6
    }
32
33 2
    public function containsSource(SourceInterface $source) : bool
34
    {
35 2
        return array_key_exists($source->sourceId(), $this->sources);
36
    }
37
38 1
    public function mergeSource(SourceInterface $source)
39
    {
40 1
        if (array_key_exists($source->sourceId(), $this->sources)) {
41 1
            unset($this->sources[$source->sourceId()]);
42
        } else {
43 1
            $this->newSources[$source->sourceId()] = $source;
44
        }
45 1
        $this->sources[$source->sourceId()] = $source;
46 1
    }
47
48
    /**
49
     * @return SourceInterface[]
50
     */
51 2
    public function allSources() : array
52
    {
53 2
        return $this->sources;
54
    }
55
56
    /**
57
     * @return SourceInterface[]
58
     */
59
    public function updatedSources() : array
60
    {
61 1
        return array_filter($this->sources, function (SourceInterface $source) {
62 1
            return $source->hasChanged();
63 1
        });
64
    }
65
66
    /**
67
     * @return SourceInterface[]
68
     */
69
    public function newSources() : array
70
    {
71
        return $this->newSources;
72
    }
73
74
    /**
75
     * Should be called after each loop while watching.
76
     */
77 1
    public function reset()
78
    {
79 1
        foreach ($this->sources as $source) {
80 1
            $source->setHasNotChanged();
81
        }
82
83 1
        $this->newSources = [];
84 1
    }
85
}
86