diff --git a/src/Container.php b/src/Container.php
index df0dd80..93c2c55 100644
--- a/src/Container.php
+++ b/src/Container.php
@@ -71,10 +71,7 @@ private function offsetSet(string $id, Closure $value): void
unset($this->values[$id]);
}
- /**
- * @return object
- */
- private function get(string $id)
+ private function get(string $id): object
{
if (!isset($this->keys[$id])) {
throw new InvalidArgumentException(sprintf('Unknown service "%s"', $id));
diff --git a/src/DetectionResult.php b/src/DetectionResult.php
index 5d2dd97..1afd877 100644
--- a/src/DetectionResult.php
+++ b/src/DetectionResult.php
@@ -27,9 +27,14 @@ public function __construct(SplFileInfo $file, int $line, $value)
$this->value = $value;
}
- public function getFile(): SplFileInfo
+ public function getSource(): string
{
- return $this->file;
+ return $this->file->getContents();
+ }
+
+ public function getFilePath(): string
+ {
+ return $this->file->getRealPath();
}
public function getLine(): int
diff --git a/src/Printer/Console.php b/src/Printer/Console.php
index 430e161..7c886bc 100644
--- a/src/Printer/Console.php
+++ b/src/Printer/Console.php
@@ -11,8 +11,6 @@
class Console implements Printer
{
- private const DEFAULT_LINE_LENGTH = 80;
-
private Highlighter $highlighter;
public function __construct(Highlighter $highlighter)
@@ -22,47 +20,61 @@ public function __construct(Highlighter $highlighter)
public function printData(OutputInterface $output, HintList $hintList, array $detections): void
{
- $length = (int) (`tput cols` ?: self::DEFAULT_LINE_LENGTH);
- $separator = str_repeat('-', $length);
- $output->writeln(PHP_EOL . $separator . PHP_EOL);
+ $index = 0;
+
+ $lines = [];
foreach ($this->groupDetectionResultPerFile($detections) as $detectionResults) {
foreach ($detectionResults as $detection) {
- $output->writeln(sprintf(
- '%s:%d. Magic number: %s',
- $detection->getFile()->getRelativePathname(),
- $detection->getLine(),
- $detection->getValue()
- ));
-
- $output->writeln(
- $this->highlighter->getCodeSnippet(
- $detection->getFile()->getContents(),
- $detection->getLine(),
- 0,
- 0
- )
- );
+ ++$index;
+
+ $lines[] = $this->getDetectionLine($index, $detection);
+ $lines[] = '';
+ $lines[] = $this->getSnippetLine($detection);
+ $lines[] = '';
if ($hintList->hasHints()) {
$hints = $hintList->getHintsByValue($detection->getValue());
if ($hints !== []) {
- $output->writeln('Suggestions:');
+ $lines[] = 'Suggestions:';
foreach ($hints as $hint) {
- $output->writeln("\t\t" . $hint);
+ $lines[] = "\t\t" . $hint;
}
- $output->write(PHP_EOL);
+ $lines[] = PHP_EOL;
}
}
}
-
- $output->writeln($separator . PHP_EOL);
}
- $output->writeln('Total of Magic Numbers: ' . count($detections) . '');
+ $lines[] = '';
+ $lines[] = '';
+ $lines[] = 'Total of Magic Numbers: ' . count($detections) . '';
+
+ $output->writeln($lines);
+ }
+
+ private function getDetectionLine(int $index, DetectionResult $detection): string
+ {
+ return sprintf(
+ '%d) %s:%d Magic number: %s',
+ $index,
+ $detection->getFilePath(),
+ $detection->getLine(),
+ $detection->getValue()
+ );
+ }
+
+ private function getSnippetLine(DetectionResult $detection): string
+ {
+ return $this->highlighter->getCodeSnippet(
+ $detection->getSource(),
+ $detection->getLine(),
+ 0,
+ 0
+ );
}
/**
@@ -75,7 +87,7 @@ private function groupDetectionResultPerFile(array $detections): array
$groupedResult = [];
foreach ($detections as $detection) {
- $groupedResult[$detection->getFile()->getRelativePathname()][] = $detection;
+ $groupedResult[$detection->getFilePath()][] = $detection;
}
return $groupedResult;
diff --git a/src/Printer/Xml.php b/src/Printer/Xml.php
index f0c933c..c204736 100644
--- a/src/Printer/Xml.php
+++ b/src/Printer/Xml.php
@@ -46,7 +46,7 @@ public function printData(OutputInterface $output, HintList $hintList, array $de
foreach ($detectionResults as $detectionResult) {
$snippet = $this->getSnippet(
- $detectionResult->getFile()->getContents(),
+ $detectionResult->getSource(),
$detectionResult->getLine(),
$detectionResult->getValue()
);
@@ -101,7 +101,7 @@ private function groupDetectionResultPerFile(array $list): array
$result = [];
foreach ($list as $detectionResult) {
- $result[$detectionResult->getFile()->getRelativePathname()][] = $detectionResult;
+ $result[$detectionResult->getFilePath()][] = $detectionResult;
}
return $result;
diff --git a/tests/DetectionResultTest.php b/tests/DetectionResultTest.php
index e6ddece..f588cd5 100644
--- a/tests/DetectionResultTest.php
+++ b/tests/DetectionResultTest.php
@@ -19,7 +19,7 @@ public function testItCreatesResult(): void
$result = new DetectionResult($file, $line, $value);
- $this->assertSame($file, $result->getFile());
+ $this->assertSame($file->getRealPath(), $result->getFilePath());
$this->assertSame($line, $result->getLine());
$this->assertSame($value, $result->getValue());
}
diff --git a/tests/Printer/XmlTest.php b/tests/Printer/XmlTest.php
index 5606f38..ee0db3e 100644
--- a/tests/Printer/XmlTest.php
+++ b/tests/Printer/XmlTest.php
@@ -38,6 +38,11 @@ public function testPrintData() : void
$splFileInfo
->method('getRelativePathname')
->willReturn('Foo/Bar.php');
+
+ $splFileInfo
+ ->method('getRealPath')
+ ->willReturn('Foo/Bar.php');
+
$splFileInfo
->method('getContents')
->willReturn(sprintf(