Skip to content

Commit e300eb6

Browse files
authored
Switch to use PREG_UNMATCHED_AS_NULL always, drop PHP <7.4, fixes #8 (#10)
1 parent c8e9d27 commit e300eb6

File tree

7 files changed

+19
-39
lines changed

7 files changed

+19
-39
lines changed

.github/workflows/continuous-integration.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ jobs:
1717
strategy:
1818
matrix:
1919
php-version:
20-
- "7.2"
21-
- "7.3"
2220
- "7.4"
2321
- "8.0"
2422
- "8.1"

.github/workflows/phpstan.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
strategy:
1818
matrix:
1919
php-version:
20-
- "7.2"
20+
- "7.4"
2121
- "8.1"
2222

2323
steps:

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ PCRE wrapping library that offers type-safe `preg_*` replacements.
66
This library gives you a way to ensure `preg_*` functions do not fail silently, returning
77
unexpected `null`s that may not be handled.
88

9-
As of 2.0 this library also enforces [`PREG_UNMATCHED_AS_NULL`](#preg_unmatched_as_null) usage for all matching functions, [read more below](#preg_unmatched_as_null) to understand the implications.
9+
As of 3.0 this library enforces [`PREG_UNMATCHED_AS_NULL`](#preg_unmatched_as_null) usage
10+
for all matching and replaceCallback functions, [read more below](#preg_unmatched_as_null)
11+
to understand the implications.
1012

1113
It thus makes it easier to work with static analysis tools like PHPStan or Psalm as it
1214
simplifies and reduces the possible return values from all the `preg_*` functions which
@@ -32,7 +34,9 @@ $ composer require composer/pcre
3234
Requirements
3335
------------
3436

35-
* PHP 5.3.2 is required but using the latest version of PHP is highly recommended.
37+
* PHP 7.4.0 is required for 3.x versions
38+
* PHP 7.2.0 is required for 2.x versions
39+
* PHP 5.3.2 is required for 1.x versions
3640

3741

3842
Basic usage
@@ -125,17 +129,13 @@ Due to type safety requirements a few restrictions are in place.
125129
- `replace`, `replaceCallback` and `replaceCallbackArray` do not support an array `$subject`,
126130
only simple strings.
127131
- As of 2.0, the library always uses `PREG_UNMATCHED_AS_NULL` for matching, which offers [much
128-
saner/more predictable results](#preg_unmatched_as_null). 3.x will also use the flag for
129-
`replaceCallback` and `replaceCallbackArray`. This is currently not doable due to the PHP
130-
version requirement and to keep things working the same across all PHP versions. If you use
131-
the library on a PHP 7.4+ project however we highly recommend already passing
132-
`PREG_UNMATCHED_AS_NULL` to `replaceCallback` and `replaceCallbackArray`.
132+
saner/more predictable results](#preg_unmatched_as_null). As of 3.0 the flag is also set for
133+
`replaceCallback` and `replaceCallbackArray`.
133134

134135
#### PREG_UNMATCHED_AS_NULL
135136

136137
As of 2.0, this library always uses PREG_UNMATCHED_AS_NULL for all `match*` and `isMatch*`
137-
functions (unfortunately `preg_replace_callback[_array]` only support this from PHP 7.4
138-
onwards so we cannot do the same there yet).
138+
functions. As of 3.0 it is also done for `replaceCallback` and `replaceCallbackArray`.
139139

140140
This means your matches will always contain all matching groups, either as null if unmatched
141141
or as string if it matched.

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
}
1818
],
1919
"require": {
20-
"php": "^7.2 || ^8.0"
20+
"php": "^7.4 || ^8.0"
2121
},
2222
"require-dev": {
2323
"symfony/phpunit-bridge": "^5",
@@ -36,11 +36,11 @@
3636
},
3737
"extra": {
3838
"branch-alias": {
39-
"dev-main": "2.x-dev"
39+
"dev-main": "3.x-dev"
4040
}
4141
},
4242
"scripts": {
43-
"test": "SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT=1 vendor/bin/simple-phpunit",
43+
"test": "vendor/bin/simple-phpunit",
4444
"phpstan": "phpstan analyse"
4545
}
4646
}

phpstan-baseline.neon

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,3 @@ parameters:
1515
count: 2
1616
path: src/Preg.php
1717

18-
-
19-
message: "#^Function preg_replace_callback invoked with 6 parameters, 3\\-5 required\\.$#"
20-
count: 1
21-
path: src/Preg.php
22-
23-
-
24-
message: "#^Function preg_replace_callback_array invoked with 5 parameters, 2\\-4 required\\.$#"
25-
count: 1
26-
path: src/Preg.php
27-

src/Preg.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1
130130
* @param string|string[] $pattern
131131
* @param string $subject
132132
* @param int $count Set by method
133-
* @param int $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
133+
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
134134
*/
135135
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
136136
{
@@ -142,11 +142,7 @@ public static function replaceCallback($pattern, callable $replacement, $subject
142142
throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
143143
}
144144

145-
if (PHP_VERSION_ID >= 70400) {
146-
$result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count, $flags);
147-
} else {
148-
$result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count);
149-
}
145+
$result = preg_replace_callback($pattern, $replacement, $subject, $limit, $count, $flags | PREG_UNMATCHED_AS_NULL);
150146
if ($result === null) {
151147
throw PcreException::fromFunction('preg_replace_callback', $pattern);
152148
}
@@ -158,7 +154,7 @@ public static function replaceCallback($pattern, callable $replacement, $subject
158154
* @param array<string, callable> $pattern
159155
* @param string $subject
160156
* @param int $count Set by method
161-
* @param int $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
157+
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
162158
*/
163159
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int &$count = null, int $flags = 0): string
164160
{
@@ -170,11 +166,7 @@ public static function replaceCallbackArray(array $pattern, $subject, int $limit
170166
throw new \TypeError(sprintf(static::INVALID_TYPE_MSG, gettype($subject)));
171167
}
172168

173-
if (PHP_VERSION_ID >= 70400) {
174-
$result = preg_replace_callback_array($pattern, $subject, $limit, $count, $flags);
175-
} else {
176-
$result = preg_replace_callback_array($pattern, $subject, $limit, $count);
177-
}
169+
$result = preg_replace_callback_array($pattern, $subject, $limit, $count, $flags | PREG_UNMATCHED_AS_NULL);
178170
if ($result === null) {
179171
$pattern = array_keys($pattern);
180172
throw PcreException::fromFunction('preg_replace_callback_array', $pattern);

src/Regex.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1
9595
/**
9696
* @param string|string[] $pattern
9797
* @param string $subject
98-
* @param int $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
98+
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
9999
*/
100100
public static function replaceCallback($pattern, callable $replacement, $subject, int $limit = -1, int $flags = 0): ReplaceResult
101101
{
@@ -107,7 +107,7 @@ public static function replaceCallback($pattern, callable $replacement, $subject
107107
/**
108108
* @param array<string, callable> $pattern
109109
* @param string $subject
110-
* @param int $flags PREG_OFFSET_CAPTURE or PREG_UNMATCHED_AS_NULL, only available on PHP 7.4+
110+
* @param int $flags PREG_OFFSET_CAPTURE is supported, PREG_UNMATCHED_AS_NULL is always set
111111
*/
112112
public static function replaceCallbackArray(array $pattern, $subject, int $limit = -1, int $flags = 0): ReplaceResult
113113
{

0 commit comments

Comments
 (0)