Skip to content

Commit 466ef30

Browse files
committed
Closure: chops 'uses' when are longer than WRAP_LENGTH
1 parent 5891f37 commit 466ef30

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/PhpGenerator/Closure.php

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

tests/PhpGenerator/Closure.long.phpt

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

0 commit comments

Comments
 (0)