Skip to content

Commit

Permalink
chore: Preg - drop half-support for array-pattern (#7976)
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus committed May 1, 2024
1 parent 63037b2 commit f81d4f8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 85 deletions.
25 changes: 0 additions & 25 deletions dev-tools/phpstan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -861,36 +861,11 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Linter/ProcessLinter.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:addUtf8Modifier\\(\\) should return array\\<string\\>\\|string but returns array\\<array\\<string\\>\\|string\\>\\.$#',
'count' => 1,
'path' => __DIR__ . '/../../src/Preg.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:removeUtf8Modifier\\(\\) should return array\\<string\\>\\|string but returns array\\<array\\<string\\>\\|string\\>\\.$#',
'count' => 1,
'path' => __DIR__ . '/../../src/Preg.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Preg\\:\\:replace\\(\\) should return string but returns array\\<int, string\\>\\|string\\.$#',
'count' => 2,
'path' => __DIR__ . '/../../src/Preg.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#1 \\$pattern of function preg_match expects string, array\\<string\\>\\|string given\\.$#',
'count' => 2,
'path' => __DIR__ . '/../../src/Preg.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#1 \\$pattern of function preg_match_all expects string, array\\<string\\>\\|string given\\.$#',
'count' => 2,
'path' => __DIR__ . '/../../src/Preg.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#1 \\$pattern of function preg_split expects string, array\\<string\\>\\|string given\\.$#',
'count' => 2,
'path' => __DIR__ . '/../../src/Preg.php',
];
$ignoreErrors[] = [
'message' => '#^Parameter \\#2 \\$offset of function substr expects int, int\\|false given\\.$#',
'count' => 1,
Expand Down
93 changes: 33 additions & 60 deletions src/Preg.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static function match(string $pattern, string $subject, ?array &$matches
return 1 === $result;
}

throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, (array) $pattern);
throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
}

/**
Expand Down Expand Up @@ -97,7 +97,7 @@ public static function matchAll(string $pattern, string $subject, ?array &$match
return $result;
}

throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, (array) $pattern);
throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
}

/**
Expand All @@ -119,7 +119,7 @@ public static function replace(string $pattern, string $replacement, $subject, i
return $result;
}

throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, (array) $pattern);
throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
}

/**
Expand All @@ -139,7 +139,7 @@ public static function replaceCallback(string $pattern, callable $callback, stri
return $result;
}

throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, (array) $pattern);
throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
}

/**
Expand All @@ -159,34 +159,16 @@ public static function split(string $pattern, string $subject, int $limit = -1,
return $result;
}

throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, (array) $pattern);
throw self::newPregException(preg_last_error(), preg_last_error_msg(), __METHOD__, $pattern);
}

/**
* @param string|string[] $pattern
*
* @return string|string[]
*/
private static function addUtf8Modifier($pattern)
private static function addUtf8Modifier(string $pattern): string
{
if (\is_array($pattern)) {
return array_map(__METHOD__, $pattern);
}

return $pattern.'u';
}

/**
* @param string|string[] $pattern
*
* @return string|string[]
*/
private static function removeUtf8Modifier($pattern)
private static function removeUtf8Modifier(string $pattern): string
{
if (\is_array($pattern)) {
return array_map(__METHOD__, $pattern);
}

if ('' === $pattern) {
return '';
}
Expand All @@ -199,44 +181,35 @@ private static function removeUtf8Modifier($pattern)
}

/**
* Create PregException.
*
* Create the generic PregException message and if possible due to finding
* an invalid pattern, tell more about such kind of error in the message.
*
* @param string[] $patterns
* Create the generic PregException message and tell more about such kind of error in the message.
*/
private static function newPregException(int $error, string $errorMsg, string $method, array $patterns): PregException
private static function newPregException(int $error, string $errorMsg, string $method, string $pattern): PregException
{
foreach ($patterns as $pattern) {
$result = null;
$errorMessage = null;

try {
$result = ExecutorWithoutErrorHandler::execute(static fn () => preg_match($pattern, ''));
} catch (ExecutorWithoutErrorHandlerException $e) {
$result = false;
$errorMessage = $e->getMessage();
}

if (false !== $result) {
continue;
}

$code = preg_last_error();

$message = sprintf(
'(code: %d) %s',
$code,
preg_replace('~preg_[a-z_]+[()]{2}: ~', '', $errorMessage)
);

return new PregException(
sprintf('%s(): Invalid PCRE pattern "%s": %s (version: %s)', $method, $pattern, $message, PCRE_VERSION),
$code
);
$result = null;
$errorMessage = null;

try {
$result = ExecutorWithoutErrorHandler::execute(static fn () => preg_match($pattern, ''));
} catch (ExecutorWithoutErrorHandlerException $e) {
$result = false;
$errorMessage = $e->getMessage();
}

return new PregException(sprintf('Error occurred when calling %s: %s.', $method, $errorMsg), $error);
if (false !== $result) {
return new PregException(sprintf('Unknown error occurred when calling %s: %s.', $method, $errorMsg), $error);
}

$code = preg_last_error();

$message = sprintf(
'(code: %d) %s',
$code,
preg_replace('~preg_[a-z_]+[()]{2}: ~', '', $errorMessage)
);

return new PregException(
sprintf('%s(): Invalid PCRE pattern "%s": %s (version: %s)', $method, $pattern, $message, PCRE_VERSION),
$code
);
}
}

0 comments on commit f81d4f8

Please sign in to comment.