Report::merge()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
namespace PHPSemVerChecker\Report;
4
5
use ArrayAccess;
6
use ArrayIterator;
7
use IteratorAggregate;
8
use PHPSemVerChecker\Operation\Operation;
9
use PHPSemVerChecker\SemanticVersioning\Level;
10
11
class Report implements ArrayAccess, IteratorAggregate
12
{
13
	/**
14
	 * @var array
15
	 */
16
	protected $differences;
17
18 150
	public function __construct()
19
	{
20 150
		$levels = array_fill_keys(Level::asList(), []);
21
22 150
		$this->differences = [
23 150
			'class'     => $levels,
24 150
			'function'  => $levels,
25 150
			'interface' => $levels,
26 150
			'trait'     => $levels,
27
		];
28 150
	}
29
30
	/**
31
	 * @param \PHPSemVerChecker\Operation\Operation $classOperation
32
	 * @return \PHPSemVerChecker\Report\Report
33
	 */
34 6
	public function addClass(Operation $classOperation)
35
	{
36 6
		return $this->add('class', $classOperation);
37
	}
38
39
	/**
40
	 * @param \PHPSemVerChecker\Operation\Operation $functionOperation
41
	 * @return \PHPSemVerChecker\Report\Report
42
	 */
43 16
	public function addFunction(Operation $functionOperation)
44
	{
45 16
		return $this->add('function', $functionOperation);
46
	}
47
48
	/**
49
	 * @param \PHPSemVerChecker\Operation\Operation $interfaceOperation
50
	 * @return \PHPSemVerChecker\Report\Report
51
	 */
52 4
	public function addInterface(Operation $interfaceOperation)
53
	{
54 4
		return $this->add('interface', $interfaceOperation);
55
	}
56
57
	/**
58
	 * @param \PHPSemVerChecker\Operation\Operation $traitOperation
59
	 * @return \PHPSemVerChecker\Report\Report
60
	 */
61 4
	public function addTrait(Operation $traitOperation)
62
	{
63 4
		return $this->add('trait', $traitOperation);
64
	}
65
66
	/**
67
	 * @param string                                $context
68
	 * @param \PHPSemVerChecker\Operation\Operation $operation
69
	 * @return $this
70
	 */
71 118
	public function add($context, Operation $operation)
72
	{
73 118
		$level = $operation->getLevel();
74 118
		$this->differences[$context][$level][] = $operation;
75
76 118
		return $this;
77
	}
78
79
	/**
80
	 * @param \PHPSemVerChecker\Report\Report $report
81
	 * @return $this
82
	 */
83 5
	public function merge(Report $report)
84
	{
85 5
		foreach ($report->differences as $context => $levels) {
86 5
			foreach ($levels as $level => $differences) {
87 5
				$this->differences[$context][$level] = array_merge($this->differences[$context][$level], $differences);
88
			}
89
		}
90
91 5
		return $this;
92
	}
93
94
	/**
95
	 * @return array
96
	 */
97
	public function getDifferences()
98
	{
99
		return $this->differences;
100
	}
101
102
	/**
103
	 * @param string|array|null $context
104
	 * @param string|array|null $level
105
	 * @return bool
106
	 */
107 2
	public function hasDifferences($context = null, $level = null)
108
	{
109 2
		$queriedContexts = $context ? (array)$context : array_keys($this->differences);
110 2
		$queriedLevels = $level ? (array)$level : Level::asList('desc');
111 2
		foreach ($queriedContexts as $queriedContext) {
112 2
			foreach ($queriedLevels as $queriedLevel) {
113 2
				if ( ! empty($this->differences[$queriedContext][$queriedLevel])) {
114 1
					return true;
115
				}
116
			}
117
		}
118 1
		return false;
119
	}
120
121
	/**
122
	 * @param string|array|null $context
123
	 * @return int
124
	 */
125 2
	public function getLevelForContext($context = null)
126
	{
127 2
		$queriedContexts = $context ? (array)$context : array_keys($this->differences);
128 2
		$levels = Level::asList('desc');
129 2
		foreach ($queriedContexts as $queriedContext) {
130 2
			foreach ($levels as $level) {
131 2
				if ( ! empty($this->differences[$queriedContext][$level])) {
132 1
					return $level;
133
				}
134
			}
135
		}
136 1
		return Level::NONE;
137
	}
138
139
	/**
140
	 * @return int
141
	 */
142
	public function getSuggestedLevel()
143
	{
144
		foreach (Level::asList('desc') as $level) {
145
			foreach ($this->differences as $context => $levels) {
146
				if ( ! empty($levels[$level])) {
147
					return $level;
148
				}
149
			}
150
		}
151
152
		return Level::NONE;
153
	}
154
155
	// TODO: Get rid of ArrayAccess (temporary to transition) <[email protected]>
156
157
	/**
158
	 * @param string $offset
159
	 * @return bool
160
	 */
161
	public function offsetExists($offset)
162
	{
163
		return isset($this->differences[$offset]);
164
	}
165
166
	/**
167
	 * @param string $offset
168
	 * @return array
169
	 */
170 116
	public function offsetGet($offset)
171
	{
172 116
		return $this->differences[$offset];
173
	}
174
175
	/**
176
	 * @param string $offset
177
	 * @param mixed  $value
178
	 */
179
	public function offsetSet($offset, $value)
180
	{
181
		if ($offset === null) {
182
			$this->differences[] = $value;
183
		} else {
184
			$this->differences[$offset] = $value;
185
		}
186
	}
187
188
	/**
189
	 * @param string $offset
190
	 */
191
	public function offsetUnset($offset)
192
	{
193
		unset($this->differences[$offset]);
194
	}
195
196
	/**
197
	 * @return \ArrayIterator|\Traversable
198
	 */
199 30
	public function getIterator()
200
	{
201 30
		return new ArrayIterator($this->differences);
202
	}
203
}
204