Skip to content
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

Do not support array of patterns in Preg methods #6184

Merged
merged 1 commit into from Dec 16, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/Preg.php
Expand Up @@ -65,15 +65,11 @@ public static function matchAll(string $pattern, string $subject, ?array &$match
}

/**
* @param string|string[] $pattern
* @param string|string[] $replacement
* @param string|string[] $subject
*
* @throws PregException
*
* @return string|string[]
*/
public static function replace($pattern, $replacement, $subject, int $limit = -1, ?int &$count = null)
public static function replace(string $pattern, string $replacement, $subject, int $limit = -1, ?int &$count = null): string
{
$result = @preg_replace(self::addUtf8Modifier($pattern), $replacement, $subject, $limit, $count);
if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
Expand All @@ -89,14 +85,9 @@ public static function replace($pattern, $replacement, $subject, int $limit = -1
}

/**
* @param string|string[] $pattern
* @param string|string[] $subject
*
* @throws PregException
*
* @return string|string[]
*/
public static function replaceCallback($pattern, callable $callback, $subject, int $limit = -1, ?int &$count = null)
public static function replaceCallback(string $pattern, callable $callback, string $subject, int $limit = -1, ?int &$count = null): string
{
$result = @preg_replace_callback(self::addUtf8Modifier($pattern), $callback, $subject, $limit, $count);
if (null !== $result && PREG_NO_ERROR === preg_last_error()) {
Expand Down
12 changes: 1 addition & 11 deletions tests/PregTest.php
Expand Up @@ -125,7 +125,7 @@ public function testPatternsValidation(string $pattern, ?int $expected = null, ?

try {
$buffer = "The quick brown \xFF\x00\\xXX jumps over the lazy dog\n";
$actual = $buffer !== Preg::replace((array) $pattern, 'abc', $buffer);
$actual = $buffer !== Preg::replace($pattern, 'abc', $buffer);
} catch (\Exception $ex) {
$setup();

Expand Down Expand Up @@ -176,7 +176,6 @@ public function testReplaceFailing(): void
* @param string|string[] $subject
*
* @dataProvider provideCommonCases
* @dataProvider provideArrayOfPatternsCases
*/
public function testReplace($pattern, $subject): void
{
Expand All @@ -199,7 +198,6 @@ public function testReplaceCallbackFailing(): void
* @param string|string[] $subject
*
* @dataProvider provideCommonCases
* @dataProvider provideArrayOfPatternsCases
*/
public function testReplaceCallback($pattern, $subject): void
{
Expand All @@ -222,14 +220,6 @@ public function provideCommonCases(): array
];
}

public function provideArrayOfPatternsCases(): array
{
return [
[['/à/', '/í/'], 'Tàíl'],
[['/'.\chr(174).'/', '/'.\chr(224).'/'], 'foo'],
];
}

public function testSplitFailing(): void
{
$this->expectException(PregException::class);
Expand Down