JsonReporter::output()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PHPSemVerChecker\Reporter;
4
5
use PHPSemVerChecker\Report\Report;
6
use PHPSemVerChecker\SemanticVersioning\Level;
7
use PHPSemVerChecker\Wrapper\Filesystem;
8
9
class JsonReporter
10
{
11
	/**
12
	 * @var \PHPSemVerChecker\Report\Report
13
	 */
14
	protected $report;
15
	/**
16
	 * @var string
17
	 */
18
	protected $path;
19
	/**
20
	 * @var \PHPSemVerChecker\Wrapper\Filesystem
21
	 */
22
	protected $filesystem;
23
24
	/**
25
	 * @param \PHPSemVerChecker\Report\Report      $report
26
	 * @param string                               $path
27
	 * @param \PHPSemVerChecker\Wrapper\Filesystem $filesystem
28
	 */
29 1
	public function __construct(Report $report, $path, Filesystem $filesystem = null)
30
	{
31 1
		$this->report = $report;
32 1
		$this->path = $path;
33 1
		$this->filesystem = $filesystem ?: new Filesystem();
34 1
	}
35
36
	/**
37
	 * @return array
38
	 */
39 1
	public function getOutput()
40
	{
41 1
		$output = [];
42 1
		$output['level'] = Level::toString($this->report->getSuggestedLevel());
43 1
		$output['changes'] = [];
44
45
		$contexts = [
46 1
			'class',
47
			'function',
48
			'interface',
49
			'trait',
50
		];
51
52 1
		$differences = $this->report->getDifferences();
53 1
		foreach ($contexts as $context) {
54 1
			foreach (Level::asList('desc') as $level) {
55 1
				$reportForLevel = $differences[$context][$level];
56
				/** @var \PHPSemVerChecker\Operation\Operation $operation */
57 1
				foreach ($reportForLevel as $operation) {
58 1
					$output['changes'][$context][] = [
59 1
						'level'    => Level::toString($level),
60 1
						'location' => $operation->getLocation(),
61 1
						'line'     => $operation->getLine(),
62 1
						'target'   => $operation->getTarget(),
63 1
						'reason'   => $operation->getReason(),
64 1
						'code'     => $operation->getCode(),
65
					];
66
				}
67
			}
68
		}
69
70 1
		return $output;
71
	}
72
73 1
	public function output()
74
	{
75 1
		$this->filesystem->write($this->path, json_encode($this->getOutput(), JSON_PRETTY_PRINT));
76 1
	}
77
}
78