Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ public function prefix(string $prefix, array|Closure|Generator|string $data): Ge
}

foreach (explode(PHP_EOL, $line) as $singleLine) {
yield $prefix . $singleLine;
// An empty line gets the prefix without its trailing whitespace, like `indent()` does
yield $singleLine === '' ? rtrim($prefix) : $prefix . $singleLine;
}
}
}
Expand Down
24 changes: 22 additions & 2 deletions tests/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1385,12 +1385,32 @@ public function testDocComment(): void
]));
}

public function testPrefixDoesNotLeaveTrailingWhitespaceOnEmptyLines(): void
{
self::assertSame(
['// first', '//', '// third'],
iterator_to_array($this->generator->prefix('// ', "first\n\nthird"), preserve_keys: false),
);
self::assertSame(['>'], iterator_to_array($this->generator->prefix('> ', ''), preserve_keys: false));
}

public function testDocCommentWithEmptyLine(): void
{
$this->assertDump(<<<'PHP'
/**
* Summary
*
* @deprecated Use something else
*/
PHP, $this->generator->docComment(['Summary', '', '@deprecated Use something else']));
}

public function testDocCommentWithFullDump(): void
{
$this->assertDump(<<<'PHP'
/**
* Calculate the sum of two numbers
*
*
* @param int $a First number
* @param int $b Second number
* @return int The sum
Expand Down Expand Up @@ -1475,7 +1495,7 @@ public function testBlockCommentWithEmptyString(): void
{
$this->assertDump(<<<'PHP'
/*
*
*
*/
PHP, $this->generator->blockComment(''));
}
Expand Down
Loading