Skip to content

Commit 7336df1

Browse files
committed
Escape special nette/di characters in BaselineNeonErrorFormatter
1 parent f419ec3 commit 7336df1

2 files changed

Lines changed: 38 additions & 2 deletions

File tree

src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPStan\Command\ErrorFormatter;
44

5+
use Nette\DI\Helpers;
56
use Nette\Neon\Neon;
67
use PHPStan\Command\AnalysisResult;
78
use PHPStan\Command\Output;
@@ -54,9 +55,9 @@ public function formatErrors(
5455

5556
foreach ($fileErrorsCounts as $message => $count) {
5657
$errorsToOutput[] = [
57-
'message' => '#^' . preg_quote($message, '#') . '$#',
58+
'message' => Helpers::escape('#^' . preg_quote($message, '#') . '$#'),
5859
'count' => $count,
59-
'path' => $this->relativePathHelper->getRelativePath($file),
60+
'path' => Helpers::escape($this->relativePathHelper->getRelativePath($file)),
6061
];
6162
}
6263
}

tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,39 @@ public function testFormatErrorMessagesRegexEscape(): void
158158
);
159159
}
160160

161+
public function testEscapeDiNeon(): void
162+
{
163+
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));
164+
$result = new AnalysisResult(
165+
[new Error('Test %value%', 'Testfile')],
166+
[],
167+
[],
168+
[],
169+
false,
170+
null,
171+
true
172+
);
173+
174+
$formatter->formatErrors(
175+
$result,
176+
$this->getOutput()
177+
);
178+
self::assertSame(
179+
trim(
180+
Neon::encode([
181+
'parameters' => [
182+
'ignoreErrors' => [
183+
[
184+
'message' => '#^Test %%value%%$#',
185+
'count' => 1,
186+
'path' => 'Testfile',
187+
],
188+
],
189+
],
190+
], Neon::BLOCK)
191+
),
192+
trim($this->getOutputContent())
193+
);
194+
}
195+
161196
}

0 commit comments

Comments
 (0)