Skip to content

Commit 2e21b88

Browse files
committed
Fix level 6 type errors (unfinished progress)
1 parent 0ff968e commit 2e21b88

6 files changed

+90
-56
lines changed

src/Runtime.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ public static function wi(RuntimeContext $cx, mixed $v, ?array $bp, array|\stdCl
256256
* @param array<array|string|int>|string|int|null $b the new context to overwrite
257257
*
258258
* @return array<array|string|int>|string|int the merged context object
259-
*
260259
*/
261260
public static function merge(mixed $a, mixed $b)
262261
{

tests/ErrorTest.php

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
use DevTheorem\Handlebars\Handlebars;
66
use DevTheorem\Handlebars\Options;
7+
use PHPUnit\Framework\Attributes\DataProvider;
78
use PHPUnit\Framework\TestCase;
89

10+
/**
11+
* @phpstan-type RenderTest array{template: string, options?: Options, expected: string}
12+
* @phpstan-type ErrorCase array{template: string, options: Options, expected?: list<string>}
13+
*/
914
class ErrorTest extends TestCase
1015
{
11-
public function testException()
16+
public function testException(): void
1217
{
1318
try {
1419
$php = Handlebars::precompile('{{{foo}}');
@@ -17,7 +22,7 @@ public function testException()
1722
}
1823
}
1924

20-
public function testLog()
25+
public function testLog(): void
2126
{
2227
$template = Handlebars::compile('{{log foo}}');
2328

@@ -38,19 +43,25 @@ public function testLog()
3843
ini_restore('error_log');
3944
}
4045

41-
#[\PHPUnit\Framework\Attributes\DataProvider("renderErrorProvider")]
42-
public function testRenderingException($test)
46+
/**
47+
* @param RenderTest $test
48+
*/
49+
#[DataProvider("renderErrorProvider")]
50+
public function testRenderingException(array $test): void
4351
{
4452
$php = Handlebars::precompile($test['template'], $test['options'] ?? new Options());
4553
$renderer = Handlebars::template($php);
4654
try {
47-
$renderer($test['data'] ?? null);
55+
$renderer(null);
4856
$this->fail("Expected to throw exception: {$test['expected']}. CODE: $php");
4957
} catch (\Exception $E) {
5058
$this->assertEquals($test['expected'], $E->getMessage());
5159
}
5260
}
5361

62+
/**
63+
* @return list<array{RenderTest}>
64+
*/
5465
public static function renderErrorProvider(): array
5566
{
5667
$errorCases = [
@@ -99,8 +110,11 @@ public static function renderErrorProvider(): array
99110
return array_map(fn($i) => [$i], $errorCases);
100111
}
101112

102-
#[\PHPUnit\Framework\Attributes\DataProvider("errorProvider")]
103-
public function testErrors($test)
113+
/**
114+
* @param ErrorCase $test
115+
*/
116+
#[DataProvider("errorProvider")]
117+
public function testErrors(array $test): void
104118
{
105119
if (!isset($test['expected'])) {
106120
// should compile without error
@@ -111,12 +125,15 @@ public function testErrors($test)
111125

112126
try {
113127
Handlebars::precompile($test['template'], $test['options']);
114-
$this->fail("Expected to throw exception: {$test['expected']}");
128+
$this->fail("Expected to throw exception: {$test['expected'][0]}");
115129
} catch (\Exception $e) {
116130
$this->assertEquals($test['expected'], explode("\n", $e->getMessage()));
117131
}
118132
}
119133

134+
/**
135+
* @return list<array{ErrorCase}>
136+
*/
120137
public static function errorProvider(): array
121138
{
122139
$errorCases = [

tests/HandlebarsSpecTest.php

Lines changed: 42 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,12 @@
44
use DevTheorem\Handlebars\Options;
55
use PHPUnit\Framework\TestCase;
66

7-
$tested = 0;
8-
9-
function recursive_unset(&$array, $unwanted_key): void
10-
{
11-
if (!is_array($array)) {
12-
return;
13-
}
14-
if (isset($array[$unwanted_key])) {
15-
unset($array[$unwanted_key]);
16-
}
17-
foreach ($array as &$value) {
18-
if (is_array($value)) {
19-
recursive_unset($value, $unwanted_key);
20-
}
21-
}
22-
}
23-
24-
function patch_safestring($code)
25-
{
26-
$classname = '\\DevTheorem\\Handlebars\\SafeString';
27-
return preg_replace('/ SafeString(\s*\(.*?\))?/', ' ' . $classname . '$1', $code);
28-
}
29-
30-
function data_helpers_fix(array &$spec)
31-
{
32-
if (isset($spec['data']) && is_array($spec['data'])) {
33-
foreach ($spec['data'] as $key => $value) {
34-
if (is_array($value) && isset($value['!code']) && isset($value['php'])) {
35-
$spec['helpers'][$key] = $value;
36-
unset($spec['data'][$key]);
37-
}
38-
}
39-
}
40-
}
41-
427
/**
438
* Used by vendor/jbboehr/handlebars-spec/spec/data.json
449
*/
4510
class Utils
4611
{
47-
public static function createFrame($data)
12+
public static function createFrame(mixed $data): mixed
4813
{
4914
if (is_array($data)) {
5015
$r = [];
@@ -59,13 +24,13 @@ public static function createFrame($data)
5924

6025
class HandlebarsSpecTest extends TestCase
6126
{
27+
private int $tested = 0;
28+
6229
#[\PHPUnit\Framework\Attributes\DataProvider("jsonSpecProvider")]
6330
public function testSpecs($spec)
6431
{
65-
global $tested;
66-
67-
recursive_unset($spec['data'], '!sparsearray');
68-
data_helpers_fix($spec);
32+
self::unsetRecursive($spec['data'], '!sparsearray');
33+
self::fixDataHelpers($spec);
6934

7035
// Fix {} for these test cases
7136
if (
@@ -176,16 +141,16 @@ public function testSpecs($spec)
176141
}
177142

178143
// setup helpers
179-
$tested++;
144+
$this->tested++;
180145
$helpers = [];
181146
$helpersList = '';
182147
foreach (is_array($spec['helpers'] ?? null) ? $spec['helpers'] : [] as $name => $func) {
183148
if (!isset($func['php'])) {
184149
$this->markTestIncomplete("Skip [{$spec['file']}#{$spec['description']}]#{$spec['no']} , no PHP helper code provided for this case.");
185150
}
186-
$hname = preg_replace('/[.\\/]/', '_', "custom_helper_{$spec['no']}_{$tested}_$name");
151+
$hname = preg_replace('/[.\\/]/', '_', "custom_helper_{$spec['no']}_{$this->tested}_$name");
187152
$helpers[$name] = $hname;
188-
$helper = patch_safestring(
153+
$helper = self::patchSafeString(
189154
preg_replace('/function/', "function $hname", $func['php'], 1),
190155
);
191156
$helper = str_replace('new \Handlebars\SafeString', 'new \DevTheorem\Handlebars\SafeString', $helper);
@@ -283,7 +248,7 @@ public function testSpecs($spec)
283248
$this->assertEquals($spec['expected'], $result, "[{$spec['file']}#{$spec['description']}]#{$spec['no']}:{$spec['it']}\nHELPERS:$helpersList");
284249
}
285250

286-
public static function jsonSpecProvider()
251+
public static function jsonSpecProvider(): array
287252
{
288253
$ret = [];
289254

@@ -313,4 +278,37 @@ public static function jsonSpecProvider()
313278

314279
return $ret;
315280
}
281+
282+
private static function fixDataHelpers(array &$spec): void
283+
{
284+
if (isset($spec['data']) && is_array($spec['data'])) {
285+
foreach ($spec['data'] as $key => $value) {
286+
if (is_array($value) && isset($value['!code']) && isset($value['php'])) {
287+
$spec['helpers'][$key] = $value;
288+
unset($spec['data'][$key]);
289+
}
290+
}
291+
}
292+
}
293+
294+
private static function unsetRecursive(mixed &$array, string $unwanted_key): void
295+
{
296+
if (!is_array($array)) {
297+
return;
298+
}
299+
if (isset($array[$unwanted_key])) {
300+
unset($array[$unwanted_key]);
301+
}
302+
foreach ($array as &$value) {
303+
if (is_array($value)) {
304+
self::unsetRecursive($value, $unwanted_key);
305+
}
306+
}
307+
}
308+
309+
private static function patchSafeString(string $code): string
310+
{
311+
$classname = '\\DevTheorem\\Handlebars\\SafeString';
312+
return preg_replace('/ SafeString(\s*\(.*?\))?/', ' ' . $classname . '$1', $code);
313+
}
316314
}

tests/ParserTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
class ParserTest extends TestCase
1010
{
11+
/**
12+
* @return list<array{array{string}|null, array<string|int>, int}>
13+
*/
1114
public static function getPartialNameProvider(): array
1215
{
1316
return [
@@ -20,6 +23,10 @@ public static function getPartialNameProvider(): array
2023
];
2124
}
2225

26+
/**
27+
* @param array{string}|null $expected
28+
* @param array<string|int> $vars
29+
*/
2330
#[DataProvider('getPartialNameProvider')]
2431
public function testGetPartialName(?array $expected, array $vars, int $pos): void
2532
{

tests/RegressionTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
use DevTheorem\Handlebars\HelperOptions;
77
use DevTheorem\Handlebars\Options;
88
use DevTheorem\Handlebars\SafeString;
9+
use PHPUnit\Framework\Attributes\DataProvider;
910
use PHPUnit\Framework\TestCase;
1011

12+
/**
13+
* @phpstan-type RegIssue array{id?: int, template: string, data?: array<mixed>, options?: Options, expected: string}
14+
*/
1115
class RegressionTest extends TestCase
1216
{
13-
#[\PHPUnit\Framework\Attributes\DataProvider("issueProvider")]
14-
public function testIssues($issue)
17+
/**
18+
* @param RegIssue $issue
19+
*/
20+
#[DataProvider("issueProvider")]
21+
public function testIssues(array $issue): void
1522
{
1623
$templateSpec = Handlebars::precompile($issue['template'], $issue['options'] ?? new Options());
1724
$context = Handlebars::getContext();
@@ -29,6 +36,9 @@ public function testIssues($issue)
2936
$this->assertEquals($issue['expected'], $result, "PHP CODE:\n$templateSpec");
3037
}
3138

39+
/**
40+
* @return list<array{RegIssue}>
41+
*/
3242
public static function issueProvider(): array
3343
{
3444
$test_helpers = ['ouch' => fn() => 'ok'];

tests/TwoDimensionIterator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace DevTheorem\Handlebars\Test;
44

5+
/**
6+
* @implements \Iterator<string, int>
7+
*/
58
class TwoDimensionIterator implements \Iterator
69
{
710
private int $position = 0;
@@ -20,7 +23,7 @@ public function rewind(): void
2023
$this->y = 0;
2124
}
2225

23-
public function current(): int|float
26+
public function current(): int
2427
{
2528
return $this->x * $this->y;
2629
}

0 commit comments

Comments
 (0)