Skip to content

Commit fac43a8

Browse files
committed
Replace a few unnecessary preg_match calls
1 parent 0aa6a3a commit fac43a8

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

src/ParserAbstract.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ protected function locInfo(int $tokenStartPos, int $tokenEndPos): SourceLocation
513513

514514
protected function id(string $token): string
515515
{
516-
if (preg_match('/^\\[.*]$/', $token)) {
516+
if ($token !== '' && $token[0] === '[' && $token[-1] === ']') {
517517
return substr($token, 1, -1);
518518
} else {
519519
return $token;
@@ -562,7 +562,7 @@ protected function prepareMustache(
562562
): MustacheStatement {
563563
$escapeFlag = $open[3] ?? $open[2] ?? '';
564564
$escaped = $escapeFlag !== '{' && $escapeFlag !== '&';
565-
$decorator = preg_match('/\\*/', $open);
565+
$decorator = str_contains($open, '*');
566566

567567
return new MustacheStatement(
568568
type: $decorator ? 'Decorator' : 'MustacheStatement',
@@ -613,7 +613,7 @@ protected function preparePath(bool $data, ?SubExpression $sexpr, array $parts,
613613
$original .= ($separator ?? '') . $part;
614614

615615
if (!$isLiteral && ($part === '..' || $part === '.' || $part === 'this')) {
616-
if (count($tail) > 0) {
616+
if ($tail !== []) {
617617
$msg = $this->getNodeError("Invalid path: $original", new Node('', $loc));
618618
throw new \Exception($msg);
619619
} elseif ($part === '..') {
@@ -673,7 +673,7 @@ protected function prepareBlock(
673673
$this->validateClose($openBlock, $close);
674674
}
675675

676-
$decorator = preg_match('/\\*/', $openBlock->open);
676+
$decorator = str_contains($openBlock->open, '*');
677677

678678
$program->blockParams = $openBlock->blockParams;
679679

src/WhitespaceControl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,11 +134,12 @@ private function visitBlock(BlockStatement|PartialBlockStatement $block): StripI
134134
}
135135

136136
// Find the inverse program that is involved with whitespace stripping.
137-
$program = $block instanceof BlockStatement
137+
$isBlockStatement = $block instanceof BlockStatement;
138+
$program = $isBlockStatement
138139
? ($block->program ?? $block->inverse)
139140
: $block->program;
140141

141-
$inverse = ($block instanceof BlockStatement && $block->program && $block->inverse)
142+
$inverse = ($isBlockStatement && $block->program && $block->inverse)
142143
? $block->inverse
143144
: null;
144145
$firstInverse = $inverse;

tests/ParserTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace DevTheorem\HandlebarsParser\Test;
44

5-
use DevTheorem\HandlebarsParser\Ast\Program;
65
use DevTheorem\HandlebarsParser\ParserFactory;
76
use PHPUnit\Framework\Attributes\DataProvider;
87
use PHPUnit\Framework\TestCase;
@@ -296,8 +295,8 @@ public function testSpecs(array $spec): void
296295
$parser = (new ParserFactory())->create();
297296

298297
try {
299-
$result = $parser->parse($spec['template']);
300-
$this->assertInstanceOf(Program::class, $result);
298+
$parser->parse($spec['template']);
299+
$this->expectNotToPerformAssertions();
301300
} catch (\Exception $e) {
302301
if (isset($spec['exception'])) {
303302
if (is_string($spec['exception'])) {

0 commit comments

Comments
 (0)