@@ -40,18 +40,21 @@ public function generate(
4040 bool $ strictMode = false ,
4141 ): string {
4242 $ context = new PrintContext ($ node ->contentType );
43- $ code = $ node ->main ->print ($ context );
44- $ code = self ::buildParams ($ code , [], '$ʟ_args ' , $ context );
45- $ this ->addMethod ('main ' , $ code , 'array $ʟ_args ' );
43+ $ scope = $ context ->getVariableScope ();
44+ $ this ->addMethod ('main ' , '' );
4645
4746 $ head = (new NodeTraverser )->traverse ($ node ->head , fn (Node $ node ) => $ node instanceof Nodes \TextNode ? new Nodes \NopNode : $ node );
4847 $ code = $ head ->print ($ context );
4948 if ($ code || $ context ->paramsExtraction ) {
5049 $ code .= 'return get_defined_vars(); ' ;
51- $ code = self ::buildParams ($ code , $ context ->paramsExtraction , '$this->params ' , $ context );
50+ $ code = self ::buildParams ($ code , $ context ->paramsExtraction , '$this->params ' , $ context, $ scope );
5251 $ this ->addMethod ('prepare ' , $ code , '' , 'array ' );
5352 }
5453
54+ $ code = $ node ->main ->print ($ context );
55+ $ code = self ::buildParams ($ code , [], '$ʟ_args ' , $ context , $ context ->getVariableScope ());
56+ $ this ->addMethod ('main ' , $ code , 'array $ʟ_args ' );
57+
5558 if ($ node ->contentType !== ContentType::Html) {
5659 $ this ->addConstant ('ContentType ' , $ node ->contentType );
5760 }
@@ -100,7 +103,7 @@ private function generateBlocks(array $blocks, PrintContext $context): void
100103 : [$ block ->method , $ block ->escaping ];
101104 }
102105
103- $ body = $ this -> buildParams ($ block ->content , $ block ->parameters , '$ʟ_args ' , $ context );
106+ $ body = self :: buildParams ($ block ->content , $ block ->parameters , '$ʟ_args ' , $ context, $ block -> variables );
104107 if (!$ block ->isDynamic () && str_contains ($ body , '$ ' )) {
105108 $ embedded = $ block ->tag ->name === 'block ' && is_int ($ block ->layer ) && $ block ->layer ;
106109 $ body = 'extract( ' . ($ embedded ? 'end($this->varStack) ' : '$this->params ' ) . '); ' . $ body ;
@@ -121,16 +124,25 @@ private function generateBlocks(array $blocks, PrintContext $context): void
121124 }
122125
123126
124- private function buildParams (string $ body , array $ params , string $ cont , PrintContext $ context ): string
125- {
127+ /**
128+ * @param Nodes\Php\ParameterNode[] $params
129+ */
130+ private static function buildParams (
131+ string $ body ,
132+ array $ params ,
133+ string $ cont ,
134+ PrintContext $ context ,
135+ VariableScope $ scope ,
136+ ): string {
126137 if (!str_contains ($ body , '$ ' ) && !str_contains ($ body , 'get_defined_vars() ' )) {
127138 return $ body ;
128139 }
129140
130141 $ res = [];
131142 foreach ($ params as $ i => $ param ) {
132143 $ res [] = $ context ->format (
133- '%node = %raw[%dump] ?? %raw[%dump] ?? %node; ' ,
144+ '%raw%node = %raw[%dump] ?? %raw[%dump] ?? %node; ' ,
145+ $ param ->type ? VariableScope::printComment ($ param ->var ->name , $ param ->type ->type ) . ' ' : '' ,
134146 $ param ->var ,
135147 $ cont ,
136148 $ i ,
@@ -143,7 +155,10 @@ private function buildParams(string $body, array $params, string $cont, PrintCon
143155 $ extract = $ params
144156 ? implode ('' , $ res ) . 'unset($ʟ_args); '
145157 : "extract( $ cont); " . (str_contains ($ cont , '$this ' ) ? '' : "unset( $ cont); " );
146- return $ extract . "\n\n" . $ body ;
158+
159+ return $ extract . "\n"
160+ . $ scope ->extractTypes () . "\n\n"
161+ . $ body ;
147162 }
148163
149164
0 commit comments