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
9 changes: 9 additions & 0 deletions doc/grammars/phpdoc-block.pp3
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,17 @@ DoctrineArgumentList
: DoctrineArgument() Eols() ( <T_COMMA> Eols() ( &<T_CLOSE_PARENTHESES> | DoctrineArgument() Eols() ) )*
;

/**
* An argument of an annotation, like:
* - bar="baz"
* - "bar"="baz"
*
* Doctrine reads only a name before the "=", but a key written in quotes is
* read too, as the name of the argument.
*/
DoctrineArgument
: Identifier() Eols() <T_EQUAL> Eols() DoctrineValue()
| DoctrineString() Eols() <T_EQUAL> Eols() DoctrineValue()
| DoctrineValue()
;

Expand Down
9 changes: 7 additions & 2 deletions src/Parser/PhpDocParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ private function parseDoctrineArguments(TokenIterator $tokens, bool $deep): arra

private function parseDoctrineArgument(TokenIterator $tokens): Doctrine\DoctrineArgument
{
if (!$tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)) {
// a quoted key, e.g. "key"="value", is not valid Doctrine syntax, but is accepted as the key
if (!$tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER, Lexer::TOKEN_DOUBLE_QUOTED_STRING, Lexer::TOKEN_DOCTRINE_ANNOTATION_STRING)) {
$startLine = $tokens->currentTokenLine();
$startIndex = $tokens->currentTokenIndex();

Expand All @@ -604,8 +605,12 @@ private function parseDoctrineArgument(TokenIterator $tokens): Doctrine\Doctrine

try {
$tokens->pushSavePoint();
$isIdentifier = $tokens->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER);
$currentValue = $tokens->currentTokenValue();
$tokens->consumeTokenType(Lexer::TOKEN_IDENTIFIER);
$tokens->next();
if (!$isIdentifier) {
$currentValue = $this->doctrineConstantExprParser->parseDoctrineString($currentValue, $tokens)->value;
}

$key = $this->enrichWithAttributes(
$tokens,
Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Parser/PhpDocParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7523,6 +7523,34 @@ public function provideDoctrineWithoutDoctrineCheckData(): Iterator
new PhpDocTagNode('@package', new GenericTagValueNode('foo')),
]),
];
yield [
'Quoted argument key',
'/** @DummyAnnotation(targetEntity=Training::class, "indexBy" = "id", "a""b"="c") */',
new PhpDocNode([
new PhpDocTagNode('@DummyAnnotation', new DoctrineTagValueNode(
new DoctrineAnnotation('@DummyAnnotation', [
new DoctrineArgument(new IdentifierTypeNode('targetEntity'), new ConstFetchNode('Training', 'class')),
new DoctrineArgument(new IdentifierTypeNode('indexBy'), new DoctrineConstExprStringNode('id')),
new DoctrineArgument(new IdentifierTypeNode('a"b'), new DoctrineConstExprStringNode('c')),
]),
'',
)),
]),
];

yield [
'Quoted positional argument',
'/** @DummyAnnotation("foo", "bar") */',
new PhpDocNode([
new PhpDocTagNode('@DummyAnnotation', new DoctrineTagValueNode(
new DoctrineAnnotation('@DummyAnnotation', [
new DoctrineArgument(null, new DoctrineConstExprStringNode('foo')),
new DoctrineArgument(null, new DoctrineConstExprStringNode('bar')),
]),
'',
)),
]),
];
}

public function provideSpecializedTags(): Iterator
Expand Down
18 changes: 18 additions & 0 deletions tests/PHPStan/Printer/PrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprStringNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstFetchNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\DoctrineConstExprStringNode;
use PHPStan\PhpDocParser\Ast\Node;
use PHPStan\PhpDocParser\Ast\NodeTraverser;
use PHPStan\PhpDocParser\Ast\NodeVisitor;
Expand Down Expand Up @@ -2231,6 +2232,23 @@ public function enterNode(Node $node)
},
];

yield [
'/** @Foo("indexBy" = "id", "name"="foo") */',
'/** @Foo("indexBy" = "id", "name"="bar") */',
new class extends AbstractNodeVisitor {

public function enterNode(Node $node)
{
if ($node instanceof DoctrineArgument && $node->key !== null && $node->key->name === 'name') {
$node->value = new DoctrineConstExprStringNode('bar');
}

return $node;
}

},
];

yield [
'/** @Foo() */',
'/** @Bar() */',
Expand Down
Loading