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

Use Language RegExp attribute #277

Merged
merged 1 commit into from
Jan 9, 2023
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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"require-dev": {
"nette/tester": "~2.0",
"tracy/tracy": "^2.3",
"phpstan/phpstan": "^1.0"
"phpstan/phpstan": "^1.0",
"jetbrains/phpstorm-attributes": "dev-master"
},
"conflict": {
"nette/di": "<3.0.6"
Expand Down
36 changes: 28 additions & 8 deletions src/Utils/Strings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Nette\Utils;

use Jetbrains\PhpStorm\Language;
use Nette;
use function is_array, is_object, strlen;

Expand Down Expand Up @@ -485,8 +486,12 @@ private static function pos(string $haystack, string $needle, int $nth = 1): ?in
* Splits a string into array by the regular expression. Parenthesized expression in the delimiter are captured.
* Parameter $flags can be any combination of PREG_SPLIT_NO_EMPTY and PREG_OFFSET_CAPTURE flags.
*/
public static function split(string $subject, string $pattern, int $flags = 0): array
{
public static function split(
string $subject,
#[Language('RegExp')]
string $pattern,
int $flags = 0
): array {
return self::pcre('preg_split', [$pattern, $subject, -1, $flags | PREG_SPLIT_DELIM_CAPTURE]);
}

Expand All @@ -495,8 +500,13 @@ public static function split(string $subject, string $pattern, int $flags = 0):
* Checks if given string matches a regular expression pattern and returns an array with first found match and each subpattern.
* Parameter $flags can be any combination of PREG_OFFSET_CAPTURE and PREG_UNMATCHED_AS_NULL flags.
*/
public static function match(string $subject, string $pattern, int $flags = 0, int $offset = 0): ?array
{
public static function match(
string $subject,
#[Language('RegExp')]
string $pattern,
int $flags = 0,
int $offset = 0
): ?array {
if ($offset > strlen($subject)) {
return null;
}
Expand All @@ -511,8 +521,13 @@ public static function match(string $subject, string $pattern, int $flags = 0, i
* Finds all occurrences matching regular expression pattern and returns a two-dimensional array. Result is array of matches (ie uses by default PREG_SET_ORDER).
* Parameter $flags can be any combination of PREG_OFFSET_CAPTURE, PREG_UNMATCHED_AS_NULL and PREG_PATTERN_ORDER flags.
*/
public static function matchAll(string $subject, string $pattern, int $flags = 0, int $offset = 0): array
{
public static function matchAll(
string $subject,
#[Language('RegExp')]
string $pattern,
int $flags = 0,
int $offset = 0
): array {
if ($offset > strlen($subject)) {
return [];
}
Expand All @@ -531,8 +546,13 @@ public static function matchAll(string $subject, string $pattern, int $flags = 0
* @param string|array $pattern
* @param string|callable $replacement
*/
public static function replace(string $subject, $pattern, $replacement = '', int $limit = -1): string
{
public static function replace(
string $subject,
#[Language('RegExp')]
$pattern,
$replacement = '',
int $limit = -1
): string {
if (is_object($replacement) || is_array($replacement)) {
if (!is_callable($replacement, false, $textual)) {
throw new Nette\InvalidStateException("Callback '$textual' is not callable.");
Expand Down