Skip to content

Commit 7225a97

Browse files
committed
Better align runtime errors with Handlebars.js
Also rename spVars to data for consistency, and simplify error tests.
1 parent 0d248f1 commit 7225a97

5 files changed

Lines changed: 85 additions & 71 deletions

File tree

‎src/Compiler.php‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,7 @@ public function composePHPRender(string $code): string
8989
\$cx = new RuntimeContext(
9090
helpers: isset(\$options['helpers']) ? array_merge(\$helpers, \$options['helpers']) : \$helpers,
9191
partials: isset(\$options['partials']) ? array_merge(\$partials, \$options['partials']) : \$partials,
92-
scopes: [],
93-
spVars: isset(\$options['data']) ? array_merge(['root' => \$in], \$options['data']) : ['root' => \$in],
94-
blParam: [],
95-
partialId: 0,
92+
data: isset(\$options['data']) ? array_merge(['root' => \$in], \$options['data']) : ['root' => \$in],
9693
);
9794
return '$code';
9895
};
@@ -918,7 +915,7 @@ private function compileProgramWithBlockParams(Program $program, array $bp, bool
918915
*/
919916
private function buildBasePath(bool $data, int $depth): string
920917
{
921-
$base = $data ? '$cx->spVars' : '$in';
918+
$base = $data ? '$cx->data' : '$in';
922919
if ($depth > 0) {
923920
$base = $data
924921
? $base . str_repeat("['_parent']", $depth)

‎src/Runtime.php‎

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class Runtime
1414
*/
1515
public static function miss(string $v): void
1616
{
17-
throw new \Exception("Runtime: $v does not exist");
17+
throw new \Exception('"' . $v . '" not defined');
1818
}
1919

2020
/**
@@ -159,14 +159,14 @@ public static function sec(RuntimeContext $cx, mixed $v, array $bp, mixed $in, b
159159
$cx->scopes[] = $in;
160160
}
161161
$i = 0;
162-
$oldSpvar = $cx->spVars ?? [];
163-
$cx->spVars = array_merge(['root' => $oldSpvar['root'] ?? null], $oldSpvar, ['_parent' => $oldSpvar]);
162+
$oldData = $cx->data ?? [];
163+
$cx->data = array_merge(['root' => $oldData['root'] ?? null], $oldData, ['_parent' => $oldData]);
164164

165165
foreach ($v as $index => $raw) {
166-
$cx->spVars['first'] = ($i === 0);
167-
$cx->spVars['last'] = ($i === $last);
168-
$cx->spVars['key'] = $index;
169-
$cx->spVars['index'] = $isSparseArray ? $index : $i;
166+
$cx->data['first'] = ($i === 0);
167+
$cx->data['last'] = ($i === $last);
168+
$cx->data['key'] = $index;
169+
$cx->data['index'] = $isSparseArray ? $index : $i;
170170
$i++;
171171
if ($bp) {
172172
$bpEntry = [];
@@ -187,11 +187,11 @@ public static function sec(RuntimeContext $cx, mixed $v, array $bp, mixed $in, b
187187
}
188188

189189
if ($isObj) {
190-
unset($cx->spVars['key']);
190+
unset($cx->data['key']);
191191
} else {
192-
unset($cx->spVars['last']);
192+
unset($cx->data['last']);
193193
}
194-
unset($cx->spVars['index'], $cx->spVars['first']);
194+
unset($cx->data['index'], $cx->data['first']);
195195

196196
if ($push) {
197197
array_pop($cx->scopes);
@@ -235,7 +235,7 @@ public static function sec(RuntimeContext $cx, mixed $v, array $bp, mixed $in, b
235235
},
236236
blockParams: 0,
237237
scope: $in,
238-
data: $cx->spVars,
238+
data: $cx->data,
239239
);
240240
$result = $v($options);
241241
return static::applyBlockHelperMissing($cx, $result, $in, $cb, $else);
@@ -391,7 +391,7 @@ public static function hbch(RuntimeContext $cx, string $ch, array $vars, mixed &
391391
inverse: fn() => '',
392392
blockParams: 0,
393393
scope: $_this,
394-
data: $cx->spVars,
394+
data: $cx->data,
395395
);
396396

397397
return static::exch($cx, $ch, $vars, $options);
@@ -411,7 +411,7 @@ public static function hbch(RuntimeContext $cx, string $ch, array $vars, mixed &
411411
public static function hbbch(RuntimeContext $cx, string $ch, array $vars, mixed &$_this, bool $inverted, \Closure $cb, ?\Closure $else = null, ?string $logicalName = null): mixed
412412
{
413413
$blockParams = isset($vars[2]) ? count($vars[2]) : 0;
414-
$data = &$cx->spVars;
414+
$data = &$cx->data;
415415

416416
// invert the logic
417417
if ($inverted) {
@@ -447,7 +447,7 @@ public static function dynhbbch(RuntimeContext $cx, string $name, mixed $callabl
447447
}
448448

449449
$blockParams = isset($vars[2]) ? count($vars[2]) : 0;
450-
$data = &$cx->spVars;
450+
$data = &$cx->data;
451451

452452
$options = new HelperOptions(
453453
name: '',
@@ -472,7 +472,7 @@ public static function dynhbbch(RuntimeContext $cx, string $name, mixed $callabl
472472

473473
/**
474474
* Build the $fn closure passed to HelperOptions for block helpers.
475-
* Handles spVars updates, block-param injection, and context scope pushing.
475+
* Handles private variable updates, block-param injection, and context scope pushing.
476476
*
477477
* @param array<array<mixed>> $vars
478478
*/
@@ -484,9 +484,9 @@ private static function makeBlockFn(RuntimeContext $cx, mixed $_this, ?\Closure
484484

485485
return function ($context = null, $data = null) use ($cx, $_this, $cb, $vars) {
486486
$cx = clone $cx;
487-
$old_spvar = $cx->spVars;
487+
$oldData = $cx->data;
488488
if (isset($data['data'])) {
489-
$cx->spVars = array_merge(['root' => $old_spvar['root']], $data['data'], ['_parent' => $old_spvar]);
489+
$cx->data = array_merge(['root' => $oldData['root']], $data['data'], ['_parent' => $oldData]);
490490
}
491491

492492
if (isset($data['blockParams'], $vars[2])) {
@@ -501,7 +501,7 @@ private static function makeBlockFn(RuntimeContext $cx, mixed $_this, ?\Closure
501501
}
502502

503503
if (isset($data['data'])) {
504-
$cx->spVars = $old_spvar;
504+
$cx->data = $oldData;
505505
}
506506
return $ret;
507507
};
@@ -558,7 +558,7 @@ public static function exch(RuntimeContext $cx, string $ch, array $vars, HelperO
558558
try {
559559
return ($cx->helpers[$ch])(...$args);
560560
} catch (\Throwable $e) {
561-
throw new \Exception("Runtime: call custom helper '$ch' error: " . $e->getMessage());
561+
throw new \Exception("Custom helper '$ch' error: " . $e->getMessage());
562562
}
563563
}
564564
}

‎src/RuntimeContext.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ class RuntimeContext
1111
* @param array<string, callable> $helpers
1212
* @param array<string, callable> $partials
1313
* @param array<mixed> $scopes
14-
* @param array<mixed> $spVars
14+
* @param array<mixed> $data
1515
* @param array<mixed> $blParam
1616
*/
1717
public function __construct(
1818
public array $helpers = [],
1919
public array $partials = [],
2020
public array $scopes = [],
21-
public array $spVars = [],
21+
public array $data = [],
2222
public array $blParam = [],
2323
public int $partialId = 0,
2424
public int $partialDepth = 0,

‎tests/ErrorTest.php‎

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@
88
use PHPUnit\Framework\TestCase;
99

1010
/**
11-
* @phpstan-type RenderTest array{template: string, options?: Options, expected: string}
12-
* @phpstan-type ErrorCase array{template: string, options: Options, expected: string|null}
11+
* @phpstan-type RenderTest array{template: string, options?: Options, data?: array<mixed>, expected: string}
12+
* @phpstan-type ErrorCase array{template: string, options?: Options, expected: string}
1313
*/
1414
class ErrorTest extends TestCase
1515
{
1616
/**
17-
* @param RenderTest $test
17+
* @param array<mixed> $data
1818
*/
1919
#[DataProvider("renderErrorProvider")]
20-
public function testRenderingException(array $test): void
20+
public function testRenderingException(string $template, string $expected, ?Options $options = null, array $data = []): void
2121
{
22-
$php = Handlebars::precompile($test['template'], $test['options'] ?? new Options());
22+
$php = Handlebars::precompile($template, $options ?? new Options());
2323
$renderer = Handlebars::template($php);
2424
try {
25-
$renderer();
26-
$this->fail("Expected to throw exception: {$test['expected']}. CODE: $php");
25+
$renderer($data);
26+
$this->fail("Expected to throw exception: {$expected}. CODE: $php");
2727
} catch (\Exception $e) {
28-
$this->assertEquals($test['expected'], $e->getMessage(), $php);
28+
$this->assertEquals($expected, $e->getMessage(), $php);
2929
}
3030
}
3131

3232
/**
33-
* @return list<array{RenderTest}>
33+
* @return RenderTest[]
3434
*/
3535
public static function renderErrorProvider(): array
3636
{
37-
$errorCases = [
37+
return [
3838
[
3939
'template' => "{{#> testPartial}}\n {{#> innerPartial}}\n {{> @partial-block}}\n {{/innerPartial}}\n{{/testPartial}}",
4040
'options' => new Options(
@@ -49,20 +49,44 @@ public static function renderErrorProvider(): array
4949
'template' => '{{> @partial-block}}',
5050
'expected' => "Runtime: the partial @partial-block could not be found",
5151
],
52+
[
53+
'template' => '{{foo.bar}}',
54+
'options' => new Options(strict: true),
55+
'data' => ['foo' => []],
56+
'expected' => '"foo.bar" not defined',
57+
],
5258
[
5359
'template' => '{{foo}}',
5460
'options' => new Options(strict: true),
55-
'expected' => 'Runtime: foo does not exist',
61+
'expected' => '"foo" not defined',
62+
],
63+
[
64+
// strict mode should override helperMissing
65+
'template' => '{{foo}}',
66+
'options' => new Options(
67+
strict: true,
68+
helpers: ['helperMissing' => fn() => 'bad'],
69+
),
70+
'expected' => '"foo" not defined',
71+
],
72+
[
73+
// strict mode should override blockHelperMissing
74+
'template' => '{{#foo}}OK{{/foo}}',
75+
'options' => new Options(
76+
strict: true,
77+
helpers: ['blockHelperMissing' => fn() => 'bad'],
78+
),
79+
'expected' => '"foo" not defined',
5680
],
5781
[
5882
'template' => '{{#foo}}OK{{/foo}}',
5983
'options' => new Options(strict: true),
60-
'expected' => 'Runtime: foo does not exist',
84+
'expected' => '"foo" not defined',
6185
],
6286
[
6387
'template' => '{{{foo}}}',
6488
'options' => new Options(strict: true),
65-
'expected' => 'Runtime: foo does not exist',
89+
'expected' => '"foo" not defined',
6690
],
6791
[
6892
'template' => '{{foo}}',
@@ -73,7 +97,7 @@ public static function renderErrorProvider(): array
7397
},
7498
],
7599
),
76-
'expected' => 'Runtime: call custom helper \'foo\' error: Expect the unexpected',
100+
'expected' => 'Custom helper \'foo\' error: Expect the unexpected',
77101
],
78102
// ensure that callable strings in data aren't treated as functions
79103
[
@@ -82,22 +106,13 @@ public static function renderErrorProvider(): array
82106
'expected' => '"foo.bar" is not a block helper function',
83107
],
84108
];
85-
86-
return array_map(fn($i) => [$i], $errorCases);
87109
}
88110

89111
#[DataProvider("errorProvider")]
90-
public function testErrors(string $template, ?string $expected, Options $options): void
112+
public function testErrors(string $template, string $expected, ?Options $options = null): void
91113
{
92-
if ($expected === null) {
93-
// should compile without error
94-
$code = Handlebars::precompile($template, $options);
95-
$this->assertNotEmpty($code);
96-
return;
97-
}
98-
99114
try {
100-
Handlebars::precompile($template, $options);
115+
Handlebars::precompile($template, $options ?? new Options());
101116
$this->fail("Expected to throw exception: {$expected}");
102117
} catch (\Exception $e) {
103118
$this->assertSame($expected, $e->getMessage());
@@ -109,14 +124,11 @@ public function testErrors(string $template, ?string $expected, Options $options
109124
*/
110125
public static function errorProvider(): array
111126
{
112-
$errorCases = [
127+
return [
113128
[
114129
'template' => '{{typeof hello}}',
115130
'expected' => 'Missing helper: "typeof"',
116131
],
117-
[
118-
'template' => '{{#with items}}OK!{{/with}}',
119-
],
120132
[
121133
'template' => '{{#with}}OK!{{/with}}',
122134
'expected' => '#with requires exactly one argument',
@@ -164,26 +176,10 @@ public static function errorProvider(): array
164176
'template' => '{{> (foo) bar}}',
165177
'expected' => 'Missing helper: "foo"',
166178
],
167-
[
168-
'template' => '{{log}}',
169-
],
170179
[
171180
'template' => '{{#*help me}}{{/help}}',
172181
'expected' => 'Unknown decorator: "help"',
173182
],
174-
[
175-
'template' => '{{#*inline}}{{/inline}}',
176-
],
177183
];
178-
179-
return array_map(function ($i) {
180-
if (!isset($i['options'])) {
181-
$i['options'] = new Options();
182-
}
183-
if (!isset($i['expected'])) {
184-
$i['expected'] = null;
185-
}
186-
return $i;
187-
}, $errorCases);
188184
}
189185
}

‎tests/RegressionTest.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,27 @@ public static function issueProvider(): array
19121912
'template' => '{{foo}}',
19131913
'expected' => '',
19141914
],
1915+
[
1916+
'template' => '{{foo.bar}}',
1917+
'expected' => '',
1918+
],
1919+
[
1920+
'template' => '{{foo.bar}}',
1921+
'data' => ['foo' => []],
1922+
'expected' => '',
1923+
],
1924+
[
1925+
'template' => '{{#with items}}OK!{{/with}}',
1926+
'expected' => '',
1927+
],
1928+
[
1929+
'template' => '{{log}}',
1930+
'expected' => '',
1931+
],
1932+
[
1933+
'template' => '{{#*inline}}{{/inline}}',
1934+
'expected' => '',
1935+
],
19151936

19161937
[
19171938
'template' => '{{#myif foo}}YES{{else}}NO{{/myif}}',

0 commit comments

Comments
 (0)