Skip to content

Commit 4168fe5

Browse files
authored
Fix missing ltrim in regex parse
1 parent 4273fbb commit 4168fe5

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/Type/Regex/RegexExpressionHelper.php

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ public function getPatternModifiers(string $pattern): ?string
8888

8989
public function removeDelimitersAndModifiers(string $pattern): string
9090
{
91+
$pattern = ltrim($pattern);
92+
9193
$endDelimiterPos = $this->getEndDelimiterPos($pattern);
9294

9395
if ($endDelimiterPos === false) {

tests/PHPStan/Analyser/nsrt/preg_match_shapes.php

+10
Original file line numberDiff line numberDiff line change
@@ -768,3 +768,13 @@ function bug11604b (string $string): void {
768768
assertType("array{0: string, 1?: ''|'XX', 2?: ''|'YY', 3?: 'ZZ'}", $matches);
769769
}
770770
}
771+
772+
function testLtrimDelimiter (string $string): void {
773+
if (preg_match(' /(x)/', $string, $matches)) {
774+
assertType("array{string, 'x'}", $matches);
775+
}
776+
777+
if (preg_match(' /(x)/', $string, $matches)) {
778+
assertType("array{string, 'x'}", $matches);
779+
}
780+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\Type\Regex;
4+
5+
use PHPStan\Testing\PHPStanTestCase;
6+
7+
class RegexExpressionHelperTest extends PHPStanTestCase
8+
{
9+
10+
public function dataRemoveDelimitersAndModifiers(): array
11+
{
12+
return [
13+
[
14+
'/x/',
15+
'x',
16+
],
17+
[
18+
'~x~',
19+
'x',
20+
],
21+
[
22+
'~~',
23+
'',
24+
],
25+
[
26+
'~x~is',
27+
'x',
28+
],
29+
[
30+
' ~x~',
31+
'x',
32+
],
33+
[
34+
' ~x~ ',
35+
'x',
36+
],
37+
[
38+
'[x]',
39+
'x',
40+
],
41+
[
42+
'{x}mx',
43+
'x',
44+
],
45+
];
46+
}
47+
48+
/**
49+
* @dataProvider dataRemoveDelimitersAndModifiers
50+
*/
51+
public function testRemoveDelimitersAndModifiers(string $inputPattern, string $expectedPatternWithoutDelimiter): void
52+
{
53+
$regexExpressionHelper = self::getContainer()->getByType(RegexExpressionHelper::class);
54+
55+
$this->assertSame(
56+
$expectedPatternWithoutDelimiter,
57+
$regexExpressionHelper->removeDelimitersAndModifiers($inputPattern),
58+
);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)