Skip to content

Commit affc97a

Browse files
committed
Document the regex shape upgrade
What changes for inferred $matches types, each verifiable against the engine with a one-line preg_match: the backreference that stops reading as its spelling, the constructs that stop losing their shapes, the /x whitespace the engine skips, the (?n) group the engine never fills, and the read judged for the analysed PHP, with the pcreVersion parameter naming an engine below what it bundles.
1 parent 904796b commit affc97a

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

‎UPGRADING.md‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ Upgrading from PHPStan 1.x to 2.0
55

66
PHPStan now requires PHP 7.4 or newer to run.
77

8+
## Regex shapes are read through the PHPRegex parser
9+
10+
Regex functions like `preg_match()` are now typed from [PHPRegex](https://github.com/php-regex/php-regex),
11+
a pure-PHP PCRE2 parser that knows which PHP and PCRE2 release it judges for,
12+
replacing the vendored PCRE grammar (forked from Hoa's). Most patterns keep
13+
their shapes; the ones that change, change toward what the engine does:
14+
15+
- `/(a)(\1)/` types the second group as `string`, not the literal `'1'` the
16+
backreference spelling was read as.
17+
- Constructs the grammar refused — `\g{1}`, `\k<name>`, `(*PRUNE)`-family
18+
verbs, `(?C)` callouts, `\Q..\E` runs, assertion conditions — produce
19+
shapes instead of falling back to `array<string, string|null>`.
20+
- Under `/x`, whitespace and `\Q..\E` are read as the engine reads them;
21+
subjects stop being typed non-falsy on whitespace the engine skips.
22+
- The `(?n)` option turns capturing off from where it stands; the shapes
23+
no longer carry a group the engine never fills.
24+
- The read is judged for the analysed PHP, not the running one: a padded
25+
`\x{ 41 }` escape is the letter A on engines from 10.43 and the letter
26+
x then text on older ones. An engine older than the one the analysed
27+
PHP bundles can be named with the `pcreVersion` parameter or
28+
`config.platform.lib-pcre` in composer.json; the pin only narrows.
29+
30+
If an inferred `$matches` type narrows past what your code expects, the
31+
baseline is the place to say so — each change above is verifiable against
32+
the engine with a one-line `preg_match()`.
33+
834
## Upgrading guide for end users
935

1036
The best way to get ready for upgrade to PHPStan 2.0 is to update to the **latest PHPStan 1.12 release**

0 commit comments

Comments
 (0)