Reporter::output()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 8
cts 8
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
namespace PHPSemVerChecker\Reporter;
4
5
use PHPSemVerChecker\Operation\Operation;
6
use PHPSemVerChecker\Report\Report;
7
use PHPSemVerChecker\SemanticVersioning\Level;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class Reporter
12
{
13
	/**
14
	 * @var \PHPSemVerChecker\Report\Report
15
	 */
16
	protected $report;
17
	/**
18
	 * @var string
19
	 */
20
	protected $cwd;
21
	/**
22
	 * @var bool
23
	 */
24
	protected $fullPath = false;
25
26
	/**
27
	 * @param \PHPSemVerChecker\Report\Report $report
28
	 */
29 1
	public function __construct(Report $report)
30
	{
31 1
		$this->report = $report;
32 1
		$this->cwd = getcwd();
33 1
	}
34
35
	/**
36
	 * @param bool $fullPath
37
	 * @return $this
38
	 */
39
	public function setFullPath($fullPath)
40
	{
41
		$this->fullPath = $fullPath;
42
43
		return $this;
44
	}
45
46
	/**
47
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
48
	 */
49 1
	public function output(OutputInterface $output)
50
	{
51 1
		$suggestedChange = $this->report->getSuggestedLevel();
52
53 1
		$output->writeln(''); // line clear
54 1
		$output->writeln('Suggested semantic versioning change: ' . Level::toString($suggestedChange));
55
56
		$contexts = [
57 1
			'class',
58
			'function',
59
			'interface',
60
			'trait',
61
		];
62
63 1
		foreach ($contexts as $context) {
64 1
			$this->outputReport($output, $this->report, $context);
65
		}
66 1
	}
67
68
	/**
69
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
70
	 * @param \PHPSemVerChecker\Report\Report                   $report
71
	 * @param string                                            $context
72
	 */
73 1
	protected function outputReport(OutputInterface $output, Report $report, $context)
74
	{
75 1
		if ( ! $report->hasDifferences($context)) {
76 1
			return;
77
		}
78
79 1
		$output->writeln(''); // line clear
80 1
		$output->writeln(ucfirst($context) . ' (' . Level::toString($report->getLevelForContext($context)) . ')');
81 1
		$this->outputTable($output, $report, $context);
82 1
	}
83
84
	/**
85
	 * @param \Symfony\Component\Console\Output\OutputInterface $output
86
	 * @param \PHPSemVerChecker\Report\Report                   $report
87
	 * @param string                                            $context
88
	 */
89 1
	protected function outputTable(OutputInterface $output, Report $report, $context)
90
	{
91 1
		$table = new Table($output);
92 1
		$table->setHeaders(['Level', 'Location', 'Target', 'Reason', 'Code']);
93 1
		foreach (Level::asList('desc') as $level) {
94 1
			$reportForLevel = $report[$context][$level];
95
			/** @var \PHPSemVerChecker\Operation\Operation $operation */
96 1
			foreach ($reportForLevel as $operation) {
97 1
				$table->addRow([Level::toString($level), $this->getLocation($operation), $operation->getTarget(), $operation->getReason(), $operation->getCode()]);
98
			}
99
		}
100 1
		$table->render();
101 1
	}
102
103
	/**
104
	 * @param \PHPSemVerChecker\Operation\Operation $operation
105
	 * @return string
106
	 */
107 1
	protected function getLocation(Operation $operation)
108
	{
109 1
		$isFullPath = $this->fullPath;
110 1
		if ($isFullPath) {
111
			$location = $operation->getLocation();
112
		} else {
113 1
			$location = str_replace($this->cwd . DIRECTORY_SEPARATOR, '', $operation->getLocation());
114
		}
115 1
		return $location . ':' . $operation->getLine();
116
	}
117
}
118