Skip to content

Commit 698bed9

Browse files
committed
Adding verbose formatting as an option
1 parent 3985bc2 commit 698bed9

File tree

4 files changed

+146
-0
lines changed

4 files changed

+146
-0
lines changed

src/Error/Error.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ public function getMessage(): string
3333
return "[{$this->type}] {$this->message}";
3434
}
3535

36+
public function getRawMessage(): string
37+
{
38+
return $this->message;
39+
}
40+
3641
public function getFileName(): string
3742
{
3843
return $this->fileName;

src/Output/VerboseFormatter.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mamazu\DocumentationParser\Output;
6+
7+
use Mamazu\DocumentationParser\Error\Error;
8+
use Webmozart\Assert\Assert;
9+
10+
class VerboseFormatter implements FormatterInterface
11+
{
12+
public function format(array $output): string
13+
{
14+
return implode(
15+
"\n",
16+
array_map(fn (Error $error): string => $this->formatError($error), $output)
17+
);
18+
}
19+
20+
private function formatError(Error $error): string
21+
{
22+
$fileContents = file_get_contents($error->getFileName());
23+
Assert::string($fileContents, 'Could not read file contents of ' . $fileContents);
24+
25+
$lines = explode("\n", $fileContents);
26+
27+
$seperator = '========== [' . $error->getType() . '@' . $error->getFileName() . ':' . $error->getLineNumber() . '] ==========';
28+
29+
$message = "\n${seperator}\n";
30+
$message .= $lines[$error->getLineNumber() - 2] . "\n";
31+
$message .= $lines[$error->getLineNumber() - 1] . "\n";
32+
$message .= "^^^ \e[31m" . $error->getRawMessage() . "\e[0m ^^^\n";
33+
$message .= ($lines[$error->getLineNumber()] ?? '$EOF$') . "\n";
34+
$message .= str_repeat('=', strlen($seperator));
35+
36+
return $message;
37+
}
38+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
namespace Tests\Mamazu\DocumentationParser\Output;
5+
6+
use Mamazu\DocumentationParser\Error\Error;
7+
use Mamazu\DocumentationParser\Output\VerboseFormatter;
8+
use org\bovigo\vfs\vfsStream;
9+
use org\bovigo\vfs\vfsStreamDirectory;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class VerboseFormatterTest extends TestCase
13+
{
14+
private const FILE_NAME = 'vfs://workDir/error.php';
15+
16+
private VerboseFormatter $verboseFormatter;
17+
18+
private vfsStreamDirectory $workDir;
19+
20+
protected function setUp(): void
21+
{
22+
parent::setUp();
23+
$this->workDir = vfsStream::setup('workDir');
24+
$this->verboseFormatter = new VerboseFormatter();
25+
26+
$file = vfsStream::newFile('error.php');
27+
$file->setContent(<<<PHP
28+
<?php
29+
echo "Hello"
30+
\$a = 10;
31+
32+
PHP);
33+
$this->workDir->addChild($file);
34+
}
35+
36+
public function testFormatsTheAnError(): void
37+
{
38+
$errors = [
39+
new Error(self::FILE_NAME, 3, 'Expected ; got variable'),
40+
];
41+
42+
$expected = <<<TXT
43+
44+
========== [@vfs://workDir/error.php:3] ==========
45+
echo "Hello"
46+
\$a = 10;
47+
^^^ \e[31mExpected ; got variable\e[0m ^^^
48+
49+
==================================================
50+
TXT;
51+
52+
$this->assertSame(
53+
$expected,
54+
$this->verboseFormatter->format($errors)
55+
);
56+
}
57+
58+
public function testFormatsTheMultipleErrors(): void
59+
{
60+
$errors = [
61+
new Error(self::FILE_NAME, 3, 'Expected ; got variable'),
62+
new Error(self::FILE_NAME, 4, 'Unexpected end of file'),
63+
];
64+
65+
$expected = <<<TXT
66+
67+
========== [@vfs://workDir/error.php:3] ==========
68+
echo "Hello"
69+
\$a = 10;
70+
^^^ \e[31mExpected ; got variable\e[0m ^^^
71+
72+
==================================================
73+
74+
========== [@vfs://workDir/error.php:4] ==========
75+
\$a = 10;
76+
77+
^^^ \e[31mUnexpected end of file\e[0m ^^^
78+
\$EOF\$
79+
==================================================
80+
TXT;
81+
82+
$this->assertSame(
83+
$expected,
84+
$this->verboseFormatter->format($errors)
85+
);
86+
}
87+
}

tests/extensions/verbose.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
use Mamazu\DocumentationParser\Application;
5+
use Mamazu\DocumentationParser\FileList;
6+
use Mamazu\DocumentationParser\Output\FormatterInterface;
7+
use Mamazu\DocumentationParser\Output\VerboseFormatter;
8+
9+
/**
10+
* Here are the variables you can use to extend the application:
11+
* @var FileList $fileList
12+
* @var Application $application
13+
* @var FormatterInterface $formatter
14+
*/
15+
16+
$formatter = new VerboseFormatter();

0 commit comments

Comments
 (0)