From 8479d405a452d915c79792c0d1c54c55dd3a9790 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 14 Jul 2020 17:36:46 +0200 Subject: [PATCH] Show error on unescaped `()` in ignoreErrors --- src/Analyser/IgnoredErrorHelper.php | 4 ++-- src/Command/IgnoredRegexValidator.php | 8 +++++++- src/Command/IgnoredRegexValidatorResult.php | 20 ++++++++++++++++++- .../Command/IgnoredRegexValidatorTest.php | 6 ++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/Analyser/IgnoredErrorHelper.php b/src/Analyser/IgnoredErrorHelper.php index 80219bfa74..4dae80838e 100644 --- a/src/Analyser/IgnoredErrorHelper.php +++ b/src/Analyser/IgnoredErrorHelper.php @@ -97,7 +97,7 @@ public function initialize(): IgnoredErrorHelperResult } if ($validationResult->areAllErrorsIgnored()) { - $errors[] = sprintf("Ignored error %s has an unescaped '||' which leads to ignoring all errors. Use '\\|\\|' instead.", $ignoreMessage); + $errors[] = sprintf("Ignored error %s has an unescaped '%s' which leads to ignoring all errors. Use '%s' instead.", $ignoreMessage, $validationResult->getWrongSequence(), $validationResult->getEscapedWrongSequence()); } } else { $otherIgnoreErrors[] = [ @@ -117,7 +117,7 @@ public function initialize(): IgnoredErrorHelperResult } if ($validationResult->areAllErrorsIgnored()) { - $errors[] = sprintf("Ignored error %s has an unescaped '||' which leads to ignoring all errors. Use '\\|\\|' instead.", $ignoreMessage); + $errors[] = sprintf("Ignored error %s has an unescaped '%s' which leads to ignoring all errors. Use '%s' instead.", $ignoreMessage, $validationResult->getWrongSequence(), $validationResult->getEscapedWrongSequence()); } } } catch (\Nette\Utils\RegexpException $e) { diff --git a/src/Command/IgnoredRegexValidator.php b/src/Command/IgnoredRegexValidator.php index 78dac29f5e..7e648f86e8 100644 --- a/src/Command/IgnoredRegexValidator.php +++ b/src/Command/IgnoredRegexValidator.php @@ -35,7 +35,13 @@ public function validate(string $regex): IgnoredRegexValidatorResult $ast = $this->parser->parse($regex); } catch (\Hoa\Exception\Exception $e) { if (strpos($e->getMessage(), 'Unexpected token "|" (alternation) at line 1') === 0) { - return new IgnoredRegexValidatorResult([], false, true); + return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|'); + } + if ( + strpos($regex, '()') !== false + && strpos($e->getMessage(), 'Unexpected token ")" (_capturing) at line 1') === 0 + ) { + return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)'); } return new IgnoredRegexValidatorResult([], false, false); } diff --git a/src/Command/IgnoredRegexValidatorResult.php b/src/Command/IgnoredRegexValidatorResult.php index ddd3d6b71b..0acda5dd11 100644 --- a/src/Command/IgnoredRegexValidatorResult.php +++ b/src/Command/IgnoredRegexValidatorResult.php @@ -12,6 +12,10 @@ class IgnoredRegexValidatorResult private bool $allErrorsIgnored; + private ?string $wrongSequence; + + private ?string $escapedWrongSequence; + /** * @param array $ignoredTypes * @param bool $anchorsInTheMiddle @@ -20,12 +24,16 @@ class IgnoredRegexValidatorResult public function __construct( array $ignoredTypes, bool $anchorsInTheMiddle, - bool $allErrorsIgnored + bool $allErrorsIgnored, + ?string $wrongSequence = null, + ?string $escapedWrongSequence = null ) { $this->ignoredTypes = $ignoredTypes; $this->anchorsInTheMiddle = $anchorsInTheMiddle; $this->allErrorsIgnored = $allErrorsIgnored; + $this->wrongSequence = $wrongSequence; + $this->escapedWrongSequence = $escapedWrongSequence; } /** @@ -46,4 +54,14 @@ public function areAllErrorsIgnored(): bool return $this->allErrorsIgnored; } + public function getWrongSequence(): ?string + { + return $this->wrongSequence; + } + + public function getEscapedWrongSequence(): ?string + { + return $this->escapedWrongSequence; + } + } diff --git a/tests/PHPStan/Command/IgnoredRegexValidatorTest.php b/tests/PHPStan/Command/IgnoredRegexValidatorTest.php index eb8110e54f..0220251751 100644 --- a/tests/PHPStan/Command/IgnoredRegexValidatorTest.php +++ b/tests/PHPStan/Command/IgnoredRegexValidatorTest.php @@ -104,6 +104,12 @@ public function dataValidate(): array false, true, ], + [ + '#Method PragmaRX\Notified\Data\Repositories\Notified::firstOrCreateByEvent() should return PragmaRX\Notified\Data\Models\Notified but returns Illuminate\Database\Eloquent\Model|null#', + [], + false, + true, + ], ]; }