From fa1f464ab280419623fd82addfcc791c503131f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Vo=C5=99=C3=AD=C5=A1ek?= Date: Wed, 9 Oct 2024 06:56:36 +0200 Subject: [PATCH] keep || and () exceptions for ignore rules validations --- src/Command/IgnoredRegexValidator.php | 19 ++++++++----------- .../Command/IgnoredRegexValidatorTest.php | 8 +++----- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Command/IgnoredRegexValidator.php b/src/Command/IgnoredRegexValidator.php index 613779b2e64..6daecb33f01 100644 --- a/src/Command/IgnoredRegexValidator.php +++ b/src/Command/IgnoredRegexValidator.php @@ -13,7 +13,6 @@ use PHPStan\Type\VerbosityLevel; use function count; use function str_contains; -use function str_starts_with; use function strrpos; use function substr; @@ -34,19 +33,17 @@ public function validate(string $regex): IgnoredRegexValidatorResult try { /** @var TreeNode $ast */ $ast = $this->parser->parse($regex); - } catch (Exception $e) { - if (str_starts_with($e->getMessage(), 'Unexpected token "|" (alternation) at line 1')) { - return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|'); - } - if ( - str_contains($regex, '()') - && str_starts_with($e->getMessage(), 'Unexpected token ")" (_capturing) at line 1') - ) { - return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)'); - } + } catch (Exception) { return new IgnoredRegexValidatorResult([], false, false); } + if (str_contains($regex, '||')) { + return new IgnoredRegexValidatorResult([], false, true, '||', '\|\|'); + } + if (str_contains($regex, '()')) { + return new IgnoredRegexValidatorResult([], false, true, '()', '\(\)'); + } + return new IgnoredRegexValidatorResult( $this->getIgnoredTypes($ast), $this->hasAnchorsInTheMiddle($ast), diff --git a/tests/PHPStan/Command/IgnoredRegexValidatorTest.php b/tests/PHPStan/Command/IgnoredRegexValidatorTest.php index 4dd4a784355..98a6dc58cb7 100644 --- a/tests/PHPStan/Command/IgnoredRegexValidatorTest.php +++ b/tests/PHPStan/Command/IgnoredRegexValidatorTest.php @@ -104,15 +104,13 @@ public function dataValidate(): array '~Result of || is always true.~', [], false, - false, + true, ], [ '#Method PragmaRX\Notified\Data\Repositories\Notified::firstOrCreateByEvent() should return PragmaRX\Notified\Data\Models\Notified but returns Illuminate\Database\Eloquent\Model|null#', - [ - 'null' => 'null', - ], - false, + [], false, + true, ], ]; }