Skip to content

Commit 2e55362

Browse files
committed
Closure: chops 'uses' when are longer than WRAP_LENGTH
1 parent 42a2193 commit 2e55362

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

src/PhpGenerator/Closure.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ public function __toString()
4242
foreach ($this->uses as $param) {
4343
$uses[] = ($param->isReference() ? '&' : '') . '$' . $param->getName();
4444
}
45+
$useStr = strlen($tmp = implode(', ', $uses)) > Helpers::WRAP_LENGTH && count($uses) > 1
46+
? "\n\t" . implode(",\n\t", $uses) . "\n"
47+
: $tmp;
48+
4549
return 'function '
4650
. ($this->returnReference ? '&' : '')
4751
. $this->parametersToString()
48-
. ($this->uses ? ' use (' . implode(', ', $uses) . ')' : '')
52+
. ($this->uses ? " use ($useStr)" : '')
4953
. $this->returnTypeToString()
5054
. " {\n" . Nette\Utils\Strings::indent(ltrim(rtrim($this->body) . "\n"), 1) . '}';
5155
}

tests/PhpGenerator/Closure.long.phpt

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
use Nette\PhpGenerator\Closure;
4+
use Tester\Assert;
5+
6+
7+
require __DIR__ . '/../bootstrap.php';
8+
9+
10+
$function = new Closure;
11+
$function->setBody('return null;');
12+
13+
for ($name = 'abcde'; $name < 'abcdr'; $name++) {
14+
$function->addParameter($name);
15+
$function->addUse($name);
16+
}
17+
18+
Assert::match(
19+
'function (
20+
$abcde,
21+
$abcdf,
22+
$abcdg,
23+
$abcdh,
24+
$abcdi,
25+
$abcdj,
26+
$abcdk,
27+
$abcdl,
28+
$abcdm,
29+
$abcdn,
30+
$abcdo,
31+
$abcdp,
32+
$abcdq
33+
) use (
34+
$abcde,
35+
$abcdf,
36+
$abcdg,
37+
$abcdh,
38+
$abcdi,
39+
$abcdj,
40+
$abcdk,
41+
$abcdl,
42+
$abcdm,
43+
$abcdn,
44+
$abcdo,
45+
$abcdp,
46+
$abcdq
47+
) {
48+
return null;
49+
}', (string) $function);

0 commit comments

Comments
 (0)