Skip to content

Commit 50c14fa

Browse files
feature #231 [PHP 8.0] Add preg_last_error_msg function (nicoSWD)
This PR was squashed before being merged into the 1.14-dev branch. Discussion ---------- [PHP 8.0] Add preg_last_error_msg function This PR adds the function `preg_last_error_msg` added in [php-src/pull/5185](php/php-src#5185) _Note_: I only tested the cases that don't require special `.ini` settings as I didn't want to set them dynamically in the test suite. Commits ------- 151806f [PHP 8.0] Add preg_last_error_msg function
2 parents 1cdbe23 + 151806f commit 50c14fa

File tree

6 files changed

+65
-4
lines changed

6 files changed

+65
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 1.15.0
2+
3+
* added `preg_last_error_msg()` to the PHP 8 polyfill
4+
15
# 1.14.0
26

37
* added PHP 8.0 polyfill

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Polyfills are provided for:
4444
- the `get_mangled_object_vars`, `mb_str_split` and `password_algos` functions
4545
introduced in PHP 7.4;
4646
- the `fdiv` function introduced in PHP 8.0;
47+
- the `preg_last_error_msg` function introduced in PHP 8.0;
4748
- the `ValueError` class introduced in PHP 8.0;
4849
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;
4950

src/Php80/Php80.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/**
1515
* @author Ion Bazan <[email protected]>
16+
* @author Nico Oelgart <[email protected]>
1617
*
1718
* @internal
1819
*/
@@ -26,6 +27,28 @@ public static function fdiv($dividend, $divisor)
2627
return (float) @($dividend / $divisor);
2728
}
2829

30+
public static function pregLastErrorMsg()
31+
{
32+
switch (preg_last_error()) {
33+
case PREG_INTERNAL_ERROR:
34+
return 'Internal error';
35+
case PREG_BAD_UTF8_ERROR:
36+
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
37+
case PREG_BAD_UTF8_OFFSET_ERROR:
38+
return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
39+
case PREG_BACKTRACK_LIMIT_ERROR:
40+
return 'Backtrack limit exhausted';
41+
case PREG_RECURSION_LIMIT_ERROR:
42+
return 'Recursion limit exhausted';
43+
case PREG_JIT_STACKLIMIT_ERROR:
44+
return 'JIT stack limit exhausted';
45+
case PREG_NO_ERROR:
46+
return 'No error';
47+
default:
48+
return 'Unknown error';
49+
}
50+
}
51+
2952
private static function floatArg($value, $caller, $pos)
3053
{
3154
if (\is_float($value)) {

src/Php80/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This component provides functions added to PHP 8.0 core:
66
- [`fdiv`](https://php.net/fdiv) (only for PHP 7.0+)
77
- `ValueError` class
88
- `FILTER_VALIDATE_BOOL` constant
9+
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)
910

1011
More information can be found in the
1112
[main Polyfill README](https://github.com/symfony/polyfill/blob/master/README.md).

src/Php80/bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
function fdiv($dividend, $divisor) { return p\Php80::fdiv($dividend, $divisor); }
1717
}
1818

19+
if (!function_exists('preg_last_error_msg')) {
20+
function preg_last_error_msg() { return p\Php80::pregLastErrorMsg(); }
21+
}
22+
1923
if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
2024
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
2125
}

tests/Php80/Php80Test.php

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@
1414
use PHPUnit\Framework\TestCase;
1515

1616
/**
17+
* @requires PHP 7.0
18+
*
1719
* @author Ion Bazan <[email protected]>
20+
* @author Nico Oelgart <[email protected]>
1821
*/
1922
class Php80Test extends TestCase
2023
{
2124
/**
22-
* @requires PHP 7.0
2325
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
2426
* @dataProvider fdivProvider
2527
*/
@@ -32,7 +34,6 @@ public function testFdiv($expected, $divident, $divisor)
3234
}
3335

3436
/**
35-
* @requires PHP 7.0
3637
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
3738
* @dataProvider nanFdivProvider
3839
*/
@@ -42,7 +43,6 @@ public function testFdivNan($divident, $divisor)
4243
}
4344

4445
/**
45-
* @requires PHP 7.0
4646
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
4747
* @dataProvider invalidFloatProvider
4848
*/
@@ -53,14 +53,42 @@ public function testFdivTypeError($divident, $divisor)
5353
}
5454

5555
/**
56-
* @requires PHP 7.0
5756
*/
5857
public function testFilterValidateBool()
5958
{
6059
$this->assertTrue(\defined('FILTER_VALIDATE_BOOL'));
6160
$this->assertSame(FILTER_VALIDATE_BOOLEAN, FILTER_VALIDATE_BOOL);
6261
}
6362

63+
/**
64+
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
65+
*/
66+
public function testPregNoError()
67+
{
68+
$this->assertSame('No error', preg_last_error_msg());
69+
}
70+
71+
/**
72+
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
73+
*/
74+
public function testPregMalformedUtfError()
75+
{
76+
@preg_split('/a/u', "a\xff");
77+
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', preg_last_error_msg());
78+
}
79+
80+
/**
81+
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
82+
*/
83+
public function testPregMalformedUtf8Offset()
84+
{
85+
@preg_match('/a/u', "\xE3\x82\xA2", $m, 0, 1);
86+
$this->assertSame(
87+
'The offset did not correspond to the beginning of a valid UTF-8 code point',
88+
preg_last_error_msg()
89+
);
90+
}
91+
6492
public function fdivProvider()
6593
{
6694
return array(

0 commit comments

Comments
 (0)