Skip to content

Commit cee0cd7

Browse files
committed
strip invalid utf-8 pattern contents to keep the error message NEON parsable.
1 parent e432e11 commit cee0cd7

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

e2e/bug-12629/phpstan-baseline.neon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ parameters:
1919
path: src/bug-12629.php
2020

2121
-
22-
message: '#^Regex pattern is invalid\: Compilation failed\: UTF\-8 error\: byte 2 top bits not 0x80 at offset 0 in pattern\: \!Ã\[€\-Ÿ\]\!u$#'
22+
message: '#^Regex pattern is invalid\: Compilation failed\: UTF\-8 error\: byte 2 top bits not 0x80 at offset 0 in pattern$#'
2323
identifier: regexp.pattern
2424
count: 1
2525
path: src/bug-12629.php
2626

2727
-
28-
message: '#^Regex pattern is invalid\: Compilation failed\: UTF\-8 error\: isolated byte with 0x80 bit set at offset 1 in pattern\: \!\[€\-Ÿ\]\!u$#'
28+
message: '#^Regex pattern is invalid\: Compilation failed\: UTF\-8 error\: isolated byte with 0x80 bit set at offset 1 in pattern$#'
2929
identifier: regexp.pattern
3030
count: 1
3131
path: src/bug-12629.php

src/Rules/Regexp/RegularExpressionPatternRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ private function validatePattern(string $pattern): ?string
123123
try {
124124
Strings::match('', $pattern);
125125
} catch (RegexpException $e) {
126+
if (str_contains($e->getMessage(), 'UTF-8 error')) {
127+
// strip invalid utf-8 pattern contents to keep the error message NEON parsable.
128+
return substr($e->getMessage(), 0, strrpos($e->getMessage(), ':'));
129+
}
126130
return $e->getMessage();
127131
}
128132

0 commit comments

Comments
 (0)