Skip to content

Commit

Permalink
Escape special nette/di characters in BaselineNeonErrorFormatter
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Aug 7, 2020
1 parent f419ec3 commit 7336df1
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Command\ErrorFormatter;

use Nette\DI\Helpers;
use Nette\Neon\Neon;
use PHPStan\Command\AnalysisResult;
use PHPStan\Command\Output;
Expand Down Expand Up @@ -54,9 +55,9 @@ public function formatErrors(

foreach ($fileErrorsCounts as $message => $count) {
$errorsToOutput[] = [
'message' => '#^' . preg_quote($message, '#') . '$#',
'message' => Helpers::escape('#^' . preg_quote($message, '#') . '$#'),
'count' => $count,
'path' => $this->relativePathHelper->getRelativePath($file),
'path' => Helpers::escape($this->relativePathHelper->getRelativePath($file)),
];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,39 @@ public function testFormatErrorMessagesRegexEscape(): void
);
}

public function testEscapeDiNeon(): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
$result = new AnalysisResult(
[new Error('Test %value%', 'Testfile')],
[],
[],
[],
false,
null,
true
);

$formatter->formatErrors(
$result,
$this->getOutput()
);
self::assertSame(
trim(
Neon::encode([
'parameters' => [
'ignoreErrors' => [
[
'message' => '#^Test %%value%%$#',
'count' => 1,
'path' => 'Testfile',
],
],
],
], Neon::BLOCK)
),
trim($this->getOutputContent())
);
}

}

0 comments on commit 7336df1

Please sign in to comment.