Skip to content

Commit cc07c4d

Browse files
committed
New Console output format (without tputs)
1 parent 890ffbe commit cc07c4d

File tree

6 files changed

+55
-36
lines changed

6 files changed

+55
-36
lines changed

src/Container.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ private function offsetSet(string $id, Closure $value): void
7171
unset($this->values[$id]);
7272
}
7373

74-
/**
75-
* @return object
76-
*/
77-
private function get(string $id)
74+
private function get(string $id): object
7875
{
7976
if (!isset($this->keys[$id])) {
8077
throw new InvalidArgumentException(sprintf('Unknown service "%s"', $id));

src/DetectionResult.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ public function __construct(SplFileInfo $file, int $line, $value)
2727
$this->value = $value;
2828
}
2929

30-
public function getFile(): SplFileInfo
30+
public function getSource(): string
3131
{
32-
return $this->file;
32+
return $this->file->getContents();
33+
}
34+
35+
public function getFilePath(): string
36+
{
37+
return $this->file->getRealPath();
3338
}
3439

3540
public function getLine(): int

src/Printer/Console.php

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
class Console implements Printer
1313
{
14-
private const DEFAULT_LINE_LENGTH = 80;
15-
1614
private Highlighter $highlighter;
1715

1816
public function __construct(Highlighter $highlighter)
@@ -22,47 +20,61 @@ public function __construct(Highlighter $highlighter)
2220

2321
public function printData(OutputInterface $output, HintList $hintList, array $detections): void
2422
{
25-
$length = (int) (`tput cols` ?: self::DEFAULT_LINE_LENGTH);
26-
$separator = str_repeat('-', $length);
27-
$output->writeln(PHP_EOL . $separator . PHP_EOL);
23+
$index = 0;
24+
25+
$lines = [];
2826

2927
foreach ($this->groupDetectionResultPerFile($detections) as $detectionResults) {
3028
foreach ($detectionResults as $detection) {
31-
$output->writeln(sprintf(
32-
'%s:%d. Magic number: %s',
33-
$detection->getFile()->getRelativePathname(),
34-
$detection->getLine(),
35-
$detection->getValue()
36-
));
37-
38-
$output->writeln(
39-
$this->highlighter->getCodeSnippet(
40-
$detection->getFile()->getContents(),
41-
$detection->getLine(),
42-
0,
43-
0
44-
)
45-
);
29+
++$index;
30+
31+
$lines[] = $this->getDetectionLine($index, $detection);
32+
$lines[] = '';
33+
$lines[] = $this->getSnippetLine($detection);
34+
$lines[] = '';
4635

4736
if ($hintList->hasHints()) {
4837
$hints = $hintList->getHintsByValue($detection->getValue());
4938

5039
if ($hints !== []) {
51-
$output->writeln('Suggestions:');
40+
$lines[] = 'Suggestions:';
5241

5342
foreach ($hints as $hint) {
54-
$output->writeln("\t\t" . $hint);
43+
$lines[] = "\t\t" . $hint;
5544
}
5645

57-
$output->write(PHP_EOL);
46+
$lines[] = PHP_EOL;
5847
}
5948
}
6049
}
61-
62-
$output->writeln($separator . PHP_EOL);
6350
}
6451

65-
$output->writeln('<info>Total of Magic Numbers: ' . count($detections) . '</info>');
52+
$lines[] = '';
53+
$lines[] = '';
54+
$lines[] = '<info>Total of Magic Numbers: ' . count($detections) . '</info>';
55+
56+
$output->writeln($lines);
57+
}
58+
59+
private function getDetectionLine(int $index, DetectionResult $detection): string
60+
{
61+
return sprintf(
62+
'%d) %s:%d Magic number: %s',
63+
$index,
64+
$detection->getFilePath(),
65+
$detection->getLine(),
66+
$detection->getValue()
67+
);
68+
}
69+
70+
private function getSnippetLine(DetectionResult $detection): string
71+
{
72+
return $this->highlighter->getCodeSnippet(
73+
$detection->getSource(),
74+
$detection->getLine(),
75+
0,
76+
0
77+
);
6678
}
6779

6880
/**
@@ -75,7 +87,7 @@ private function groupDetectionResultPerFile(array $detections): array
7587
$groupedResult = [];
7688

7789
foreach ($detections as $detection) {
78-
$groupedResult[$detection->getFile()->getRelativePathname()][] = $detection;
90+
$groupedResult[$detection->getFilePath()][] = $detection;
7991
}
8092

8193
return $groupedResult;

src/Printer/Xml.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function printData(OutputInterface $output, HintList $hintList, array $de
4646

4747
foreach ($detectionResults as $detectionResult) {
4848
$snippet = $this->getSnippet(
49-
$detectionResult->getFile()->getContents(),
49+
$detectionResult->getSource(),
5050
$detectionResult->getLine(),
5151
$detectionResult->getValue()
5252
);
@@ -101,7 +101,7 @@ private function groupDetectionResultPerFile(array $list): array
101101
$result = [];
102102

103103
foreach ($list as $detectionResult) {
104-
$result[$detectionResult->getFile()->getRelativePathname()][] = $detectionResult;
104+
$result[$detectionResult->getFilePath()][] = $detectionResult;
105105
}
106106

107107
return $result;

tests/DetectionResultTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testItCreatesResult(): void
1919

2020
$result = new DetectionResult($file, $line, $value);
2121

22-
$this->assertSame($file, $result->getFile());
22+
$this->assertSame($file->getRealPath(), $result->getFilePath());
2323
$this->assertSame($line, $result->getLine());
2424
$this->assertSame($value, $result->getValue());
2525
}

tests/Printer/XmlTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public function testPrintData() : void
3838
$splFileInfo
3939
->method('getRelativePathname')
4040
->willReturn('Foo/Bar.php');
41+
42+
$splFileInfo
43+
->method('getRealPath')
44+
->willReturn('Foo/Bar.php');
45+
4146
$splFileInfo
4247
->method('getContents')
4348
->willReturn(sprintf(

0 commit comments

Comments
 (0)