Skip to content

Commit 8831a3e

Browse files
committed
Dumper, Helpers::formatDocComment(), Printer::printFunction() & etc normalizes whitespace
1 parent 2618071 commit 8831a3e

File tree

7 files changed

+18
-13
lines changed

7 files changed

+18
-13
lines changed

src/PhpGenerator/Dumper.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ private function dumpObject(object $var, array $parents, int $level): string
198198
private function dumpLiteral(Literal $var, int $level): string
199199
{
200200
$s = $var->formatWith($this);
201+
$s = Nette\Utils\Strings::normalizeNewlines($s);
201202
$s = Nette\Utils\Strings::indent(trim($s), $level, $this->indentation);
202203
return ltrim($s, $this->indentation);
203204
}

src/PhpGenerator/Extractor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ private function parseCode(string $code): void
4848
throw new Nette\InvalidStateException('The input string is not a PHP code.');
4949
}
5050

51-
$this->code = str_replace("\r\n", "\n", $code);
51+
$this->code = Nette\Utils\Strings::normalizeNewlines($code);
5252
$lexer = new PhpParser\Lexer\Emulative(['usedAttributes' => ['startFilePos', 'endFilePos', 'comments']]);
5353
$parser = (new ParserFactory)->create(ParserFactory::ONLY_PHP7, $lexer);
5454
$stmts = $parser->parse($this->code);

src/PhpGenerator/Helpers.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ public static function formatDocComment(string $content): string
8484
if ($s === '') {
8585
return '';
8686
} elseif (str_contains($content, "\n")) {
87-
return str_replace("\n", "\n * ", "/**\n$s") . "\n */\n";
87+
$s = str_replace("\n", "\n * ", "/**\n$s") . "\n */";
88+
return Nette\Utils\Strings::normalize($s) . "\n";
8889
} else {
8990
return "/** $s */\n";
9091
}

src/PhpGenerator/Printer.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ public function printFunction(GlobalFunction $function, ?PhpNamespace $namespace
4444
. $function->getName();
4545
$returnType = $this->printReturnType($function);
4646
$body = Helpers::simplifyTaggedNames($function->getBody(), $this->namespace);
47+
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
4748

4849
return Helpers::formatDocComment($function->getComment() . "\n")
4950
. self::printAttributes($function->getAttributes())
5051
. $line
5152
. $this->printParameters($function, strlen($line) + strlen($returnType) + 2) // 2 = parentheses
5253
. $returnType
53-
. "\n{\n" . $this->indent(ltrim(rtrim($body) . "\n")) . "}\n";
54+
. "\n{\n" . $this->indent($body) . "}\n";
5455
}
5556

5657

@@ -66,14 +67,15 @@ public function printClosure(Closure $closure, ?PhpNamespace $namespace = null):
6667
? "\n" . $this->indentation . implode(",\n" . $this->indentation, $uses) . ",\n"
6768
: $tmp;
6869
$body = Helpers::simplifyTaggedNames($closure->getBody(), $this->namespace);
70+
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
6971

7072
return self::printAttributes($closure->getAttributes(), inline: true)
7173
. 'function '
7274
. ($closure->getReturnReference() ? '&' : '')
7375
. $this->printParameters($closure)
7476
. ($uses ? " use ($useStr)" : '')
7577
. $this->printReturnType($closure)
76-
. " {\n" . $this->indent(ltrim(rtrim($body) . "\n")) . '}';
78+
. " {\n" . $this->indent($body) . '}';
7779
}
7880

7981

@@ -93,7 +95,7 @@ public function printArrowFunction(Closure $closure, ?PhpNamespace $namespace =
9395
. ($closure->getReturnReference() ? '&' : '')
9496
. $this->printParameters($closure)
9597
. $this->printReturnType($closure)
96-
. ' => ' . trim($body) . ';';
98+
. ' => ' . trim(Strings::normalize($body)) . ';';
9799
}
98100

99101

@@ -110,7 +112,8 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
110112
. $method->getName();
111113
$returnType = $this->printReturnType($method);
112114
$params = $this->printParameters($method, strlen($line) + strlen($returnType) + strlen($this->indentation) + 2);
113-
$body = Helpers::simplifyTaggedNames((string) $method->getBody(), $this->namespace);
115+
$body = Helpers::simplifyTaggedNames($method->getBody(), $this->namespace);
116+
$body = ltrim(rtrim(Strings::normalize($body)) . "\n");
114117

115118
return Helpers::formatDocComment($method->getComment() . "\n")
116119
. self::printAttributes($method->getAttributes())
@@ -119,10 +122,7 @@ public function printMethod(Method $method, ?PhpNamespace $namespace = null, boo
119122
. $returnType
120123
. ($method->isAbstract() || $isInterface
121124
? ";\n"
122-
: (str_contains($params, "\n") ? ' ' : "\n")
123-
. "{\n"
124-
. $this->indent(ltrim(rtrim($body) . "\n"))
125-
. "}\n");
125+
: (str_contains($params, "\n") ? ' ' : "\n") . "{\n" . $this->indent($body) . "}\n");
126126
}
127127

128128

tests/PhpGenerator/Dumper.dump().phpt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Assert::same('\'He\ll\\\\\o \\\'wor\\\\\\\'ld\\\\\'', $dumper->dump('He\ll\\\o \
4949
// literal
5050
Assert::same('[$s]', $dumper->dump([new Literal('$s')]));
5151
Assert::same("[strlen('hello')]", $dumper->dump([new Literal('strlen(?)', ['hello'])]));
52+
Assert::same("a\nb", $dumper->dump(new Literal("a\r\nb")));
5253

5354

5455
// arrays

tests/PhpGenerator/Helpers.comments.phpt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Assert::same('', Helpers::formatDocComment(' '));
1717
Assert::same("/** @var string */\n", Helpers::formatDocComment('@var string'));
1818
Assert::same("/**\n * @var string\n */\n", Helpers::formatDocComment("@var string\n"));
1919
Assert::same("/**\n * A\n * B\n * C\n */\n", Helpers::formatDocComment("A\nB\nC\n"));
20+
Assert::same("/**\n * @var string\n */\n", Helpers::formatDocComment("@var string \r\n"));
21+
Assert::same("/**\n * A\n *\n * B\n */\n", Helpers::formatDocComment("A\n\nB"));
2022

2123
Assert::same('', Helpers::unformatDocComment(''));
2224
Assert::same('', Helpers::unformatDocComment("/** */\n\r\t"));

tests/PhpGenerator/Printer.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ $class->addMethod('first')
4545
->addComment('@return resource')
4646
->setFinal(true)
4747
->setReturnType('stdClass')
48-
->setBody("func();\nreturn ?;", [['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5, 'ffffffff' => 6]])
48+
->setBody("func(); \r\nreturn ?;", [['aaaaaaaaaaaa' => 1, 'bbbbbbbbbbb' => 2, 'cccccccccccccc' => 3, 'dddddddddddd' => 4, 'eeeeeeeeeeee' => 5, 'ffffffff' => 6]])
4949
->addParameter('var')
5050
->setType('stdClass');
5151

@@ -67,7 +67,7 @@ sameFile(__DIR__ . '/expected/Printer.class-alt.expect', $printer->printClass($c
6767
$function = new Nette\PhpGenerator\GlobalFunction('func');
6868
$function
6969
->setReturnType('stdClass')
70-
->setBody("func();\nreturn 123;")
70+
->setBody("func(); \r\nreturn 123;")
7171
->addParameter('var')
7272
->setType('stdClass');
7373

@@ -77,7 +77,7 @@ sameFile(__DIR__ . '/expected/Printer.function.expect', $printer->printFunction(
7777
$closure = new Nette\PhpGenerator\Closure;
7878
$closure
7979
->setReturnType('stdClass')
80-
->setBody("func();\nreturn 123;")
80+
->setBody("func(); \r\nreturn 123;")
8181
->addParameter('var')
8282
->setType('stdClass');
8383

0 commit comments

Comments
 (0)