Skip to content

Commit b9606ae

Browse files
committed
minor #4399 Call For recurseFunc inline (ruudk)
This PR was merged into the 4.x branch. Discussion ---------- Call For recurseFunc inline After phpstan/phpstan-src#3554 this now makes it easier for PHPStan to understands what's going on. This will allow TwigStan to handle for loops in v4. Commits ------- e4f6bf8 Call For recurseFunc inline
2 parents eca86f1 + e4f6bf8 commit b9606ae

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/Node/ForNode.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function compile(Compiler $compiler): void
4949
->write("\$$iteratorVar = new \Twig\Runtime\LoopIterator(")
5050
->subcompile($this->getNode('seq'))
5151
->raw(");\n")
52-
->write("\$$functionVar = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {\n")
52+
->write("yield from (\$$functionVar = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {\n")
5353
->indent()
5454
->write("\$macros = \$this->macros;\n")
5555
->write("\$parent = \$context;\n")
@@ -94,8 +94,7 @@ public function compile(Compiler $compiler): void
9494
$compiler
9595
->write("yield from [];\n")
9696
->outdent()
97-
->write("};\n")
98-
->write("yield from \$$functionVar(\$$iteratorVar, \$context, \$blocks, \$$functionVar, 0);\n")
97+
->write("})(\$$iteratorVar, \$context, \$blocks, \$$functionVar, 0);\n")
9998
;
10099
}
101100
}

tests/Node/ForTest.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function provideTests(): iterable
6868
$tests[] = [$node, <<<EOF
6969
// line 1
7070
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$itemsGetter});
71-
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
71+
yield from (\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
7272
\$macros = \$this->macros;
7373
\$parent = \$context;
7474
foreach (\$iterator as \$context["key"] => \$context["item"]) {
@@ -77,8 +77,7 @@ public static function provideTests(): iterable
7777
unset(\$context['key'], \$context['item']);
7878
\$context = array_intersect_key(\$context, \$parent) + \$parent;
7979
yield from [];
80-
};
81-
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
80+
})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
8281
EOF
8382
];
8483

@@ -93,7 +92,7 @@ public static function provideTests(): iterable
9392
$tests[] = [$node, <<<EOF
9493
// line 1
9594
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
96-
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
95+
yield from (\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
9796
\$macros = \$this->macros;
9897
\$parent = \$context;
9998
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
@@ -103,8 +102,7 @@ public static function provideTests(): iterable
103102
unset(\$context['k'], \$context['v'], \$context['loop']);
104103
\$context = array_intersect_key(\$context, \$parent) + \$parent;
105104
yield from [];
106-
};
107-
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
105+
})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
108106
EOF
109107
];
110108

@@ -119,7 +117,7 @@ public static function provideTests(): iterable
119117
$tests[] = [$node, <<<EOF
120118
// line 1
121119
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
122-
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
120+
yield from (\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
123121
\$macros = \$this->macros;
124122
\$parent = \$context;
125123
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
@@ -129,8 +127,7 @@ public static function provideTests(): iterable
129127
unset(\$context['k'], \$context['v'], \$context['loop']);
130128
\$context = array_intersect_key(\$context, \$parent) + \$parent;
131129
yield from [];
132-
};
133-
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
130+
})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
134131
EOF
135132
];
136133

@@ -146,7 +143,7 @@ public static function provideTests(): iterable
146143
$tests[] = [$node, <<<EOF
147144
// line 1
148145
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$valuesGetter});
149-
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
146+
yield from (\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
150147
\$macros = \$this->macros;
151148
\$parent = \$context;
152149
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
@@ -161,8 +158,7 @@ public static function provideTests(): iterable
161158
unset(\$context['k'], \$context['v'], \$context['loop']);
162159
\$context = array_intersect_key(\$context, \$parent) + \$parent;
163160
yield from [];
164-
};
165-
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
161+
})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
166162
EOF
167163
];
168164

@@ -173,7 +169,7 @@ public static function provideTests(): iterable
173169
$tests[] = [$node, <<<EOF
174170
// line 1
175171
\$__internal_compile_0 = new \Twig\Runtime\LoopIterator({$itemsGetter});
176-
\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
172+
yield from (\$__internal_compile_1 = function (\$iterator, &\$context, \$blocks, \$recurseFunc, \$depth) {
177173
\$macros = \$this->macros;
178174
\$parent = \$context;
179175
\$context['loop'] = new \Twig\Runtime\LoopContext(\$iterator, \$parent, \$blocks, \$recurseFunc, \$depth);
@@ -183,8 +179,7 @@ public static function provideTests(): iterable
183179
unset(\$context['_key'], \$context['item'], \$context['loop']);
184180
\$context = array_intersect_key(\$context, \$parent) + \$parent;
185181
yield from [];
186-
};
187-
yield from \$__internal_compile_1(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
182+
})(\$__internal_compile_0, \$context, \$blocks, \$__internal_compile_1, 0);
188183
EOF
189184
, $env];
190185

0 commit comments

Comments
 (0)