Skip to content

Commit 9cfff24

Browse files
committed
Judge the pattern for the analysed PHP, not the running one
The pre-check that gated unreadable patterns compiled them on the running engine, so a construct the runner takes but the analysed PHP predates kept its shapes — a padded \x{ 41 } on a project pinned to PHP 8.2 read as the letter x then text instead of carrying no shape at all. The reader validates for the target it reads for, the engine that answers is the one the project runs, and the invalid-pattern rule keeps speaking for the running engine as it always did — each part of the pipeline names its own judge.
1 parent fe67a32 commit 9cfff24

3 files changed

Lines changed: 45 additions & 9 deletions

File tree

‎src/Type/Regex/PHPRegexReader.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHPRegex\Parser\Exception\RegexException;
66
use PHPRegex\Parser\Node\RegexNode;
7+
use PHPRegex\Parser\Syntax\Delimiters;
78
use PHPRegex\Parser\PcreTarget;
89
use PHPRegex\Parser\RegexParser;
910
use PHPStan\DependencyInjection\AutowiredService;
@@ -45,6 +46,20 @@ public function read(string $rawRegex, string $modifiers, string $delimiter): Re
4546
return $this->parser->parsePattern($rawRegex, $modifiers, $delimiter);
4647
}
4748

49+
/**
50+
* Whether the engine the judged target runs accepts the pattern: the
51+
* parser validates for the target it reads for, so a construct the
52+
* running engine takes but the analysed one predates is refused here.
53+
*/
54+
public function accepts(string $rawRegex, string $modifiers, string $delimiter): bool
55+
{
56+
$this->parser ??= RegexParser::create($this->targetOptions());
57+
58+
$regex = $delimiter . $rawRegex . Delimiters::closing($delimiter) . $modifiers;
59+
60+
return $this->parser->validate($regex)->isValid;
61+
}
62+
4863
/**
4964
* @return array<string, int|string>
5065
*/

‎src/Type/Regex/RegexGroupParser.php‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace PHPStan\Type\Regex;
44

5-
use Nette\Utils\RegexpException;
65
use Nette\Utils\Strings;
76
use PHPRegex\Parser\Exception\RegexException;
87
use PHPRegex\Parser\Node\AlternationNode;
@@ -90,13 +89,6 @@ public function parseGroups(string $regex): ?RegexAstWalkResult
9089

9190
$modifiers = $this->regexExpressionHelper->getPatternModifiers($regex) ?? '';
9291
} else {
93-
try {
94-
Strings::match('', $regex);
95-
} catch (RegexpException) {
96-
// pattern is invalid, so let the RegularExpressionPatternRule report it
97-
return self::$parsedAst[$cacheKey] = null;
98-
}
99-
10092
$modifiers = $this->regexExpressionHelper->getPatternModifiers($regex) ?? '';
10193
foreach (self::NOT_SUPPORTED_MODIFIERS as $notSupportedModifier) {
10294
if (str_contains($modifiers, $notSupportedModifier)) {
@@ -105,11 +97,21 @@ public function parseGroups(string $regex): ?RegexAstWalkResult
10597
}
10698

10799
$rawRegex = $this->regexExpressionHelper->removeDelimitersAndModifiers($regex);
100+
$delimiter = self::delimiterOf($regex);
101+
102+
// the pattern is judged for the analysed PHP: one the engine it
103+
// runs refuses carries no shape, whatever the running engine
104+
// says about it — the invalid-pattern rule reports on the
105+
// running engine, and each part speaks for its own judge
106+
if (!$this->reader->accepts($rawRegex, $modifiers, $delimiter)) {
107+
return self::$parsedAst[$cacheKey] = null;
108+
}
109+
108110
try {
109111
$ast = self::$parsedAst[$cacheKey] = $this->reader->read(
110112
$rawRegex,
111113
$modifiers,
112-
self::delimiterOf($regex),
114+
$delimiter,
113115
);
114116
} catch (RegexException) {
115117
return self::$parsedAst[$cacheKey] = null;

‎tests/PHPStan/Type/Regex/RegexShapeReadingTest.php‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,25 @@ public function testAPinAboveTheBundledEngineIsIgnored(): void
453453
self::assertSame('10.44', $targetOf(new PHPRegexReader(new PhpVersion(80400), PcreVersion::fromString('10.44'))), 'a pin at the bundled engine judges it');
454454
}
455455

456+
public function testTargetInvalidPatternsGetNoShape(): void
457+
{
458+
// the pre-check judges for the analysed PHP, not the running one: a
459+
// padded \x{ 41 } compiles on the runner's 10.43+ engine but the PHP
460+
// an 8.2 project runs refuses it, so the shape is null and the
461+
// invalid-pattern rule speaks about the running engine alone
462+
$helper = (new ReflectionClass(RegexExpressionHelper::class))->newInstanceWithoutConstructor();
463+
$php82 = new RegexGroupParser(new PhpVersion(80200), $helper, new PHPRegexReader(new PhpVersion(80200), PcreVersion::fromString('10.40')));
464+
$php84 = new RegexGroupParser(new PhpVersion(80400), $helper, new PHPRegexReader(new PhpVersion(80400), PcreVersion::fromString('10.44')));
465+
466+
$this->resetParsedAstCache();
467+
self::assertNull($php82->parseGroups('/(a\x{ 41 })/'), 'the engine PHP 8.2 bundles refuses the padded escape');
468+
469+
$this->resetParsedAstCache();
470+
$result = $php84->parseGroups('/(a\x{ 41 })/');
471+
self::assertNotNull($result, 'the engine PHP 8.4 bundles reads it');
472+
self::assertNotInstanceOf(ConstantStringType::class, array_values($result->getCapturingGroups())[0]->getType());
473+
}
474+
456475
public function testEngineRefusedPatternsGetNoShapeAndBracketDelimitersWalk(): void
457476
{
458477
$parser = $this->createParser();

0 commit comments

Comments
 (0)