Skip to content

Commit 54cbaf1

Browse files
committed
Require RuntimeContext in HelperOptions
1 parent fb8fb13 commit 54cbaf1

3 files changed

Lines changed: 26 additions & 10 deletions

File tree

src/HelperOptions.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class HelperOptions
2121
public function __construct(
2222
public mixed &$scope,
2323
public array &$data,
24+
private readonly RuntimeContext $cx,
2425
public readonly string $name = '',
2526
public readonly array $hash = [],
2627
public readonly int $blockParams = 0,
27-
private readonly ?RuntimeContext $cx = null,
2828
private readonly ?Closure $cb = null,
2929
private readonly ?Closure $inv = null,
3030
private readonly array $outerBlockParams = [],
@@ -45,9 +45,10 @@ public function __isset(string $name): bool
4545

4646
public function fn(mixed $context = Scope::Use, mixed $data = null): string
4747
{
48-
if ($this->cx === null) {
49-
throw new \Exception('fn() is not supported for inline helpers');
50-
} elseif ($this->cb === null) {
48+
if ($this->cb === null) {
49+
if ($this->inv === null) {
50+
throw new \Exception('fn() is not supported for inline helpers');
51+
}
5152
return '';
5253
}
5354
$cx = $this->cx;
@@ -69,9 +70,10 @@ public function fn(mixed $context = Scope::Use, mixed $data = null): string
6970

7071
public function inverse(mixed $context = null, mixed $data = null): string
7172
{
72-
if ($this->cx === null) {
73-
throw new \Exception('inverse() is not supported for inline helpers');
74-
} elseif ($this->inv === null) {
73+
if ($this->inv === null) {
74+
if ($this->cb === null) {
75+
throw new \Exception('inverse() is not supported for inline helpers');
76+
}
7577
return '';
7678
}
7779
return $this->callBlock($this->inv, $context ?? $this->scope, $context !== null, $data);
@@ -81,7 +83,6 @@ public function inverse(mixed $context = null, mixed $data = null): string
8183
private function callBlock(\Closure $closure, mixed $context, bool $pushDepths, ?array $data): string
8284
{
8385
$cx = $this->cx;
84-
assert($cx !== null);
8586
$savedFrame = null;
8687
$bpStack = null;
8788

@@ -133,7 +134,6 @@ public function iterate(array $items): string
133134
return '';
134135
}
135136
$cx = $this->cx;
136-
assert($cx !== null);
137137
$cb = $this->cb;
138138

139139
// Push depths and save partials once for the entire loop.

src/Runtime.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ public static function hbch(RuntimeContext $cx, \Closure $helper, string $name,
479479
$positional[] = new HelperOptions(
480480
scope: $_this,
481481
data: $cx->frame,
482+
cx: $cx,
482483
name: $name,
483484
hash: $hash,
484485
);
@@ -502,10 +503,10 @@ public static function hbbch(RuntimeContext $cx, \Closure $helper, string $name,
502503
$positional[] = new HelperOptions(
503504
scope: $_this,
504505
data: $cx->frame,
506+
cx: $cx,
505507
name: $name,
506508
hash: $hash,
507509
blockParams: $blockParamCount,
508-
cx: $cx,
509510
cb: $cb,
510511
inv: $else,
511512
outerBlockParams: $outerBlockParams,

tests/ErrorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace DevTheorem\Handlebars\Test;
44

55
use DevTheorem\Handlebars\Handlebars;
6+
use DevTheorem\Handlebars\HelperOptions;
67
use DevTheorem\Handlebars\Options;
78
use PHPUnit\Framework\Attributes\DataProvider;
89
use PHPUnit\Framework\TestCase;
@@ -103,6 +104,20 @@ public static function renderErrorProvider(): array
103104
'options' => new Options(strict: true),
104105
'expected' => '"foo" not defined',
105106
],
107+
[
108+
'template' => '{{foo}}',
109+
'helpers' => [
110+
'foo' => fn(HelperOptions $options) => $options->fn(),
111+
],
112+
'expected' => 'fn() is not supported for inline helpers',
113+
],
114+
[
115+
'template' => '{{foo}}',
116+
'helpers' => [
117+
'foo' => fn(HelperOptions $options) => $options->inverse(),
118+
],
119+
'expected' => 'inverse() is not supported for inline helpers',
120+
],
106121
[
107122
'template' => '{{foo}}',
108123
'helpers' => [

0 commit comments

Comments
 (0)