Skip to content

Commit

Permalink
Adding preg_replace to special cases functions (#72)
Browse files Browse the repository at this point in the history
* Adding preg_replace to special cases functions
  • Loading branch information
moufmouf committed Jan 10, 2019
1 parent 3c9f28a commit 3980192
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 9 deletions.
6 changes: 0 additions & 6 deletions generated/Exceptions/PcreException.php

This file was deleted.

1 change: 1 addition & 0 deletions generated/functionsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -1074,4 +1074,5 @@
'json_decode',
'apc_fetch',
'apcu_fetch',
'preg_replace',
];
3 changes: 2 additions & 1 deletion generator/config/specialCasesFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
return [
'json_decode',
'apc_fetch',
'apcu_fetch'
'apcu_fetch',
'preg_replace',
];
4 changes: 2 additions & 2 deletions generator/tests/GeneratedFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testPregMatch()
require_once __DIR__.'/../../generated/pcre.php';
require_once __DIR__.'/../../lib/Exceptions/SafeExceptionInterface.php';
require_once __DIR__.'/../../lib/Exceptions/AbstractSafeException.php';
require_once __DIR__.'/../../generated/Exceptions/PcreException.php';
require_once __DIR__.'/../../lib/Exceptions/PcreException.php';


$url = 'https://open.spotify.com/track/0nCqpKBrvDchO1BIvt7DTR?si=iLUKDfkLSy-IpnLA7qImnw';
Expand Down Expand Up @@ -83,7 +83,7 @@ public function testPregSplit()
require_once __DIR__.'/../../generated/pcre.php';
require_once __DIR__.'/../../lib/Exceptions/SafeExceptionInterface.php';
require_once __DIR__.'/../../lib/Exceptions/AbstractSafeException.php';
require_once __DIR__.'/../../generated/Exceptions/PcreException.php';
require_once __DIR__.'/../../lib/Exceptions/PcreException.php';

$keywords = preg_split("/[\s,]+/", "hypertext language, programming", null);
$this->assertSame(['hypertext', 'language', 'programming'], $keywords);
Expand Down
21 changes: 21 additions & 0 deletions generator/tests/SpecialCasesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Safe;

use PHPUnit\Framework\TestCase;
use Safe\Exceptions\PcreException;

class SpecialCasesTest extends TestCase
{
public function testPregReplace()
{
require_once __DIR__.'/../../lib/special_cases.php';
require_once __DIR__.'/../../lib/Exceptions/SafeExceptionInterface.php';
require_once __DIR__.'/../../lib/Exceptions/AbstractSafeException.php';
require_once __DIR__.'/../../lib/Exceptions/PcreException.php';

$this->expectException(PcreException::class);
$this->expectExceptionMessage('PREG_BAD_UTF8_ERROR: Invalid UTF8 character');
preg_replace("/([\s,]+)/u", "foo", "\xc3\x28");
}
}
21 changes: 21 additions & 0 deletions lib/Exceptions/PcreException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php


namespace Safe\Exceptions;

class PcreException extends \Exception implements SafeExceptionInterface
{
public static function createFromPhpError(): self
{
$errorMap = [
PREG_INTERNAL_ERROR => 'PREG_INTERNAL_ERROR: Internal error',
PREG_BACKTRACK_LIMIT_ERROR => 'PREG_BACKTRACK_LIMIT_ERROR: Backtrack limit reached',
PREG_RECURSION_LIMIT_ERROR => 'PREG_RECURSION_LIMIT_ERROR: Recursion limit reached',
PREG_BAD_UTF8_ERROR => 'PREG_BAD_UTF8_ERROR: Invalid UTF8 character',
PREG_BAD_UTF8_OFFSET_ERROR => 'PREG_BAD_UTF8_OFFSET_ERROR',
PREG_JIT_STACKLIMIT_ERROR => 'PREG_JIT_STACKLIMIT_ERROR',
];
$errMsg = $errorMap[preg_last_error()] ?? 'Unknown PCRE error: '.preg_last_error();
return new static($errMsg, \preg_last_error());
}
}
87 changes: 87 additions & 0 deletions lib/special_cases.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Safe;

use const PREG_NO_ERROR;
use Safe\Exceptions\ApcException;
use Safe\Exceptions\ApcuException;
use Safe\Exceptions\JsonException;
use Safe\Exceptions\PcreException;

/**
* Wrapper for json_decode that throws when an error occurs.
Expand Down Expand Up @@ -73,3 +75,88 @@ function apcu_fetch($key)
}
return $result;
}

/**
* Searches subject for matches to
* pattern and replaces them with
* replacement.
*
* @param mixed $pattern The pattern to search for. It can be either a string or an array with
* strings.
*
* Several PCRE modifiers
* are also available.
* @param mixed $replacement The string or an array with strings to replace. If this parameter is a
* string and the pattern parameter is an array,
* all patterns will be replaced by that string. If both
* pattern and replacement
* parameters are arrays, each pattern will be
* replaced by the replacement counterpart. If
* there are fewer elements in the replacement
* array than in the pattern array, any extra
* patterns will be replaced by an empty string.
*
* replacement may contain references of the form
* \\n or
* $n, with the latter form
* being the preferred one. Every such reference will be replaced by the text
* captured by the n'th parenthesized pattern.
* n can be from 0 to 99, and
* \\0 or $0 refers to the text matched
* by the whole pattern. Opening parentheses are counted from left to right
* (starting from 1) to obtain the number of the capturing subpattern.
* To use backslash in replacement, it must be doubled
* ("\\\\" PHP string).
*
* When working with a replacement pattern where a backreference is
* immediately followed by another number (i.e.: placing a literal number
* immediately after a matched pattern), you cannot use the familiar
* \\1 notation for your backreference.
* \\11, for example, would confuse
* preg_replace since it does not know whether you
* want the \\1 backreference followed by a literal
* 1, or the \\11 backreference
* followed by nothing. In this case the solution is to use
* ${1}1. This creates an isolated
* $1 backreference, leaving the 1
* as a literal.
*
* When using the deprecated e modifier, this function escapes
* some characters (namely ', ",
* \ and NULL) in the strings that replace the
* backreferences. This is done to ensure that no syntax errors arise
* from backreference usage with either single or double quotes (e.g.
* 'strlen(\'$1\')+strlen("$2")'). Make sure you are
* aware of PHP's string
* syntax to know exactly how the interpreted string will look.
* @param string|array $subject The string or an array with strings to search and replace.
*
* If subject is an array, then the search and
* replace is performed on every entry of subject,
* and the return value is an array as well.
* @param int $limit The maximum possible replacements for each pattern in each
* subject string. Defaults to
* -1 (no limit).
* @param int $count If specified, this variable will be filled with the number of
* replacements done.
* @return string|array|null preg_replace returns an array if the
* subject parameter is an array, or a string
* otherwise.
*
* If matches are found, the new subject will
* be returned, otherwise subject will be
* returned unchanged.
*
* Returns a file pointer resource on success, .
* @throws PcreException
*
*/
function preg_replace($pattern, $replacement, $subject, int $limit = -1, int &$count = null)
{
error_clear_last();
$result = \preg_replace($pattern, $replacement, $subject, $limit, $count);
if (preg_last_error() !== PREG_NO_ERROR) {
throw PcreException::createFromPhpError();
}
return $result;
}
1 change: 1 addition & 0 deletions rector-migrate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1075,3 +1075,4 @@ services:
json_decode: 'Safe\json_decode'
apc_fetch: 'Safe\apc_fetch'
apcu_fetch: 'Safe\apcu_fetch'
preg_replace: 'Safe\preg_replace'

0 comments on commit 3980192

Please sign in to comment.