Skip to content

Commit

Permalink
keep || and () exceptions for ignore rules validations
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Oct 11, 2024
1 parent a6e8039 commit 23d4dfa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 16 deletions.
19 changes: 8 additions & 11 deletions src/Command/IgnoredRegexValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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),
Expand Down
8 changes: 3 additions & 5 deletions tests/PHPStan/Command/IgnoredRegexValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}
Expand Down

0 comments on commit 23d4dfa

Please sign in to comment.