/
Console.php
58 lines (51 loc) · 1.96 KB
/
Console.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
namespace Povils\PHPMND\Printer;
use JakubOnderka\PhpConsoleColor\ConsoleColor;
use JakubOnderka\PhpConsoleHighlighter\Highlighter;
use Povils\PHPMND\FileReportList;
use Povils\PHPMND\HintList;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class Console
*
* @package Povils\PHPMND\Printer
*/
class Console implements Printer
{
const DEFAULT_LINE_LENGTH = 80;
public function printData(OutputInterface $output, FileReportList $fileReportList, HintList $hintList): void
{
$length = (int) (`tput cols` ?: self::DEFAULT_LINE_LENGTH);
$separator = str_repeat('-', $length);
$output->writeln(PHP_EOL . $separator . PHP_EOL);
$total = 0;
foreach ($fileReportList->getFileReports() as $fileReport) {
$entries = $fileReport->getEntries();
$total += count($entries);
foreach ($entries as $entry) {
$output->writeln(sprintf(
'%s:%d Magic number: %s',
$fileReport->getFile()->getRelativePathname(),
$entry['line'],
$entry['value']
));
$highlighter = new Highlighter(new ConsoleColor());
$output->writeln(
$highlighter->getCodeSnippet($fileReport->getFile()->getContents(), $entry['line'], 0, 0)
);
if ($hintList->hasHints()) {
$hints = $hintList->getHintsByValue($entry['value']);
if (false === empty($hints)) {
$output->writeln('Suggestions:');
foreach ($hints as $hint) {
$output->writeln("\t\t" . $hint);
}
$output->write(PHP_EOL);
}
}
}
$output->writeln($separator . PHP_EOL);
}
$output->writeln('<info>Total of Magic Numbers: ' . $total . '</info>');
}
}