From ef7c1e243ba325cdccd0264e86bff74795a704e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A1chym=20Tou=C5=A1ek?= Date: Mon, 14 Feb 2022 10:43:09 +0100 Subject: [PATCH] Use Language RegExp attribute --- composer.json | 3 ++- src/Utils/Strings.php | 36 ++++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index 1ca7380fa..e6fcfbd25 100644 --- a/composer.json +++ b/composer.json @@ -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" diff --git a/src/Utils/Strings.php b/src/Utils/Strings.php index 1314c62fc..8dca339c0 100644 --- a/src/Utils/Strings.php +++ b/src/Utils/Strings.php @@ -9,6 +9,7 @@ namespace Nette\Utils; +use Jetbrains\PhpStorm\Language; use Nette; use function is_array, is_object, strlen; @@ -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]); } @@ -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; } @@ -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 []; } @@ -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.");