Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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']));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we need to change this assertion because of the newly added testfiles under data/

$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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved the test into this new file, which uses windows line endings.
also added a .gitattributes file, which should make sure that the line-endings for this file are enforced to CRLF


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

}