Skip to content

Commit

Permalink
[Php80] Handle nested attribute with constant on AnnotationAttributeR…
Browse files Browse the repository at this point in the history
…ector (#1661)
  • Loading branch information
Jean85 committed Jan 13, 2022
1 parent 2607c2a commit a84d951
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;

use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\ConstantReference;

/**
* @GenericAnnotation(
* some={
* ConstantReference::FIRST_NAME={
* foo: {"bar"}
* },
* ConstantReference::LAST_NAME
* }
* )
* @GenericAnnotation(
* some={
* ConstantReference::LAST_NAME,
* trailingValue
* }
* )
*/
final class ArrayWithConstantAsKey
{
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Fixture;

use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\GenericAnnotation;
use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\ConstantReference;

#[GenericAnnotation(some: [ConstantReference::FIRST_NAME => ['foo' => ['bar']], 0 => ConstantReference::LAST_NAME])]
#[GenericAnnotation(some: [ConstantReference::LAST_NAME, 'trailingValue'])]
final class ArrayWithConstantAsKey
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
final class ConstantReference
{
public const FIRST_NAME = 'firstName';
public const LAST_NAME = 'lastName';
}

0 comments on commit a84d951

Please sign in to comment.