Skip to content

Commit 3a11863

Browse files
committed
Update RegularExpressionPatternRule.php
1 parent 9f388bf commit 3a11863

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Rules/Regexp/RegularExpressionPatternRule.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PHPStan\Analyser\Scope;
1010
use PHPStan\Rules\Rule;
1111
use PHPStan\Rules\RuleErrorBuilder;
12+
use PHPStan\ShouldNotHappenException;
1213
use PHPStan\Type\Regex\RegexExpressionHelper;
1314
use function in_array;
1415
use function sprintf;
@@ -126,8 +127,11 @@ private function validatePattern(string $pattern): ?string
126127
try {
127128
Strings::match('', $pattern);
128129
} catch (RegexpException $e) {
129-
$lastColonPos = strrpos($e->getMessage(), ':');
130-
if (str_contains($e->getMessage(), 'UTF-8 error') && $lastColonPos !== false) {
130+
if (str_contains($e->getMessage(), 'UTF-8 error')) {
131+
$lastColonPos = strrpos($e->getMessage(), ':');
132+
if ($lastColonPos === false) {
133+
throw new ShouldNotHappenException();
134+
}
131135
// strip invalid utf-8 pattern contents to keep the error message NEON parsable.
132136
return substr($e->getMessage(), 0, $lastColonPos);
133137
}

0 commit comments

Comments
 (0)