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
|
1 |
|
public function __construct(Report $report) |
26
|
|
|
{ |
27
|
1 |
|
$this->report = $report; |
28
|
1 |
|
$this->cwd = getcwd(); |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
public function setFullPath($fullPath) |
32
|
|
|
{ |
33
|
|
|
$this->fullPath = $fullPath; |
34
|
|
|
|
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
public function output(OutputInterface $output) |
39
|
|
|
{ |
40
|
1 |
|
$suggestedChange = $this->report->getSuggestedLevel(); |
41
|
|
|
|
42
|
1 |
|
$output->writeln(''); // line clear |
43
|
1 |
|
$output->writeln('Suggested semantic versioning change: ' . Level::toString($suggestedChange)); |
44
|
|
|
|
45
|
|
|
$contexts = [ |
46
|
1 |
|
'class', |
47
|
|
|
'function', |
48
|
|
|
'interface', |
49
|
|
|
'trait', |
50
|
|
|
]; |
51
|
|
|
|
52
|
1 |
|
foreach ($contexts as $context) { |
53
|
1 |
|
$this->outputReport($output, $this->report, $context); |
54
|
|
|
} |
55
|
1 |
|
} |
56
|
|
|
|
57
|
1 |
|
protected function outputReport(OutputInterface $output, Report $report, $context) |
58
|
|
|
{ |
59
|
1 |
|
if ( ! $report->hasDifferences($context)) { |
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$output->writeln(''); // line clear |
64
|
1 |
|
$output->writeln(ucfirst($context) . ' (' . Level::toString($report->getLevelForContext($context)) . ')'); |
65
|
1 |
|
$this->outputTable($output, $report, $context); |
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 outputTable(OutputInterface $output, Report $report, $context) |
74
|
|
|
{ |
75
|
1 |
|
$table = new Table($output); |
76
|
1 |
|
$table->setHeaders(['Level', 'Location', 'Target', 'Reason', 'Code']); |
77
|
1 |
|
foreach (Level::asList('desc') as $level) { |
78
|
1 |
|
$reportForLevel = $report[$context][$level]; |
79
|
|
|
/** @var \PHPSemVerChecker\Operation\Operation $operation */ |
80
|
1 |
|
foreach ($reportForLevel as $operation) { |
81
|
1 |
|
$table->addRow([Level::toString($level), $this->getLocation($operation), $operation->getTarget(), $operation->getReason(), $operation->getCode()]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
1 |
|
$table->render(); |
85
|
1 |
|
} |
86
|
|
|
|
87
|
1 |
|
protected function getLocation(Operation $operation) |
88
|
|
|
{ |
89
|
1 |
|
$isFullPath = $this->fullPath; |
90
|
1 |
|
if ($isFullPath) { |
91
|
|
|
$location = $operation->getLocation(); |
92
|
|
|
} else { |
93
|
1 |
|
$location = str_replace($this->cwd . DIRECTORY_SEPARATOR, '', $operation->getLocation()); |
94
|
|
|
} |
95
|
1 |
|
return $location . ':' . $operation->getLine(); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|