Skip to content

Commit 6419da7

Browse files
committed
JsonErrorFormatter - strip console-specific characters from tip
1 parent fb4c5ba commit 6419da7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/Command/ErrorFormatter/JsonErrorFormatter.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Nette\Utils\Json;
66
use PHPStan\Command\AnalysisResult;
77
use PHPStan\Command\Output;
8+
use Symfony\Component\Console\Formatter\OutputFormatter;
89
use function array_key_exists;
910
use function count;
1011

@@ -26,6 +27,8 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
2627
'errors' => [],
2728
];
2829

30+
$tipFormatter = new OutputFormatter(false);
31+
2932
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
3033
$file = $fileSpecificError->getFile();
3134
if (!array_key_exists($file, $errorsArray['files'])) {
@@ -43,7 +46,7 @@ public function formatErrors(AnalysisResult $analysisResult, Output $output): in
4346
];
4447

4548
if ($fileSpecificError->getTip() !== null) {
46-
$message['tip'] = $fileSpecificError->getTip();
49+
$message['tip'] = $tipFormatter->format($fileSpecificError->getTip());
4750
}
4851

4952
$errorsArray['files'][$file]['messages'][] = $message;

tests/PHPStan/Command/ErrorFormatter/JsonErrorFormatterTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace PHPStan\Command\ErrorFormatter;
44

5+
use Nette\Utils\Json;
6+
use PHPStan\Analyser\Error;
7+
use PHPStan\Command\AnalysisResult;
58
use PHPStan\Testing\ErrorFormatterTestCase;
69
use function sprintf;
710

@@ -235,4 +238,26 @@ public function testFormatErrors(
235238
$this->assertJsonStringEqualsJsonString($expected, $this->getOutputContent(), sprintf('%s: JSON do not match', $message));
236239
}
237240

241+
public function dataFormatTip(): iterable
242+
{
243+
yield ['tip', 'tip'];
244+
yield ['<fg=cyan>%configurationFile%</>', '%configurationFile%'];
245+
yield ['this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', 'this check by setting treatPhpDocTypesAsCertain: false in your %configurationFile%.'];
246+
}
247+
248+
/**
249+
* @dataProvider dataFormatTip
250+
*/
251+
public function testFormatTip(string $tip, string $expectedTip): void
252+
{
253+
$formatter = new JsonErrorFormatter(false);
254+
$formatter->formatErrors(new AnalysisResult([
255+
new Error('Foo', '/foo/bar.php', 1, true, null, null, $tip),
256+
], [], [], [], [], false, null, true, 0), $this->getOutput());
257+
258+
$content = $this->getOutputContent();
259+
$json = Json::decode($content, Json::FORCE_ARRAY);
260+
$this->assertSame($expectedTip, $json['files']['/foo/bar.php']['messages'][0]['tip']);
261+
}
262+
238263
}

0 commit comments

Comments
 (0)