Skip to content

[PHP 8.0] Add preg_last_error_msg function #231

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.15.0

* added `preg_last_error_msg()` to the PHP 8 polyfill

# 1.14.0

* added PHP 8.0 polyfill
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Polyfills are provided for:
- the `get_mangled_object_vars`, `mb_str_split` and `password_algos` functions
introduced in PHP 7.4;
- the `fdiv` function introduced in PHP 8.0;
- the `preg_last_error_msg` function introduced in PHP 8.0;
- the `ValueError` class introduced in PHP 8.0;
- the `FILTER_VALIDATE_BOOL` constant introduced in PHP 8.0;

Expand Down
23 changes: 23 additions & 0 deletions src/Php80/Php80.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @author Ion Bazan <[email protected]>
* @author Nico Oelgart <[email protected]>
*
* @internal
*/
Expand All @@ -26,6 +27,28 @@ public static function fdiv($dividend, $divisor)
return (float) @($dividend / $divisor);
}

public static function pregLastErrorMsg()
{
switch (preg_last_error()) {
case PREG_INTERNAL_ERROR:
return 'Internal error';
case PREG_BAD_UTF8_ERROR:
return 'Malformed UTF-8 characters, possibly incorrectly encoded';
case PREG_BAD_UTF8_OFFSET_ERROR:
return 'The offset did not correspond to the beginning of a valid UTF-8 code point';
case PREG_BACKTRACK_LIMIT_ERROR:
return 'Backtrack limit exhausted';
case PREG_RECURSION_LIMIT_ERROR:
return 'Recursion limit exhausted';
case PREG_JIT_STACKLIMIT_ERROR:
return 'JIT stack limit exhausted';
case PREG_NO_ERROR:
return 'No error';
default:
return 'Unknown error';
}
}

private static function floatArg($value, $caller, $pos)
{
if (\is_float($value)) {
Expand Down
1 change: 1 addition & 0 deletions src/Php80/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This component provides functions added to PHP 8.0 core:
- [`fdiv`](https://php.net/fdiv) (only for PHP 7.0+)
- `ValueError` class
- `FILTER_VALIDATE_BOOL` constant
- [`preg_last_error_msg`](https://php.net/preg_last_error_msg)

More information can be found in the
[main Polyfill README](https://github.com/symfony/polyfill/blob/master/README.md).
Expand Down
4 changes: 4 additions & 0 deletions src/Php80/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
function fdiv($dividend, $divisor) { return p\Php80::fdiv($dividend, $divisor); }
}

if (!function_exists('preg_last_error_msg')) {
function preg_last_error_msg() { return p\Php80::pregLastErrorMsg(); }
}

if (!defined('FILTER_VALIDATE_BOOL') && defined('FILTER_VALIDATE_BOOLEAN')) {
define('FILTER_VALIDATE_BOOL', FILTER_VALIDATE_BOOLEAN);
}
Expand Down
36 changes: 32 additions & 4 deletions tests/Php80/Php80Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@
use PHPUnit\Framework\TestCase;

/**
* @requires PHP 7.0
*
* @author Ion Bazan <[email protected]>
* @author Nico Oelgart <[email protected]>
*/
class Php80Test extends TestCase
{
/**
* @requires PHP 7.0
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
* @dataProvider fdivProvider
*/
Expand All @@ -32,7 +34,6 @@ public function testFdiv($expected, $divident, $divisor)
}

/**
* @requires PHP 7.0
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
* @dataProvider nanFdivProvider
*/
Expand All @@ -42,7 +43,6 @@ public function testFdivNan($divident, $divisor)
}

/**
* @requires PHP 7.0
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
* @dataProvider invalidFloatProvider
*/
Expand All @@ -53,14 +53,42 @@ public function testFdivTypeError($divident, $divisor)
}

/**
* @requires PHP 7.0
*/
public function testFilterValidateBool()
{
$this->assertTrue(\defined('FILTER_VALIDATE_BOOL'));
$this->assertSame(FILTER_VALIDATE_BOOLEAN, FILTER_VALIDATE_BOOL);
}

/**
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
*/
public function testPregNoError()
{
$this->assertSame('No error', preg_last_error_msg());
}

/**
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
*/
public function testPregMalformedUtfError()
{
@preg_split('/a/u', "a\xff");
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', preg_last_error_msg());
}

/**
* @covers \Symfony\Polyfill\Php80\Php80::pregLastErrorMsg
*/
public function testPregMalformedUtf8Offset()
{
@preg_match('/a/u', "\xE3\x82\xA2", $m, 0, 1);
$this->assertSame(
'The offset did not correspond to the beginning of a valid UTF-8 code point',
preg_last_error_msg()
);
}

public function fdivProvider()
{
return array(
Expand Down