Skip to content

Commit 9472087

Browse files
committed
Fix level 6 type errors
1 parent 0ff968e commit 9472087

21 files changed

+304
-251
lines changed

phpstan.neon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
parameters:
2-
level: 5
2+
level: 6
33
paths:
44
- src
55
- tests

src/Compiler.php

Lines changed: 55 additions & 52 deletions
Large diffs are not rendered by default.

src/Context.php

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,21 @@
77
*/
88
final class Context
99
{
10+
/**
11+
* @param array<mixed> $stack
12+
* @param array<mixed>|null $currentToken
13+
* @param string[] $error
14+
* @param array<mixed> $elseLvl
15+
* @param array<string, string> $usedPartial
16+
* @param list<string> $partialStack
17+
* @param array<string, string> $partialCode
18+
* @param array<string, true> $usedHelpers
19+
* @param array<mixed> $parsed
20+
* @param array<string, string> $partials
21+
* @param array<mixed> $partialBlock
22+
* @param array<mixed> $inlinePartial
23+
* @param array<string, callable> $helpers
24+
*/
1025
public function __construct(
1126
public readonly Options $options,
1227
public int $level = 0,
@@ -15,12 +30,9 @@ public function __construct(
1530
public array $error = [],
1631
public array $elseLvl = [],
1732
public bool $elseChain = false,
18-
public array $tokens = [
19-
'ahead' => false,
20-
'current' => 0,
21-
'count' => 0,
22-
'partialind' => '',
23-
],
33+
public string $tokenSearch = '',
34+
public string $partialIndent = '',
35+
public int|false $tokenAhead = false,
2436
public array $usedPartial = [],
2537
public array $partialStack = [],
2638
public array $partialCode = [],
@@ -34,33 +46,17 @@ public function __construct(
3446
public array $inlinePartial = [],
3547
public array $helpers = [],
3648
public string|false $rawBlock = false,
37-
public readonly array $ops = [
38-
'separator' => '.',
39-
'f_start' => 'return ',
40-
'f_end' => ';',
41-
'op_start' => 'return ',
42-
'op_end' => ';',
43-
'cnd_start' => '.(',
44-
'cnd_then' => ' ? ',
45-
'cnd_else' => ' : ',
46-
'cnd_end' => ').',
47-
'cnd_nend' => ')',
48-
],
49+
public readonly string $startChar = '{',
50+
public readonly string $separator = '.',
51+
public readonly string $cndStart = '.(',
52+
public readonly string $cndEnd = ').',
53+
public readonly string $cndThen = ' ? ',
54+
public readonly string $cndElse = ' : ',
55+
public readonly string $fStart = 'return ',
56+
public readonly string $fEnd = ';',
4957
) {
5058
$this->partials = $options->partials;
51-
52-
foreach ($options->helpers as $name => $func) {
53-
$tn = is_int($name) ? $func : $name;
54-
if (is_callable($func)) {
55-
$this->helpers[$tn] = $func;
56-
} else {
57-
if (is_array($func)) {
58-
$this->error[] = "Custom helper $name must be a function, not an array.";
59-
} else {
60-
$this->error[] = "Custom helper '$tn' must be a function.";
61-
}
62-
}
63-
}
59+
$this->helpers = $options->helpers;
6460
}
6561

6662
/**

src/Encoder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ final class Encoder
1010
/**
1111
* Get the HTML encoded value of the specified variable.
1212
*
13-
* @param array<array|string|int>|string|int|bool|null $var value to be htmlencoded
13+
* @param array<array<mixed>|string|int>|string|int|bool|null $var value to be htmlencoded
1414
*/
1515
public static function enc(array|string|int|bool|null $var): string
1616
{
@@ -20,7 +20,7 @@ public static function enc(array|string|int|bool|null $var): string
2020
/**
2121
* Runtime method for {{var}}, and deal with single quote the same as Handlebars.js.
2222
*
23-
* @param array<array|string|int>|string|int|bool|null $var value to be htmlencoded
23+
* @param array<array<mixed>|string|int>|string|int|bool|null $var value to be htmlencoded
2424
*
2525
* @return string The htmlencoded value of the specified variable
2626
*/

src/Exporter.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public static function helpers(Context $context): string
3030
$ret .= (" '$name' => " . static::closure($func) . ",\n");
3131
continue;
3232
}
33-
$ret .= " '$name' => '$func',\n";
33+
if (is_string($func)) {
34+
$ret .= " '$name' => '$func',\n";
35+
} else {
36+
throw new \Exception('Unexpected helper type: ' . gettype($func));
37+
}
3438
}
3539

3640
return "[$ret]";

src/Expression.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public static function arrayString(array $list): string
3030
/**
3131
* Analyze an expression
3232
*
33-
* @param array<array|string|int> $var variable parsed path
33+
* @param array<array<mixed>|string|int> $var variable parsed path
3434
*
35-
* @return array{int, bool, array} analyzed result
35+
* @return array{int, bool, array<mixed>} analyzed result
3636
*/
3737
public static function analyze(array $var): array
3838
{

src/Handlebars.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
final class Handlebars
66
{
77
protected static Context $lastContext;
8+
/** @var array<mixed> */
89
public static array $lastParsed;
910

1011
/**
1112
* Compiles a template so it can be executed immediately.
12-
* @return \Closure(mixed=, array=):string
13+
* @return \Closure(mixed=, array<mixed>=):string
1314
*/
1415
public static function compile(string $template, Options $options = new Options()): \Closure
1516
{

src/HelperOptions.php

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

55
class HelperOptions
66
{
7+
/**
8+
* @param array<mixed> $hash
9+
* @param array<mixed> $data
10+
*/
711
public function __construct(
812
public readonly string $name,
913
public readonly array $hash,
@@ -14,12 +18,12 @@ public function __construct(
1418
public array &$data,
1519
) {}
1620

17-
public function fn(...$args): string
21+
public function fn(mixed ...$args): string
1822
{
1923
return ($this->fn)(...$args);
2024
}
2125

22-
public function inverse(...$args): string
26+
public function inverse(mixed ...$args): string
2327
{
2428
return ($this->inverse)(...$args);
2529
}

src/Options.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
readonly class Options
66
{
7+
/**
8+
* @param array<string, callable> $helpers
9+
* @param array<string, string> $partials
10+
*/
711
public function __construct(
812
public bool $knownHelpersOnly = false,
913
public bool $noEscape = false,

src/Parser.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ final class Parser
1616
/**
1717
* Get partial block id and fix the variable list
1818
*
19-
* @param array<bool|int|string|array> $vars parsed token
19+
* @param array<bool|int|string|array<mixed>> $vars parsed token
2020
*/
2121
public static function getPartialBlock(array &$vars): int
2222
{
@@ -31,7 +31,8 @@ public static function getPartialBlock(array &$vars): int
3131
/**
3232
* Get block params and fix the variable list
3333
*
34-
* @param array<bool|int|string|array> $vars parsed token
34+
* @param array<bool|int|string|array<mixed>> $vars parsed token
35+
* @return array<mixed>
3536
*/
3637
public static function getBlockParams(array &$vars): array
3738
{
@@ -144,7 +145,7 @@ protected static function getExpression(string $v, Context $context, int|string
144145
*
145146
* @param array<string> $token preg_match results
146147
*
147-
* @return array{bool,array} Return parsed result
148+
* @return array{bool,array<mixed>} Return parsed result
148149
*/
149150
public static function parse(array $token, Context $context): array
150151
{
@@ -190,7 +191,7 @@ public static function getPartialName(array $vars, int $pos = 0): ?array
190191
*
191192
* @param string $expression the full string of a sub expression
192193
*
193-
* @return array<string|int|array> Return parsed result
194+
* @return array{int, array<mixed>, string} Return parsed result
194195
*/
195196
public static function subexpression(string $expression, Context $context): array
196197
{
@@ -207,7 +208,7 @@ public static function subexpression(string $expression, Context $context): arra
207208
/**
208209
* Check a parsed result is a subexpression or not
209210
*
210-
* @param array<string|int|array> $var
211+
* @param array<string|int|array<mixed>> $var
211212
*/
212213
public static function isSubExp(array $var): bool
213214
{
@@ -220,7 +221,7 @@ public static function isSubExp(array $var): bool
220221
* @param list<string> $vars parsed token
221222
* @param string $token original token
222223
*
223-
* @return array<bool|int|array> Return parsed result
224+
* @return array<bool|int|array<mixed>> Return parsed result
224225
*/
225226
protected static function advancedVariable(array $vars, Context $context, string $token): array
226227
{

src/Partial.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ public static function compile(Context $context, string $template, string $name)
9292
$context->merge($tmpContext);
9393

9494
if (!$context->options->preventIndent) {
95-
$code = preg_replace('/^/m', "'{$context->ops['separator']}\$sp{$context->ops['separator']}'", $code);
95+
$code = preg_replace('/^/m', "'{$context->separator}\$sp{$context->separator}'", $code);
9696
// remove extra spaces before partial
9797
$code = preg_replace('/^\'\\.\\$sp\\.\'(\'\\.LR::p\\()/m', '$1', $code, 1);
9898
// add spaces after partial
9999
$code = preg_replace('/^(\'\\.LR::p\\(.+\\)\\.)(\'.+)/m', '$1\$sp.$2', $code, 1);
100100
}
101-
return "function (\$cx, \$in, \$sp) {{$context->ops['op_start']}'$code'{$context->ops['op_end']}}";
101+
return "function (\$cx, \$in, \$sp) {{$context->fStart}'$code'{$context->fEnd}}";
102102
}
103103
}

0 commit comments

Comments
 (0)