Skip to content

Commit 59caa32

Browse files
committed
Make Options, HelperOptions, and SafeString final
1 parent 6238170 commit 59caa32

3 files changed

Lines changed: 13 additions & 14 deletions

File tree

src/HelperOptions.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum Scope
1414
/**
1515
* @phpstan-import-type Template from Handlebars
1616
*/
17-
class HelperOptions
17+
final class HelperOptions
1818
{
1919
/**
2020
* @param array<mixed> $data
@@ -46,7 +46,7 @@ public function hasPartial(string $name): bool
4646
* Typically used alongside hasPartial() to implement lazy partial loading.
4747
* @param Template $partial
4848
*/
49-
public function registerPartial(string $name, \Closure $partial): void
49+
public function registerPartial(string $name, Closure $partial): void
5050
{
5151
$this->cx->partials[$name] = $partial;
5252
}
@@ -69,6 +69,7 @@ public function fn(mixed $context = Scope::Use, mixed $data = null): string
6969
if ($this->inv === null) {
7070
throw new \Exception('fn() is not supported for inline helpers');
7171
}
72+
// Occurs when blockHelperMissing routes a truthy context through fn() for an inverted block.
7273
return '';
7374
}
7475
return $this->invokeBlock($this->cb, $context, $data);
@@ -85,7 +86,7 @@ public function inverse(mixed $context = Scope::Use, mixed $data = null): string
8586
return $this->invokeBlock($this->inv, $context, $data);
8687
}
8788

88-
private function invokeBlock(\Closure $closure, mixed $context, mixed $data): string
89+
private function invokeBlock(Closure $closure, mixed $context, mixed $data): string
8990
{
9091
$cx = $this->cx;
9192
// Save inlinePartials so that any {{#* inline}} partials registered inside the block body

src/Options.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Closure;
66

7-
readonly class Options
7+
final readonly class Options
88
{
99
/** @var array<string, bool> */
1010
public array $knownHelpers;
@@ -27,6 +27,6 @@ public function __construct(
2727
public ?Closure $partialResolver = null,
2828
) {
2929
$builtIn = ['if' => true, 'unless' => true, 'each' => true, 'with' => true, 'lookup' => true, 'log' => true];
30-
$this->knownHelpers = array_replace($builtIn, $knownHelpers);
30+
$this->knownHelpers = $knownHelpers ? array_replace($builtIn, $knownHelpers) : $builtIn;
3131
}
3232
}

src/SafeString.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44

55
/**
66
* Can be returned from a custom helper to prevent an HTML string from being escaped
7-
* when the template is rendered. When constructing, any external content should be
8-
* properly escaped using Handlebars::escapeExpression() to avoid potential security concerns.
7+
* when the template is rendered. Because SafeString bypasses the automatic HTML escaping
8+
* that {{ }} applies, any user-supplied content embedded in it must first be escaped with
9+
* Handlebars::escapeExpression() to prevent XSS vulnerabilities.
910
*/
10-
class SafeString implements \Stringable
11+
final readonly class SafeString implements \Stringable
1112
{
12-
private string $string;
13-
14-
public function __construct(string $string)
15-
{
16-
$this->string = $string;
17-
}
13+
public function __construct(
14+
private string $string,
15+
) {}
1816

1917
public function __toString(): string
2018
{

0 commit comments

Comments
 (0)