Skip to content

Commit

Permalink
[PHP 8.0] Fix attribute multi-line (#337)
Browse files Browse the repository at this point in the history
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Jun 30, 2021
1 parent 748a436 commit 1d1dc73
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@

namespace Rector\BetterPhpDocParser\PhpDocParser;

use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\ArrayParser;
Expand Down Expand Up @@ -52,7 +50,7 @@ public function resolveAnnotationMethodCall(BetterTokenIterator $tokenIterator):
*/
public function resolveAnnotationValue(
BetterTokenIterator $tokenIterator
): CurlyListNode | string | array | ConstExprFalseNode | ConstExprTrueNode | ConstExprIntegerNode | DoctrineAnnotationTagValueNode {
): CurlyListNode | string | array | ConstExprNode | DoctrineAnnotationTagValueNode {
// skips dummy tokens like newlines
$tokenIterator->tryConsumeTokenType(Lexer::TOKEN_PHPDOC_EOL);

Expand Down Expand Up @@ -116,7 +114,7 @@ private function resolveAnnotationValues(BetterTokenIterator $tokenIterator): ar
*/
private function parseValue(
BetterTokenIterator $tokenIterator
): CurlyListNode | string | array | ConstExprFalseNode | ConstExprTrueNode | ConstExprIntegerNode | DoctrineAnnotationTagValueNode {
): CurlyListNode | string | array | ConstExprNode | DoctrineAnnotationTagValueNode {
if ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_OPEN_CURLY_BRACKET)) {
$items = $this->arrayParser->parseCurlyArray($tokenIterator);
return new CurlyListNode($items);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprFalseNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprIntegerNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprNode;
use PHPStan\PhpDocParser\Ast\ConstExpr\ConstExprTrueNode;
use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function autowirePlainValueParser(
*/
public function parseValue(
BetterTokenIterator $tokenIterator
): string | array | ConstExprFalseNode | ConstExprTrueNode | ConstExprIntegerNode | DoctrineAnnotationTagValueNode {
): string | array | ConstExprNode | DoctrineAnnotationTagValueNode {
$currentTokenValue = $tokenIterator->currentTokenValue();

// temporary hackaround multi-line doctrine annotations
Expand All @@ -61,16 +62,9 @@ public function parseValue(
$tokenIterator->next();

// normalize value
if (strtolower($currentTokenValue) === 'false') {
return new ConstExprFalseNode();
}

if (strtolower($currentTokenValue) === 'true') {
return new ConstExprTrueNode();
}

if (is_numeric($currentTokenValue) && (string) (int) $currentTokenValue === $currentTokenValue) {
return new ConstExprIntegerNode($currentTokenValue);
$constantValue = $this->matchConstantValue($currentTokenValue);
if ($constantValue !== null) {
return $constantValue;
}

while ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON) ||
Expand All @@ -85,6 +79,21 @@ public function parseValue(
return $this->parseNestedDoctrineAnnotationTagValueNode($currentTokenValue, $tokenIterator);
}

$start = $tokenIterator->currentPosition();

if ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
while ($tokenIterator->isCurrentTokenTypes(
[Lexer::TOKEN_PHPDOC_EOL, Lexer::TOKEN_IDENTIFIER, Lexer::TOKEN_COLON]
)) {
$tokenIterator->next();
}
}

$end = $tokenIterator->currentPosition();
if ($start + 1 < $end) {
return $tokenIterator->printFromTo($start, $end);
}

return $currentTokenValue;
}

Expand Down Expand Up @@ -113,4 +122,22 @@ private function parseNestedDoctrineAnnotationTagValueNode(
$identifierTypeNode = new IdentifierTypeNode($fullyQualifiedAnnotationClass);
return new DoctrineAnnotationTagValueNode($identifierTypeNode, $annotationShortName, $values);
}

private function matchConstantValue(string $currentTokenValue): ConstExprNode | null
{
if (strtolower($currentTokenValue) === 'false') {
return new ConstExprFalseNode();
}

if (strtolower($currentTokenValue) === 'true') {
return new ConstExprTrueNode();
}
if (! is_numeric($currentTokenValue)) {
return null;
}
if ((string) (int) $currentTokenValue !== $currentTokenValue) {
return null;
}
return new ConstExprIntegerNode($currentTokenValue);
}
}
9 changes: 1 addition & 8 deletions packages/NodeTypeResolver/MethodParameterTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@ public function provideParameterTypesByStaticCall(StaticCall $staticCall): array
return [];
}

$parametersAcceptor = ParametersAcceptorSelector::selectSingle($methodReflection->getVariants());

$parameterTypes = [];
foreach ($parametersAcceptor->getParameters() as $parameterReflection) {
$parameterTypes[] = $parameterReflection->getType();
}

return $parameterTypes;
return $this->provideParameterTypesFromMethodReflection($methodReflection);
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/PhpAttribute/Printer/PhpAttributeGroupFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ public function create(
public function createArgsFromItems(array $items, ?string $silentKey = null): array
{
$args = [];

if ($silentKey !== null && isset($items[$silentKey])) {
$silentValue = BuilderHelpers::normalizeValue($items[$silentKey]);
$this->normalizeStringDoubleQuote($silentValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Path;

/**
* @Path("
* summary: Send webcam reward
* ")
*/
final class MultilineContent
{
}

?>
-----
<?php

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

use Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Path;

#[\Rector\Tests\Php80\Rector\Class_\AnnotationToAttributeRector\Source\Attribute\Path(path: '
summary: Send webcam reward
')]
final class MultilineContent
{
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public function refactor(Node $node): ?Node
private function hasFunctionOrClosureInside(
ClassMethod | Function_ | Closure $functionLike,
Variable $variable
): bool
{
): bool {
if ($functionLike->stmts === null) {
return false;
}
Expand Down

0 comments on commit 1d1dc73

Please sign in to comment.