Skip to content

Commit

Permalink
Improve standard output format
Browse files Browse the repository at this point in the history
Heavily inspired on rust errors output.

Reviewed-by: Alexandre Eher <[email protected]>
Signed-off-by: Jefersson Nathan <[email protected]>
  • Loading branch information
malukenho committed Nov 18, 2020
1 parent 7d7b48c commit 7e4405c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 25 deletions.
48 changes: 45 additions & 3 deletions src/Output/Stdout.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@
use Codelicia\Xulieta\ValueObject\Violation;
use Symfony\Component\Console\Output\OutputInterface;

use function explode;
use function max;
use function round;
use function str_pad;
use function strlen;

use const PHP_EOL;
use const STR_PAD_LEFT;

final class Stdout implements OutputFormatter
{
Expand All @@ -20,9 +27,44 @@ public function __construct(OutputInterface $output)

public function addViolation(Violation $violation): void
{
$this->writeln('<error>Wrong code on file: ' . $violation->code()->file() . '</error>');
$this->writeln($violation->message() . PHP_EOL);
$this->writeln($violation->code()->code());
$this->output->writeln(' --> ' . $violation->file());

$linesAround = 5;
$code = $violation->code()->code();
$lines = explode(PHP_EOL, $code);
$i = 0;
$errorOccurred = false;
$startLine = max(0, $violation->violationLine() - $linesAround);
$endLine = $violation->violationLine() + $linesAround;
foreach ($lines as $line) {
$i++;

if (! ($i >= $startLine && $i <= $endLine)) {
continue;
}

if ($errorOccurred) {
$text = empty($line) ? $line : ' ' . $line;
$this->output->writeln(str_pad((string) $i, 2, ' ', STR_PAD_LEFT) . ' | <fg=red>|</>' . $text);
} else {
$text = empty($line) ? $line : ' ' . $line;
$this->output->writeln(str_pad((string) $i, 2, ' ', STR_PAD_LEFT) . ' |' . $text);
}

if ($i !== $violation->violationLine()) {
continue;
}

$errorOccurred = true;
$middleOfTheLine = (int) round(strlen($line) / 2);
$this->output->writeln(' | <fg=red>_' . str_pad('^', $middleOfTheLine, '_', STR_PAD_LEFT) . '</>');
}

$this->output->writeln([
' | <fg=red>|</>',
' <fg=red>=</> note: <fg=yellow>' . $violation->message() . '</>',
'',
]);
}

public function writeln(string $text): void
Expand Down
7 changes: 4 additions & 3 deletions tests/functional/md-immediately-php-open-tag.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ $checkRunner('tests/assets/immediately-open-tag.md');
--EXPECTF--
Finding documentation files on tests/assets/immediately-open-tag.md

Wrong code on file: tests/assets/immediately-open-tag.md
Syntax error, unexpected EOF, expecting ';' on line 2
--> tests/assets/immediately-open-tag.md
1 | echo 'Hello World!'
| |
= note: Syntax error, unexpected EOF, expecting ';' on line 2

echo 'Hello World!'

Operation failed!
16 changes: 9 additions & 7 deletions tests/functional/md-syntax-error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ $checkRunner('tests/assets/syntax-error.md');
--EXPECTF--
Finding documentation files on tests/assets/syntax-error.md

Wrong code on file: tests/assets/syntax-error.md
Syntax error, unexpected '}', expecting ';' on line 5
--> tests/assets/syntax-error.md
1 | <?php
2 |
3 | if (true) {
4 | echo 'Hello World!'
5 | }
| _^
| |
= note: Syntax error, unexpected '}', expecting ';' on line 5

<?php
if (true) {
echo 'Hello World!'
}

Operation failed!
9 changes: 5 additions & 4 deletions tests/functional/rst-immediately-php-open-tag.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ $checkRunner('tests/assets/immediately-open-tag.rst');
--EXPECTF--
Finding documentation files on tests/assets/immediately-open-tag.rst

Wrong code on file: tests/assets/immediately-open-tag.rst
Syntax error, unexpected EOF, expecting ';' on line 3

echo "Hello World!"
--> tests/assets/immediately-open-tag.rst
1 | echo "Hello World!"
2 |
| |
= note: Syntax error, unexpected EOF, expecting ';' on line 3


Operation failed!
18 changes: 10 additions & 8 deletions tests/functional/rst-syntax-error.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ $checkRunner('tests/assets/syntax-error.rst');
--EXPECTF--
Finding documentation files on tests/assets/syntax-error.rst

Wrong code on file: tests/assets/syntax-error.rst
Syntax error, unexpected '}', expecting ';' on line 5
<?php
if (true) {
echo 'Hello World!'
}
--> tests/assets/syntax-error.rst
1 | <?php
2 |
3 | if (true) {
4 | echo 'Hello World!'
5 | }
| _^
6 | |
| |
= note: Syntax error, unexpected '}', expecting ';' on line 5


Operation failed!

0 comments on commit 7e4405c

Please sign in to comment.