Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Console output format (without tputs) #161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
9 changes: 7 additions & 2 deletions src/DetectionResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
66 changes: 39 additions & 27 deletions src/Printer/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

class Console implements Printer
{
private const DEFAULT_LINE_LENGTH = 80;

private Highlighter $highlighter;

public function __construct(Highlighter $highlighter)
Expand All @@ -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('<info>Total of Magic Numbers: ' . count($detections) . '</info>');
$lines[] = '';
$lines[] = '';
$lines[] = '<info>Total of Magic Numbers: ' . count($detections) . '</info>';

$output->writeln($lines);
}

private function getDetectionLine(int $index, DetectionResult $detection): string
{
return sprintf(
'%d) %s:%d Magic number: %s',
$index,
$detection->getFilePath(),
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Show real path instead of just filename as it is possible to have identical file names but under different namespaces.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the output format doesnt it ? May be better to wait for a new major version.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may take ages to release new major :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed but a break in a major version is not great either :(

$detection->getLine(),
$detection->getValue()
);
}

private function getSnippetLine(DetectionResult $detection): string
{
return $this->highlighter->getCodeSnippet(
$detection->getSource(),
$detection->getLine(),
0,
0
);
}

/**
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Printer/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/DetectionResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Printer/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down