Skip to content

Commit

Permalink
baseline: normalize newlines in error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Nov 23, 2019
1 parent 0ba2dba commit 95843b4
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php eol=crlf
9 changes: 7 additions & 2 deletions src/Analyser/IgnoredError.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,15 @@ public static function shouldIgnore(
?string $path
): bool
{
// normalize newlines to allow working with ignore-patterns independent of used OS newline-format
$errorMessage = $error->getMessage();
$errorMessage = str_replace(['\r\n', '\r'], '\n', $errorMessage);
$ignoredErrorPattern = str_replace(['\r\n', '\r'], '\n', $ignoredErrorPattern);

if ($path !== null) {
$fileExcluder = new FileExcluder($fileHelper, [$path]);

if (\Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) === null) {
if (\Nette\Utils\Strings::match($errorMessage, $ignoredErrorPattern) === null) {
return false;
}

Expand All @@ -63,7 +68,7 @@ public static function shouldIgnore(
return $isExcluded;
}

return \Nette\Utils\Strings::match($error->getMessage(), $ignoredErrorPattern) !== null;
return \Nette\Utils\Strings::match($errorMessage, $ignoredErrorPattern) !== null;
}

}
3 changes: 3 additions & 0 deletions src/Command/ErrorFormatter/BaselineNeonErrorFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public function formatErrors(
}

foreach ($fileErrorsCounts as $message => $count) {
// normalize newlines to allow working with ignore-patterns independent of used OS newline-format
$message = str_replace(['\r\n', '\r'], '\n', $message);

$errorsToOutput[] = [
'message' => '#^' . preg_quote($message, '#') . '$#',
'count' => $count,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function testErrorWithTrait(): void
{
$output = $this->runPhpStan(null);
$errors = Json::decode($output, Json::FORCE_ARRAY);
$this->assertSame(4, array_sum($errors['totals']));
$this->assertCount(4, $errors['files']);
$this->assertSame(7, array_sum($errors['totals']));
$this->assertCount(5, $errors['files']);
}

public function testGenerateBaselineAndRunAgainWithIt(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,32 @@ public function testFormatErrorMessagesRegexEscape(): void
);
}

public function testFormatErrorMessagesNormalizedNewlines(): void
{
$formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH));

$result = new AnalysisResult(
[new Error('Error message\nwith\r\ndifferent\rnewlines', 'Testfile')],
['Error message\nwith\r\ndifferent\rnewlines'],
false,
false,
null
);
$formatter->formatErrors(
$result,
$this->getErrorConsoleStyle()
);

self::assertSame(
trim(Neon::encode(['parameters' => ['ignoreErrors' => [
[
'message' => '#^Error message\\\\nwith\\\\ndifferent\\\\nnewlines$#',
'count' => 1,
'path' => 'Testfile',
],
]]], Neon::BLOCK)),
trim($this->getOutputContent())
);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Command/ErrorFormatter/data/WindowsNewlines.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace BaselineIntegration;

class WindowsNewlines {

/**
* The following phpdoc is invalid and should trigger a error message containing newlines.
*
* @param
* $object
*/
public function phpdocWithNewlines($object) {
}

}

0 comments on commit 95843b4

Please sign in to comment.