-
Notifications
You must be signed in to change notification settings - Fork 137
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
Adding preg_replace to special cases functions #72
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1074,4 +1074,5 @@ | |
'json_decode', | ||
'apc_fetch', | ||
'apcu_fetch', | ||
'preg_replace', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,6 @@ | |
return [ | ||
'json_decode', | ||
'apc_fetch', | ||
'apcu_fetch' | ||
'apcu_fetch', | ||
'preg_replace', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
preg_replace("/([\s,]+)/u", "foo", "\xc3\x28"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
|
||
namespace Safe\Exceptions; | ||
|
||
class PcreException extends \Exception implements SafeExceptionInterface | ||
{ | ||
public static function createFromPhpError(): self | ||
{ | ||
$errMsg = array_flip(get_defined_constants(true)['pcre'])[preg_last_error()]; | ||
return new static($errMsg, \preg_last_error()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,9 +7,12 @@ | |
|
||
namespace Safe; | ||
|
||
use const PREG_NO_ERROR; | ||
use Safe\Exceptions\ApcException; | ||
use Safe\Exceptions\ApcuException; | ||
use Safe\Exceptions\JsonException; | ||
use Safe\Exceptions\PcreException; | ||
use function var_dump; | ||
|
||
/** | ||
* Wrapper for json_decode that throws when an error occurs. | ||
|
@@ -73,3 +76,90 @@ 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); | ||
var_dump($result); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙃 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gee... committed too quickly it seems :) |
||
if (preg_last_error() !== PREG_NO_ERROR) { | ||
// TODO: PcreException should use code here: http://php.net/manual/en/function.preg-last-error.php | ||
throw PcreException::createFromPhpError(); | ||
} | ||
return $result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe cs-fixer could help?