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
2 changes: 1 addition & 1 deletion examples/03-reconstituting-a-docblock.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
$docblock = $factory->create($docComment);

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

// Reconstitution is performed by the `getDocComment()` method.
$reconstitutedDocComment = $serializer->getDocComment($docblock);
Expand Down
2 changes: 1 addition & 1 deletion examples/04-adding-your-own-tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,5 +126,5 @@ public function __toString(): string

// As an experiment: let's reconstitute the DocBlock and observe that because we added a __toString() method
// to the tag class that we can now also see it.
$serializer = new Serializer();
$serializer = new Serializer(0, '',true, null, null, PHP_EOL);
$reconstitutedDocComment = $serializer->getDocComment($docblock);
3 changes: 1 addition & 2 deletions src/DocBlock/DescriptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use phpDocumentor\Reflection\Utils;

use function count;
use function explode;
use function implode;
use function ltrim;
use function min;
Expand Down Expand Up @@ -146,7 +145,7 @@ private function lex(string $contents): array
*/
private function removeSuperfluousStartingWhitespace(string $contents): string
{
$lines = explode("\n", $contents);
$lines = Utils::pregSplit("/\r\n?|\n/", $contents);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This an actual bugfix. windows line endings were not handled here.


// if there is only one line then we don't have lines with superfluous whitespace and
// can use the contents as-is
Expand Down
9 changes: 7 additions & 2 deletions src/DocBlock/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class Serializer

/** @var Formatter A custom tag formatter. */
protected $tagFormatter;
/** @var string */
private $lineEnding;

/**
* Create a Serializer instance.
Expand All @@ -51,19 +53,22 @@ class Serializer
* @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping.
* @param Formatter $tagFormatter A custom tag formatter, defaults to PassthroughFormatter.
* @param string $lineEnding Line ending used in the output, by default \n is used.
*/
public function __construct(
int $indent = 0,
string $indentString = ' ',
bool $indentFirstLine = true,
?int $lineLength = null,
?Formatter $tagFormatter = null
?Formatter $tagFormatter = null,
string $lineEnding = "\n"
) {
$this->indent = $indent;
$this->indentString = $indentString;
$this->isFirstLineIndented = $indentFirstLine;
$this->lineLength = $lineLength;
$this->tagFormatter = $tagFormatter ?: new PassthroughFormatter();
$this->lineEnding = $lineEnding;
}

/**
Expand Down Expand Up @@ -96,7 +101,7 @@ public function getDocComment(DocBlock $docblock): string

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

return $comment . $indent . ' */';
return str_replace("\n", $this->lineEnding, $comment . $indent . ' */');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new feature :-)

}

private function removeTrailingSpaces(string $indent, string $text): string
Expand Down
1 change: 0 additions & 1 deletion src/DocBlock/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public function getName(): string;

/**
* @return Tag|mixed Class that implements Tag
*
* @phpstan-return ?Tag
*/
public static function create(string $body);
Expand Down
5 changes: 1 addition & 4 deletions src/DocBlock/Tags/Method.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ final class Method extends BaseTag implements Factory\StaticMethod

/**
* @param array<int, array<string, Type|string>> $arguments
*
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
*/
public function __construct(
Expand Down Expand Up @@ -186,7 +185,6 @@ public function getMethodName(): string

/**
* @return array<int, array<string, Type|string>>
*
* @phpstan-return array<int, array{name: string, type: Type}>
*/
public function getArguments(): array
Expand Down Expand Up @@ -239,10 +237,9 @@ public function __toString(): string

/**
* @param mixed[][]|string[] $arguments
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
*
* @return mixed[][]
*
* @phpstan-param array<int, array{name: string, type: Type}|string> $arguments
* @phpstan-return array<int, array{name: string, type: Type}>
*/
private function filterArguments(array $arguments = []): array
Expand Down
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
namespace phpDocumentor\Reflection;

use phpDocumentor\Reflection\Exception\PcreException;

use Webmozart\Assert\Assert;

use function preg_last_error;
use function preg_split as php_preg_split;

Expand Down
14 changes: 12 additions & 2 deletions tests/integration/InterpretingDocBlocksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ public function testInterpretingASimpleDocBlock(): void
$this->assertInstanceOf(DocBlock::class, $docblock);
$this->assertSame('This is an example of a summary.', $summary);
$this->assertInstanceOf(Description::class, $description);
$this->assertSame($descriptionText, $description->render());
$this->assertSame(
str_replace(
PHP_EOL,
"\n",
$descriptionText
),
$description->render()
);
$this->assertEmpty($docblock->getTags());
}

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

$this->assertSame(
str_replace(
PHP_EOL,
"\n",
<<<'DESCRIPTION'
You can escape the @-sign by surrounding it with braces, for example: @. And escape a closing brace within an
inline tag by adding an opening brace in front of it like this: }.
Expand All @@ -135,7 +145,7 @@ public function testDescriptionsCanEscapeAtSignsAndClosingBraces(): void

Do note that an {@internal inline tag that has an opening brace ({) does not break out}.
DESCRIPTION
,
),
$foundDescription
);
}
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/DocBlock/DescriptionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
use phpDocumentor\Reflection\Types\Context;
use PHPUnit\Framework\TestCase;

use function str_replace;

use const PHP_EOL;

/**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\DescriptionFactory
* @covers ::<private>
Expand Down Expand Up @@ -192,7 +196,7 @@ public function testIfSuperfluousStartingSpacesAreRemoved(): void

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

$this->assertSame($expectedDescription, $description->render());
$this->assertSame(str_replace(PHP_EOL, "\n", $expectedDescription), $description->render());
}

/**
Expand Down
23 changes: 17 additions & 6 deletions tests/unit/DocBlock/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
use phpDocumentor\Reflection\DocBlock;
use PHPUnit\Framework\TestCase;

use function str_replace;

use const PHP_EOL;

/**
* @coversDefaultClass \phpDocumentor\Reflection\DocBlock\Serializer
* @covers ::<private>
Expand Down Expand Up @@ -63,7 +67,7 @@ public function testReconstructsADocCommentFromADocBlock(): void
]
);

$this->assertSame($expected, $fixture->getDocComment($docBlock));
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
}

/**
Expand Down Expand Up @@ -98,7 +102,7 @@ public function testAddPrefixToDocBlock(): void
]
);

$this->assertSame($expected, $fixture->getDocComment($docBlock));
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
}

/**
Expand Down Expand Up @@ -133,7 +137,7 @@ public function testAddPrefixToDocBlockExceptFirstLine(): void
]
);

$this->assertSame($expected, $fixture->getDocComment($docBlock));
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
}

/**
Expand Down Expand Up @@ -174,7 +178,7 @@ public function testWordwrapsAroundTheGivenAmountOfCharacters(): void
]
);

$this->assertSame($expected, $fixture->getDocComment($docBlock));
$this->assertSameString($expected, $fixture->getDocComment($docBlock));
}

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

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

$docBlock->removeTag($genericTag);
$this->assertSame($expectedAfterRemove, $fixture->getDocComment($docBlock));
$this->assertSameString($expectedAfterRemove, $fixture->getDocComment($docBlock));
}

public function assertSameString(string $expected, string $actual): void
{
$expected = str_replace(PHP_EOL, "\n", $expected);

self::assertSame($expected, $actual);
}
}
2 changes: 0 additions & 2 deletions tests/unit/DocBlock/StandardTagFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,6 @@ public function testValidFormattedTags(string $input, string $tagName, string $r

/**
* @return string[][]
*
* @phpstan-return array<string, array<int, string>>
*/
public function validTagProvider(): array
Expand Down Expand Up @@ -526,7 +525,6 @@ public function testInValidFormattedTags(string $input): void

/**
* @return string[][]
*
* @phpstan-return list<array<int, string>>
*/
public function invalidTagProvider(): array
Expand Down
20 changes: 7 additions & 13 deletions tests/unit/DocBlockFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
use PHPUnit\Framework\TestCase;
use ReflectionClass;

use function str_replace;

use const PHP_EOL;

/**
* @uses \Webmozart\Assert\Assert
* @uses \phpDocumentor\Reflection\DocBlock
Expand Down Expand Up @@ -134,7 +138,7 @@ public function testSummaryAndDescriptionAreSeparated(string $given, string $sum
$docblock = $fixture->create($given);

$this->assertSame($summary, $docblock->getSummary());
$this->assertEquals(new Description($description), $docblock->getDescription());
$this->assertEquals(new Description(str_replace(PHP_EOL, "\n", $description)), $docblock->getDescription());
}

/**
Expand All @@ -159,12 +163,7 @@ public function testDescriptionsRetainFormatting(): void
*/
DOCBLOCK;

$description = <<<DESCRIPTION
This is a multiline Description
that contains a code block.

See here: a CodeBlock
DESCRIPTION;
$description = "This is a multiline Description\nthat contains a code block.\n\n See here: a CodeBlock";

$docblock = $fixture->create($given);

Expand All @@ -180,14 +179,9 @@ public function testDescriptionsRetainFormatting(): void
*/
public function testTagsAreInterpretedUsingFactory(): void
{
$tagString = <<<TAG
@author Mike van Riel <me@mikevanriel.com> This is with
multiline description.
TAG;

$tag = m::mock(Tag::class);
$tagFactory = m::mock(TagFactory::class);
$tagFactory->shouldReceive('create')->with($tagString, m::type(Context::class))->andReturn($tag);
$tagFactory->shouldReceive('create')->with(m::any(), m::type(Context::class))->andReturn($tag);

$fixture = new DocBlockFactory(new DescriptionFactory($tagFactory), $tagFactory);

Expand Down