Skip to content

Commit 128c46a

Browse files
committed
Remove some redundant regex escapes
1 parent 7a76300 commit 128c46a

File tree

4 files changed

+36
-35
lines changed

4 files changed

+36
-35
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
"php": ">=8.2"
2424
},
2525
"require-dev": {
26-
"friendsofphp/php-cs-fixer": "^3.73",
26+
"friendsofphp/php-cs-fixer": "^3.74",
2727
"jbboehr/handlebars-spec": "dev-master",
28-
"phpstan/phpstan": "^2.1.10",
28+
"phpstan/phpstan": "^2.1.11",
2929
"phpunit/phpunit": "^11.5"
3030
},
3131
"autoload": {

composer.lock

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Parser.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,10 @@ protected static function getExpression(string $v, Context $context, int|string
109109
$ret[] = $levels;
110110
}
111111

112-
if (preg_match('/\\]/', $v)) {
112+
if (str_contains($v, ']')) {
113113
preg_match_all(Token::VARNAME_SEARCH, $v, $matchedAll);
114114
} else {
115-
preg_match_all('/([^\\.\\/]+)/', $v, $matchedAll);
115+
preg_match_all('/([^.\\/]+)/', $v, $matchedAll);
116116
}
117117

118118
if ($v !== '.') {
@@ -239,7 +239,7 @@ protected static function advancedVariable(array $vars, Context $context, string
239239
continue;
240240
}
241241

242-
if (preg_match('/^((\\[([^\\]]+)\\])|([^=^["\']+))=(.+)$/', $var, $m)) {
242+
if (preg_match('/^((\\[([^\\]]+)\\])|([^=^\\["\']+))=(.+)$/', $var, $m)) {
243243
$idx = $m[3] ? $m[3] : $m[4];
244244
$var = $m[5];
245245
// handle foo=(...)
@@ -251,13 +251,13 @@ protected static function advancedVariable(array $vars, Context $context, string
251251

252252
if (!preg_match("/^(\"|\\\\')(.*)(\"|\\\\')$/", $var)) {
253253
// foo] Rule 1: no starting [ or [ not start from head
254-
if (preg_match('/^[^\\[\\.]+[\\]\\[]/', $var)
254+
if (preg_match('/^[^\\[.]+[\\[\\]]/', $var)
255255
// [bar Rule 2: no ending ] or ] not in the end
256-
|| preg_match('/[\\[\\]][^\\]\\.]+$/', $var)
256+
|| preg_match('/[\\[\\]][^].]+$/', $var)
257257
// ]bar. Rule 3: middle ] not before .
258-
|| preg_match('/\\][^\\]\\[\\.]+\\./', $var)
258+
|| preg_match('/][^\\[.\\]]+\\./', $var)
259259
// .foo[ Rule 4: middle [ not after .
260-
|| preg_match('/\\.[^\\]\\[\\.]+\\[/', preg_replace('/^(..\\/)+/', '', preg_replace('/\\[[^\\]]+\\]/', '[XXX]', $var)))
260+
|| preg_match('/\\.[^\\[.\\]]+\\[/', preg_replace('/^(..\\/)+/', '', preg_replace('/\\[[^\\]]+\\]/', '[XXX]', $var)))
261261
) {
262262
$context->error[] = "Wrong variable naming as '$var' in $token !";
263263
} else {
@@ -337,7 +337,7 @@ protected static function detectQuote(string $string): ?array
337337
protected static function analyze(string $token, Context $context): array
338338
{
339339
// Do not break quoted strings. Also, allow escaped quotes inside them.
340-
$count = preg_match_all('/(\s*)([^"\s]*"(\\\\\\\\.|[^"])*"|[^\'\s]*\'(\\\\\\\\.|[^\'])*\'|\\S+)/', $token, $matches);
340+
$count = preg_match_all('/(\s*)([^"\s]*"(\\\\\\\\.|[^"])*"|[^\'\s]*\'(\\\\\\\\.|[^\'])*\'|\S+)/', $token, $matches);
341341
// Parse arguments and deal with "..." or [...] or (...) or \'...\' or |...|
342342
if ($count > 0) {
343343
$vars = [];

tests/HandlebarsSpecTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,12 @@ public function testSpecs($spec)
182182
if (!isset($func['php'])) {
183183
$this->markTestIncomplete("Skip [{$spec['file']}#{$spec['description']}]#{$spec['no']} , no PHP helper code provided for this case.");
184184
}
185-
$hname = preg_replace('/\\.|\\//', '_', "custom_helper_{$spec['no']}_{$tested}_$name");
185+
$hname = preg_replace('/[.\\/]/', '_', "custom_helper_{$spec['no']}_{$tested}_$name");
186186
$helpers[$name] = $hname;
187187
$helper = patch_safestring(
188188
preg_replace('/function/', "function $hname", $func['php'], 1),
189189
);
190+
$helper = str_replace('new \Handlebars\SafeString', 'new \DevTheorem\Handlebars\SafeString', $helper);
190191
$helper = str_replace('$options[\'data\']', '$options->data', $helper);
191192
$helper = str_replace('$options[\'hash\']', '$options->hash', $helper);
192193
$helper = str_replace('$arguments[count($arguments)-1][\'name\'];', '$arguments[count($arguments)-1]->name;', $helper);

0 commit comments

Comments
 (0)