Skip to content

Commit e4b1dd3

Browse files
authored
Merge pull request #296 from phpDocumentor/fix/287-line-endings
Improve line-endings for windows.
2 parents 44e31d1 + 6fa60f9 commit e4b1dd3

12 files changed

+53
-36
lines changed

examples/03-reconstituting-a-docblock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
$docblock = $factory->create($docComment);
2121

2222
// Create the serializer that will reconstitute the DocBlock back to its original form.
23-
$serializer = new Serializer();
23+
$serializer = new Serializer(0, '', true, null, null, PHP_EOL);
2424

2525
// Reconstitution is performed by the `getDocComment()` method.
2626
$reconstitutedDocComment = $serializer->getDocComment($docblock);

examples/04-adding-your-own-tag.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,5 @@ public function __toString(): string
126126

127127
// As an experiment: let's reconstitute the DocBlock and observe that because we added a __toString() method
128128
// to the tag class that we can now also see it.
129-
$serializer = new Serializer();
129+
$serializer = new Serializer(0, '',true, null, null, PHP_EOL);
130130
$reconstitutedDocComment = $serializer->getDocComment($docblock);

src/DocBlock/DescriptionFactory.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use phpDocumentor\Reflection\Utils;
1818

1919
use function count;
20-
use function explode;
2120
use function implode;
2221
use function ltrim;
2322
use function min;
@@ -146,7 +145,7 @@ private function lex(string $contents): array
146145
*/
147146
private function removeSuperfluousStartingWhitespace(string $contents): string
148147
{
149-
$lines = explode("\n", $contents);
148+
$lines = Utils::pregSplit("/\r\n?|\n/", $contents);
150149

151150
// if there is only one line then we don't have lines with superfluous whitespace and
152151
// can use the contents as-is

src/DocBlock/Serializer.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class Serializer
4242

4343
/** @var Formatter A custom tag formatter. */
4444
protected $tagFormatter;
45+
/** @var string */
46+
private $lineEnding;
4547

4648
/**
4749
* Create a Serializer instance.
@@ -51,19 +53,22 @@ class Serializer
5153
* @param bool $indentFirstLine Whether to indent the first line.
5254
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
5355
* @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
56+
* @param string $lineEnding Line ending used in the output, by default \n is used.
5457
*/
5558
public function __construct(
5659
int $indent = 0,
5760
string $indentString = ' ',
5861
bool $indentFirstLine = true,
5962
?int $lineLength = null,
60-
?Formatter $tagFormatter = null
63+
?Formatter $tagFormatter = null,
64+
string $lineEnding = "\n"
6165
) {
6266
$this->indent = $indent;
6367
$this->indentString = $indentString;
6468
$this->isFirstLineIndented = $indentFirstLine;
6569
$this->lineLength = $lineLength;
6670
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
71+
$this->lineEnding = $lineEnding;
6772
}
6873

6974
/**
@@ -96,7 +101,7 @@ public function getDocComment(DocBlock $docblock): string
96101

97102
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment);
98103

99-
return $comment . $indent . ' */';
104+
return str_replace("\n", $this->lineEnding, $comment . $indent . ' */');
100105
}
101106

102107
private function removeTrailingSpaces(string $indent, string $text): string

src/DocBlock/Tag.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public function getName(): string;
2121

2222
/**
2323
* @return Tag|mixed Class that implements Tag
24-
*
2524
* @phpstan-return ?Tag
2625
*/
2726
public static function create(string $body);

src/DocBlock/Tags/Method.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ final class Method extends BaseTag implements Factory\StaticMethod
5959

6060
/**
6161
* @param array<int, array<string, Type|string>> $arguments
62-
*
6362
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
6463
*/
6564
public function __construct(
@@ -186,7 +185,6 @@ public function getMethodName(): string
186185

187186
/**
188187
* @return array<int, array<string, Type|string>>
189-
*
190188
* @phpstan-return array<int, array{name: string, type: Type}>
191189
*/
192190
public function getArguments(): array
@@ -239,10 +237,9 @@ public function __toString(): string
239237

240238
/**
241239
* @param mixed[][]|string[] $arguments
240+
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
242241
*
243242
* @return mixed[][]
244-
*
245-
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
246243
* @phpstan-return array<int, array{name: string, type: Type}>
247244
*/
248245
private function filterArguments(array $arguments = []): array

src/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
namespace phpDocumentor\Reflection;
1515

1616
use phpDocumentor\Reflection\Exception\PcreException;
17-
1817
use Webmozart\Assert\Assert;
18+
1919
use function preg_last_error;
2020
use function preg_split as php_preg_split;
2121

tests/integration/InterpretingDocBlocksTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,14 @@ public function testInterpretingASimpleDocBlock(): void
7979
$this->assertInstanceOf(DocBlock::class, $docblock);
8080
$this->assertSame('This is an example of a summary.', $summary);
8181
$this->assertInstanceOf(Description::class, $description);
82-
$this->assertSame($descriptionText, $description->render());
82+
$this->assertSame(
83+
str_replace(
84+
PHP_EOL,
85+
"\n",
86+
$descriptionText
87+
),
88+
$description->render()
89+
);
8390
$this->assertEmpty($docblock->getTags());
8491
}
8592

@@ -124,6 +131,9 @@ public function testDescriptionsCanEscapeAtSignsAndClosingBraces(): void
124131
include(__DIR__ . '/../../examples/playing-with-descriptions/02-escaping.php');
125132

126133
$this->assertSame(
134+
str_replace(
135+
PHP_EOL,
136+
"\n",
127137
<<<'DESCRIPTION'
128138
You can escape the @-sign by surrounding it with braces, for example: @. And escape a closing brace within an
129139
inline tag by adding an opening brace in front of it like this: }.
@@ -135,7 +145,7 @@ public function testDescriptionsCanEscapeAtSignsAndClosingBraces(): void
135145
136146
Do note that an {@internal inline tag that has an opening brace ({) does not break out}.
137147
DESCRIPTION
138-
,
148+
),
139149
$foundDescription
140150
);
141151
}

tests/unit/DocBlock/DescriptionFactoryTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
use phpDocumentor\Reflection\Types\Context;
2121
use PHPUnit\Framework\TestCase;
2222

23+
use function str_replace;
24+
25+
use const PHP_EOL;
26+
2327
/**
2428
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\DescriptionFactory
2529
* @covers ::<private>
@@ -192,7 +196,7 @@ public function testIfSuperfluousStartingSpacesAreRemoved(): void
192196

193197
$description = $factory->create($descriptionText, new Context(''));
194198

195-
$this->assertSame($expectedDescription, $description->render());
199+
$this->assertSame(str_replace(PHP_EOL, "\n", $expectedDescription), $description->render());
196200
}
197201

198202
/**

tests/unit/DocBlock/SerializerTest.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
use phpDocumentor\Reflection\DocBlock;
1818
use PHPUnit\Framework\TestCase;
1919

20+
use function str_replace;
21+
22+
use const PHP_EOL;
23+
2024
/**
2125
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Serializer
2226
* @covers ::<private>
@@ -63,7 +67,7 @@ public function testReconstructsADocCommentFromADocBlock(): void
6367
]
6468
);
6569

66-
$this->assertSame($expected, $fixture->getDocComment($docBlock));
70+
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
6771
}
6872

6973
/**
@@ -98,7 +102,7 @@ public function testAddPrefixToDocBlock(): void
98102
]
99103
);
100104

101-
$this->assertSame($expected, $fixture->getDocComment($docBlock));
105+
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
102106
}
103107

104108
/**
@@ -133,7 +137,7 @@ public function testAddPrefixToDocBlockExceptFirstLine(): void
133137
]
134138
);
135139

136-
$this->assertSame($expected, $fixture->getDocComment($docBlock));
140+
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
137141
}
138142

139143
/**
@@ -174,7 +178,7 @@ public function testWordwrapsAroundTheGivenAmountOfCharacters(): void
174178
]
175179
);
176180

177-
$this->assertSame($expected, $fixture->getDocComment($docBlock));
181+
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
178182
}
179183

180184
/**
@@ -198,9 +202,16 @@ public function testNoExtraSpacesAfterTagRemoval(): void
198202
$genericTag = new DocBlock\Tags\Generic('unknown-tag');
199203

200204
$docBlock = new DocBlock('', null, [$genericTag]);
201-
$this->assertSame($expected, $fixture->getDocComment($docBlock));
205+
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
202206

203207
$docBlock->removeTag($genericTag);
204-
$this->assertSame($expectedAfterRemove, $fixture->getDocComment($docBlock));
208+
$this->assertSameString($expectedAfterRemove, $fixture->getDocComment($docBlock));
209+
}
210+
211+
public function assertSameString(string $expected, string $actual): void
212+
{
213+
$expected = str_replace(PHP_EOL, "\n", $expected);
214+
215+
self::assertSame($expected, $actual);
205216
}
206217
}

0 commit comments

Comments
 (0)