Skip to content

Make requiring whitespace before description optional #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 9, 2022
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
9 changes: 7 additions & 2 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ class PhpDocParser
/** @var ConstExprParser */
private $constantExprParser;

public function __construct(TypeParser $typeParser, ConstExprParser $constantExprParser)
/** @var bool */
private $requireWhitespaceBeforeDescription;

public function __construct(TypeParser $typeParser, ConstExprParser $constantExprParser, bool $requireWhitespaceBeforeDescription = false)
{
$this->typeParser = $typeParser;
$this->constantExprParser = $constantExprParser;
$this->requireWhitespaceBeforeDescription = $requireWhitespaceBeforeDescription;
}


Expand Down Expand Up @@ -492,7 +496,8 @@ private function parseOptionalDescription(TokenIterator $tokens, bool $limitStar
}

if (
!$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END)
$this->requireWhitespaceBeforeDescription
&& !$tokens->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_CLOSE_PHPDOC, Lexer::TOKEN_END)
&& !$tokens->isPrecededByHorizontalWhitespace()
) {
$tokens->consumeTokenType(Lexer::TOKEN_HORIZONTAL_WS); // will throw exception
Expand Down
91 changes: 84 additions & 7 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ class PhpDocParserTest extends TestCase
/** @var PhpDocParser */
private $phpDocParser;

/** @var PhpDocParser */
private $phpDocParserWithRequiredWhitespaceBeforeDescription;

protected function setUp(): void
{
parent::setUp();
$this->lexer = new Lexer();
$constExprParser = new ConstExprParser();
$this->phpDocParser = new PhpDocParser(new TypeParser($constExprParser), $constExprParser);
$typeParser = new TypeParser($constExprParser);
$this->phpDocParser = new PhpDocParser($typeParser, $constExprParser);
$this->phpDocParserWithRequiredWhitespaceBeforeDescription = new PhpDocParser($typeParser, $constExprParser, true);
}


Expand All @@ -83,14 +88,37 @@ protected function setUp(): void
* @dataProvider provideRealWorldExampleData
* @dataProvider provideDescriptionWithOrWithoutHtml
*/
public function testParse(string $label, string $input, PhpDocNode $expectedPhpDocNode, int $nextTokenType = Lexer::TOKEN_END): void
public function testParse(
string $label,
string $input,
PhpDocNode $expectedPhpDocNode,
?PhpDocNode $withRequiredWhitespaceBeforeDescriptionExpectedPhpDocNode = null
): void
{
$this->executeTestParse(
$this->phpDocParser,
$label,
$input,
$expectedPhpDocNode
);

$this->executeTestParse(
$this->phpDocParserWithRequiredWhitespaceBeforeDescription,
$label,
$input,
$withRequiredWhitespaceBeforeDescriptionExpectedPhpDocNode ?? $expectedPhpDocNode
);
}


private function executeTestParse(PhpDocParser $phpDocParser, string $label, string $input, PhpDocNode $expectedPhpDocNode): void
{
$tokens = new TokenIterator($this->lexer->tokenize($input));
$actualPhpDocNode = $this->phpDocParser->parse($tokens);
$actualPhpDocNode = $phpDocParser->parse($tokens);

$this->assertEquals($expectedPhpDocNode, $actualPhpDocNode, $label);
$this->assertSame((string) $expectedPhpDocNode, (string) $actualPhpDocNode);
$this->assertSame($nextTokenType, $tokens->currentTokenType());
$this->assertSame(Lexer::TOKEN_END, $tokens->currentTokenType());
}


Expand Down Expand Up @@ -675,6 +703,16 @@ public function provideVarTagsData(): Iterator
)
),
]),
new PhpDocNode([
new PhpDocTagNode(
'@var',
new VarTagValueNode(
new IdentifierTypeNode('Foo'),
'$foo',
'#desc'
)
),
]),
];

yield [
Expand Down Expand Up @@ -1459,6 +1497,15 @@ public function provideReturnTagsData(): Iterator
yield [
'invalid variadic callable',
'/** @return \Closure(...int, string): string */',
new PhpDocNode([
new PhpDocTagNode(
'@return',
new ReturnTagValueNode(
new IdentifierTypeNode('\Closure'),
'(...int, string): string'
)
),
]),
new PhpDocNode([
new PhpDocTagNode(
'@return',
Expand Down Expand Up @@ -2265,6 +2312,16 @@ public function provideSingleLinePhpDocData(): Iterator
yield [
'callable with incomplete signature without return type',
'/** @var callable(int) */',
new PhpDocNode([
new PhpDocTagNode(
'@var',
new VarTagValueNode(
new IdentifierTypeNode('callable'),
'',
'(int)'
)
),
]),
new PhpDocNode([
new PhpDocTagNode(
'@var',
Expand Down Expand Up @@ -4241,6 +4298,20 @@ public function provideDescriptionWithOrWithoutHtml(): Iterator
'/**' . PHP_EOL .
' * @return Foo <strong>Important description' . PHP_EOL .
' */',
new PhpDocNode([
new PhpDocTagNode(
'@return',
new ReturnTagValueNode(
new GenericTypeNode(
new IdentifierTypeNode('Foo'),
[
new IdentifierTypeNode('strong'),
]
),
'Important description'
)
),
]),
new PhpDocNode([
new PhpDocTagNode(
'@return',
Expand Down Expand Up @@ -4305,14 +4376,20 @@ public function provideTagsWithNumbers(): Iterator
* @dataProvider dataParseTagValue
* @param PhpDocNode $expectedPhpDocNode
*/
public function testParseTagValue(string $tag, string $phpDoc, Node $expectedPhpDocNode, int $nextTokenType = Lexer::TOKEN_END): void
public function testParseTagValue(string $tag, string $phpDoc, Node $expectedPhpDocNode): void
{
$this->executeTestParseTagValue($this->phpDocParser, $tag, $phpDoc, $expectedPhpDocNode);
$this->executeTestParseTagValue($this->phpDocParserWithRequiredWhitespaceBeforeDescription, $tag, $phpDoc, $expectedPhpDocNode);
}

private function executeTestParseTagValue(PhpDocParser $phpDocParser, string $tag, string $phpDoc, Node $expectedPhpDocNode): void
{
$tokens = new TokenIterator($this->lexer->tokenize($phpDoc));
$actualPhpDocNode = $this->phpDocParser->parseTagValue($tokens, $tag);
$actualPhpDocNode = $phpDocParser->parseTagValue($tokens, $tag);

$this->assertEquals($expectedPhpDocNode, $actualPhpDocNode);
$this->assertSame((string) $expectedPhpDocNode, (string) $actualPhpDocNode);
$this->assertSame($nextTokenType, $tokens->currentTokenType());
$this->assertSame(Lexer::TOKEN_END, $tokens->currentTokenType());
}

}