From a84d9511bc69b509039b87fad7f8868c5bef0a93 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Thu, 13 Jan 2022 14:05:38 +0100 Subject: [PATCH] [Php80] Handle nested attribute with constant on AnnotationAttributeRector (#1661) --- .../ArrayParser.php | 7 +++ .../nested_array_with_constants.php.inc | 43 +++++++++++++++++++ .../Source/ConstantReference.php | 1 + 3 files changed, 51 insertions(+) create mode 100644 rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_array_with_constants.php.inc diff --git a/packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php b/packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php index 3ec70b0cd0e..27b14ec9ffa 100644 --- a/packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php +++ b/packages/BetterPhpDocParser/PhpDocParser/StaticDoctrineAnnotationParser/ArrayParser.php @@ -96,6 +96,13 @@ private function resolveArrayItem(BetterTokenIterator $tokenIterator): array $tokenIterator->next(); } + $tokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL); + + if ($tokenIterator->isCurrentTokenTypes([Lexer::TOKEN_CLOSE_CURLY_BRACKET, Lexer::TOKEN_COMMA])) { + // it's a value, not a key + return [null, $key]; + } + if ($tokenIterator->isCurrentTokenTypes( [Lexer::TOKEN_EQUAL, Lexer::TOKEN_COLON] ) || $tokenIterator->isNextTokenTypes([Lexer::TOKEN_EQUAL, Lexer::TOKEN_COLON])) { diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_array_with_constants.php.inc b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_array_with_constants.php.inc new file mode 100644 index 00000000000..951092472f6 --- /dev/null +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Fixture/nested_array_with_constants.php.inc @@ -0,0 +1,43 @@ + +----- + ['foo' => ['bar']], 0 => ConstantReference::LAST_NAME])] +#[GenericAnnotation(some: [ConstantReference::LAST_NAME, 'trailingValue'])] +final class ArrayWithConstantAsKey +{ +} + +?> diff --git a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/ConstantReference.php b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/ConstantReference.php index e4575dd3844..becf2ffa4dd 100644 --- a/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/ConstantReference.php +++ b/rules-tests/Php80/Rector/Class_/AnnotationToAttributeRector/Source/ConstantReference.php @@ -7,4 +7,5 @@ final class ConstantReference { public const FIRST_NAME = 'firstName'; + public const LAST_NAME = 'lastName'; }