Completed
Pull Request — master (#99)
by
unknown
02:17
created

Reporter::getLocation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0932

Importance

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