@@ -33,7 +33,7 @@ final class Compiler
3333 /**
3434 * Compile-time stack of block param name arrays, innermost first.
3535 * Only populated for constructs that push to $cx->blParam at runtime (currently #each).
36- * @var list<list< string> >
36+ * @var list<string[] >
3737 */
3838 private array $ blockParamValues = [];
3939
@@ -51,12 +51,15 @@ public function compile(Program $program, Context $context): string
5151 {
5252 $ this ->context = $ context ;
5353 $ this ->blockParamValues = [];
54- $ code = '' ;
54+ return $ this ->compileBody ($ program );
55+ }
5556
57+ private function compileBody (Program $ program ): string
58+ {
59+ $ code = '' ;
5660 foreach ($ program ->body as $ statement ) {
5761 $ code .= $ this ->accept ($ statement );
5862 }
59-
6063 return $ code ;
6164 }
6265
@@ -98,14 +101,7 @@ public function composePHPRender(string $code): string
98101
99102 private function compileProgram (Program $ program , bool $ withSp = false ): string
100103 {
101- $ code = '' ;
102-
103- foreach ($ program ->body as $ statement ) {
104- $ code .= $ this ->accept ($ statement );
105- }
106-
107- $ quoted = "' " . $ code . "' " ;
108-
104+ $ quoted = "' " . $ this ->compileBody ($ program ) . "' " ;
109105 return $ withSp ? "\$sp. $ quoted " : $ quoted ;
110106 }
111107
@@ -192,7 +188,7 @@ private function BlockStatement(BlockStatement $block): string
192188 // Regular section: {{#"foo"}}...{{/"foo"}}
193189 $ body = $ this ->compileProgram ($ block ->program , true );
194190 $ else = $ this ->compileElseClause ($ block );
195- return "'. " . self ::getRuntimeFunc ('sec ' , "\$cx, $ var, null , \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ".' " ;
191+ return "'. " . self ::getRuntimeFunc ('sec ' , "\$cx, $ var, [] , \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ".' " ;
196192 }
197193
198194 // Inverted section: {{^var}}...{{/var}}
@@ -216,6 +212,9 @@ private function BlockStatement(BlockStatement $block): string
216212
217213 private function compileDynamicBlockHelper (BlockStatement $ block ): string
218214 {
215+ if (!$ block ->program ) {
216+ throw new \Exception ('Dynamic block program must not be empty ' );
217+ }
219218 $ varPath = $ this ->compileExpression ($ block ->path );
220219 $ bp = $ block ->program ->blockParams ;
221220 $ params = $ this ->compileParams ($ block ->params , $ block ->hash , $ bp ?: null );
@@ -310,7 +309,7 @@ private function compileSection(BlockStatement $block): string
310309 return "'. " . self ::getRuntimeFunc ('hbbch ' , "\$cx, 'blockHelperMissing', [[ $ var],[]], \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else, $ escapedName " ) . ".' " ;
311310 }
312311
313- return "'. " . self ::getRuntimeFunc ('sec ' , "\$cx, $ var, null , \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ".' " ;
312+ return "'. " . self ::getRuntimeFunc ('sec ' , "\$cx, $ var, [] , \$in, false, function( \$cx, \$in) use (& \$sp) {return $ body;} $ else " ) . ".' " ;
314313 }
315314
316315 private function compileInvertedSection (BlockStatement $ block ): string
@@ -323,8 +322,8 @@ private function compileInvertedSection(BlockStatement $block): string
323322
324323 private function compileBlockHelper (BlockStatement $ block , string $ helperName , ?string $ missingName = null ): string
325324 {
326- $ bp = $ block ->program ->blockParams ?? $ block ->inverse ->blockParams ?? null ;
327- $ params = $ this ->compileParams ($ block ->params , $ block ->hash , $ bp ?: null );
325+ $ bp = $ block ->program ->blockParams ?? $ block ->inverse ->blockParams ?? [] ;
326+ $ params = $ this ->compileParams ($ block ->params , $ block ->hash , $ bp );
328327 $ escapedName = $ missingName === null ? 'null ' : self ::quote ($ missingName );
329328
330329 if ($ block ->program === null ) {
@@ -489,7 +488,7 @@ private function MustacheStatement(MustacheStatement $mustache): string
489488 return $ this ->compileLog ($ mustache );
490489 }
491490
492- if (count ( $ mustache ->params ) !== 0 ) {
491+ if ($ mustache ->params ) {
493492 // Non-simple path with params (data var or pathed expression): invoke via dv()
494493 if ($ helperName === null ) {
495494 $ varPath = $ this ->PathExpression ($ path );
@@ -535,7 +534,7 @@ private function MustacheStatement(MustacheStatement $mustache): string
535534 return "'. " . self ::getRuntimeFunc ($ fn , $ call ) . ".' " ;
536535 }
537536
538- if (count ( $ mustache ->params ) !== 0 ) {
537+ if ($ mustache ->params ) {
539538 throw new \Exception ('Missing helper: " ' . $ literalKey . '" ' );
540539 }
541540
@@ -577,7 +576,7 @@ private function SubExpression(SubExpression $expression): string
577576
578577 // Built-in: lookup (as subexpression)
579578 if ($ helperName === 'lookup ' ) {
580- return $ this ->compileLookupExpr ($ expression ->params );
579+ return $ this ->getWithLookup ($ expression ->params [ 0 ], $ expression -> params [ 1 ] );
581580 }
582581
583582 if ($ helperName !== null ) {
@@ -878,19 +877,13 @@ private static function scopedId(PathExpression $path): bool
878877 */
879878 private function getSimpleHelperName (PathExpression |Literal $ path ): ?string
880879 {
881- if (!$ path instanceof PathExpression) {
882- return null ;
883- }
884-
885- if ($ path ->data || $ path ->depth > 0 || self ::scopedId ($ path )) {
886- return null ;
887- }
888-
889- if (count ($ path ->parts ) !== 1 ) {
890- return null ;
891- }
892-
893- if (!is_string ($ path ->parts [0 ])) {
880+ if (!$ path instanceof PathExpression
881+ || $ path ->data
882+ || $ path ->depth > 0
883+ || self ::scopedId ($ path )
884+ || count ($ path ->parts ) !== 1
885+ || !is_string ($ path ->parts [0 ])
886+ ) {
894887 return null ;
895888 }
896889
@@ -971,18 +964,11 @@ private static function quote(string $string): string
971964
972965 /**
973966 * Get string presentation for a string list
974- * @param array< string> $list
967+ * @param string[] $list
975968 */
976969 private static function listString (array $ list ): string
977970 {
978- $ string = '[ ' ;
979- foreach ($ list as $ item ) {
980- $ string .= self ::quote ($ item ) . ', ' ;
981- }
982- if ($ list ) {
983- $ string = substr ($ string , 0 , -1 );
984- }
985- return $ string . '] ' ;
971+ return '[ ' . implode (', ' , array_map (self ::quote (...), $ list )) . '] ' ;
986972 }
987973
988974 private function missValue (string $ key ): string
@@ -992,11 +978,11 @@ private function missValue(string $key): string
992978 : 'null ' ;
993979 }
994980
995- /** @return array{list< string> , string} [$bp, $bs] */
981+ /** @return array{string[] , string} [$bp, $bs] */
996982 private function getProgramBlockParams (?Program $ program ): array
997983 {
998984 $ bp = $ program ? $ program ->blockParams : [];
999- $ bs = $ bp ? self ::listString ($ bp ) : ' null ' ;
985+ $ bs = self ::listString ($ bp );
1000986 return [$ bp , $ bs ];
1001987 }
1002988
@@ -1007,7 +993,7 @@ private function compileProgramOrEmpty(?Program $program, bool $withSp = false):
1007993
1008994 /**
1009995 * Return only the string parts of a mixed parts array, re-indexed.
1010- * @param list <string|SubExpression> $parts
996+ * @param array <string|SubExpression> $parts
1011997 * @return list<string>
1012998 */
1013999 private static function stringPartsOf (array $ parts ): array
@@ -1048,20 +1034,6 @@ private function compileLookup(MustacheStatement $mustache, bool $raw): string
10481034 return "'. " . self ::getRuntimeFunc ($ fn , $ varCode ) . ".' " ;
10491035 }
10501036
1051- /**
1052- * Compile lookup as a sub-expression argument.
1053- *
1054- * @param Expression[] $params
1055- */
1056- private function compileLookupExpr (array $ params ): string
1057- {
1058- $ itemsExpr = $ params [0 ];
1059- $ idxExpr = $ params [1 ];
1060- $ varCode = $ this ->getWithLookup ($ itemsExpr , $ idxExpr );
1061-
1062- return self ::getRuntimeFunc ('raw ' , "$ varCode, 1 " );
1063- }
1064-
10651037 /**
10661038 * Compile a path with an additional dynamic lookup segment.
10671039 */
0 commit comments