Skip to content
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

[PhpAttribute] Fix array key of class constant reference #1659

Merged
merged 4 commits into from
Jan 11, 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
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,35 @@ private function resolveArrayItem(BetterTokenIterator $tokenIterator): array
// skip newlines
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);

if ($tokenIterator->isNextTokenTypes([Lexer::TOKEN_EQUAL, Lexer::TOKEN_COLON])) {
$key = null;

// join "ClassName::CONSTANT_REFERENCE" to identifier
if ($tokenIterator->isNextTokenTypes([Lexer::TOKEN_DOUBLE_COLON])) {
$key = $tokenIterator->currentTokenValue();

// "::"
$tokenIterator->next();
$key .= $tokenIterator->currentTokenValue();

$tokenIterator->consumeTokenType(Lexer::TOKEN_DOUBLE_COLON);
$key .= $tokenIterator->currentTokenValue();

$tokenIterator->next();
}

if ($tokenIterator->isCurrentTokenTypes(
[Lexer::TOKEN_EQUAL, Lexer::TOKEN_COLON]
) || $tokenIterator->isNextTokenTypes([Lexer::TOKEN_EQUAL, Lexer::TOKEN_COLON])) {
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_EQUAL);
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_COLON);

if ($tokenIterator->isNextTokenType(Lexer::TOKEN_IDENTIFIER)) {
$key = $this->plainValueParser->parseValue($tokenIterator);
} else {
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_COMMA);
$key = $this->plainValueParser->parseValue($tokenIterator);
if ($key === null) {
if ($tokenIterator->isNextTokenType(Lexer::TOKEN_IDENTIFIER)) {
$key = $this->plainValueParser->parseValue($tokenIterator);
} else {
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_COMMA);
$key = $this->plainValueParser->parseValue($tokenIterator);
}
}

$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_EQUAL);
Expand All @@ -97,7 +117,7 @@ private function resolveArrayItem(BetterTokenIterator $tokenIterator): array
return [$key, $this->plainValueParser->parseValue($tokenIterator)];
}

return [null, $this->plainValueParser->parseValue($tokenIterator)];
return [$key, $this->plainValueParser->parseValue($tokenIterator)];
}

/**
Expand Down
46 changes: 46 additions & 0 deletions packages/PhpAttribute/NodeFactory/NamedArgsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@

namespace Rector\PhpAttribute\NodeFactory;

use Nette\Utils\Strings;
use PhpParser\BuilderHelpers;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Rector\NodeTypeResolver\Node\AttributeKey;

final class NamedArgsFactory
{
/**
* @see https://regex101.com/r/1bJR0J/1
* @var string
*/
private const CLASS_CONST_REGEX = '#(?<class>\w+)::(?<constant>\w+)#';

/**
* @param array<string|int, mixed|Expr> $values
* @return Arg[]
Expand All @@ -23,6 +34,9 @@ public function createFromValues(array $values): array

foreach ($values as $key => $argValue) {
$expr = BuilderHelpers::normalizeValue($argValue);

$this->normalizeArrayWithConstFetchKey($expr);

$name = null;

// for named arguments
Expand Down Expand Up @@ -55,4 +69,36 @@ private function normalizeStringDoubleQuote(Expr $expr): void

$expr->setAttribute(AttributeKey::KIND, String_::KIND_DOUBLE_QUOTED);
}

private function normalizeArrayWithConstFetchKey(Expr $expr): void
{
if (! $expr instanceof Array_) {
return;
}

foreach ($expr->items as $arrayItem) {
if (! $arrayItem instanceof ArrayItem) {
continue;
}

if (! $arrayItem->key instanceof String_) {
continue;
}

$string = $arrayItem->key;

$match = Strings::match($string->value, self::CLASS_CONST_REGEX);
if ($match === null) {
continue;
}

/** @var string $class */
$class = $match['class'];

/** @var string $constant */
$constant = $match['constant'];

$arrayItem->key = new ClassConstFetch(new Name($class), $constant);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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(
* properties={
* ConstantReference::FIRST_NAME = "John",
* }
* )
*/
final class NestedArray
{
}

?>
-----
<?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(properties: [ConstantReference::FIRST_NAME => 'John'])]
final class NestedArray
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

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

final class ConstantReference
{
public const FIRST_NAME = 'firstName';
}