Skip to content

Commit

Permalink
ignoreErrors fix
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas authored and ondrejmirtes committed Sep 28, 2022
1 parent 376449e commit f9bb961
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 27 deletions.
8 changes: 4 additions & 4 deletions conf/config.neon
Expand Up @@ -314,13 +314,13 @@ parametersSchema:
anyOf(
string(),
structure([
message: string()
path: string()
messages: listOf(string())
?path: string()
?reportUnmatched: bool()
]),
structure([
messages: listOf(string())
path: string()
message: string()
?path: string()
?reportUnmatched: bool()
]),
structure([
Expand Down
62 changes: 39 additions & 23 deletions src/DependencyInjection/ValidateIgnoredErrorsExtension.php
Expand Up @@ -101,35 +101,28 @@ public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry
),
),
);
$errors = [];

$errors = [];
foreach ($ignoreErrors as $ignoreError) {
try {
if (is_array($ignoreError)) {
if (isset($ignoreError['count'])) {
continue; // ignoreError coming from baseline will be correct
}
$ignoreMessage = $ignoreError['message'];
} else {
$ignoreMessage = $ignoreError;
}

Strings::match('', $ignoreMessage);
$validationResult = $ignoredRegexValidator->validate($ignoreMessage);
$ignoredTypes = $validationResult->getIgnoredTypes();
if (count($ignoredTypes) > 0) {
$errors[] = $this->createIgnoredTypesError($ignoreMessage, $ignoredTypes);
if (is_array($ignoreError)) {
if (isset($ignoreError['count'])) {
continue; // ignoreError coming from baseline will be correct
}

if ($validationResult->hasAnchorsInTheMiddle()) {
$errors[] = $this->createAnchorInTheMiddleError($ignoreMessage);
if (isset($ignoreError['messages'])) {
$ignoreMessages = $ignoreError['messages'];
} else {
$ignoreMessages = [$ignoreError['message']];
}
} else {
$ignoreMessages = [$ignoreError];
}

if ($validationResult->areAllErrorsIgnored()) {
$errors[] = sprintf("Ignored error %s has an unescaped '%s' which leads to ignoring all errors. Use '%s' instead.", $ignoreMessage, $validationResult->getWrongSequence(), $validationResult->getEscapedWrongSequence());
foreach ($ignoreMessages as $ignoreMessage) {
$error = $this->validateMessage($ignoredRegexValidator, $ignoreMessage);
if ($error === null) {
continue;
}
} catch (RegexpException $e) {
$errors[] = $e->getMessage();
$errors[] = $error;
}
}

Expand All @@ -140,6 +133,29 @@ public function getRegistry(): OperatorTypeSpecifyingExtensionRegistry
throw new InvalidIgnoredErrorPatternsException($errors);
}

private function validateMessage(IgnoredRegexValidator $ignoredRegexValidator, string $ignoreMessage): ?string
{
try {
Strings::match('', $ignoreMessage);
$validationResult = $ignoredRegexValidator->validate($ignoreMessage);
$ignoredTypes = $validationResult->getIgnoredTypes();
if (count($ignoredTypes) > 0) {
return $this->createIgnoredTypesError($ignoreMessage, $ignoredTypes);
}

if ($validationResult->hasAnchorsInTheMiddle()) {
return $this->createAnchorInTheMiddleError($ignoreMessage);
}

if ($validationResult->areAllErrorsIgnored()) {
return sprintf("Ignored error %s has an unescaped '%s' which leads to ignoring all errors. Use '%s' instead.", $ignoreMessage, $validationResult->getWrongSequence(), $validationResult->getEscapedWrongSequence());
}
} catch (RegexpException $e) {
return $e->getMessage();
}
return null;
}

/**
* @param array<string, string> $ignoredTypes
*/
Expand Down
25 changes: 25 additions & 0 deletions tests/PHPStan/DependencyInjection/IgnoreErrorsTest.php
@@ -0,0 +1,25 @@
<?php declare(strict_types = 1);

namespace PHPStan\DependencyInjection;

use PHPStan\Testing\PHPStanTestCase;

class IgnoreErrorsTest extends PHPStanTestCase
{

public function testIgnoreErrors(): void
{
$this->assertCount(10, self::getContainer()->getParameter('ignoreErrors'));
}

/**
* @return string[]
*/
public static function getAdditionalConfigFiles(): array
{
return [
__DIR__ . '/ignoreErrors.neon',
];
}

}
42 changes: 42 additions & 0 deletions tests/PHPStan/DependencyInjection/ignoreErrors.neon
@@ -0,0 +1,42 @@
parameters:
ignoreErrors:
- "#error#"
-
message: '#error#'
reportUnmatched: false
-
message: '#error#'
path: '/dir/*'
-
message: '#error#'
path: '/dir/*'
reportUnmatched: false
-
messages:
- '#error#'
path: '/dir/*'
-
messages:
- '#error#'
path: '/dir/*'
reportUnmatched: false
-
message: '#error#'
paths:
- '/dir/*'
-
message: '#error#'
paths:
- '/dir/*'
reportUnmatched: false
-
messages:
- '#error#'
paths:
- '/dir/*'
-
messages:
- '#error#'
paths:
- '/dir/*'
reportUnmatched: false

0 comments on commit f9bb961

Please sign in to comment.