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

Fix annotation to attribute parsing string #5317

Merged
merged 4 commits into from
Dec 3, 2023
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: 3 additions & 6 deletions config/set/php83.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
<?php

declare(strict_types=1);
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;

use Rector\Config\RectorConfig;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;
use Rector\Php83\Rector\ClassMethod\AddOverrideAttributeToOverriddenMethodsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
AddOverrideAttributeToOverriddenMethodsRector::class,
AddTypeToConstRector::class,
]);
$rectorConfig->rules([AddOverrideAttributeToOverriddenMethodsRector::class, AddTypeToConstRector::class]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);


namespace Rector\Tests\BetterPhpDocParser\PhpDocParser\StaticDoctrineAnnotationParser\Source;

class StringAnnotationValue
{
/**
* @CustomAnnotation(description="List of value:
* - <b>TRY</b>: To try
* - <b>TEST</b>: to test (Default if no parameters given)")
*/
public function test()
{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,7 @@ public function parseValue(
return $constExprNode;
}

while ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON) ||
$tokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)
) {
$currentTokenValue .= $tokenIterator->currentTokenValue();
$tokenIterator->next();
}
$currentTokenValue = $this->parseStringValue($tokenIterator, $currentTokenValue);

// nested entity!, supported in attribute since PHP 8.1
if ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_OPEN_PARENTHESES)) {
Expand Down Expand Up @@ -98,6 +93,59 @@ public function parseValue(
return $currentTokenValue;
}

private function parseStringValue(BetterTokenIterator $tokenIterator, string $currentTokenValue): string
{
if (
// start with a quote but doesn't end with one
(str_starts_with($currentTokenValue, '"') && ! str_ends_with($currentTokenValue, '"'))
) {
$currentTokenValue = $this->parseMultilineOrWhiteSpacedString($tokenIterator, $currentTokenValue);
} else {
while ($tokenIterator->isCurrentTokenType(Lexer::TOKEN_DOUBLE_COLON) ||
$tokenIterator->isCurrentTokenType(Lexer::TOKEN_IDENTIFIER)
) {
$currentTokenValue .= $tokenIterator->currentTokenValue();
$tokenIterator->next();
}
}

return $currentTokenValue;
}

private function parseMultilineOrWhiteSpacedString(
BetterTokenIterator $tokenIterator,
string $currentTokenValue
): string {
while (str_starts_with($currentTokenValue, '"') && ! str_ends_with($currentTokenValue, '"')) {
if (! $tokenIterator->isCurrentTokenType(Lexer::TOKEN_PHPDOC_EOL)) {
$currentTokenValue .= ' ';
}

if (str_starts_with($currentTokenValue, '"') && str_contains(
$tokenIterator->currentTokenValue(),
'"'
) && $currentTokenValue !== $tokenIterator->currentTokenValue()) {
//starts with '"' and current token contains '"', should be the end
$currentTokenValue .= substr(
$tokenIterator->currentTokenValue(),
0,
((int) strpos($tokenIterator->currentTokenValue(), '"') + 1)
);
$tokenIterator->next();
break;
}

$currentTokenValue .= $tokenIterator->currentTokenValue();
$tokenIterator->next();
}

if (str_starts_with($currentTokenValue, '"') && str_ends_with($currentTokenValue, '"')) {
return trim(str_replace('"', '', $currentTokenValue));
}

return $currentTokenValue;
}

private function parseNestedDoctrineAnnotationTagValueNode(
string $currentTokenValue,
BetterTokenIterator $tokenIterator,
Expand Down
1 change: 0 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Rector\CodingStyle\Rector\String_\UseClassKeywordForClassNameResolutionRector;
use Rector\Config\RectorConfig;

use Rector\Core\Collector\MockedClassCollector;
use Rector\Core\Collector\ParentClassCollector;
use Rector\DeadCode\Rector\ConstFetch\RemovePhpVersionIdCheckRector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

declare(strict_types=1);
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;

use Rector\Config\RectorConfig;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Php83\Rector\ClassConst\AddTypeToConstRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(AddTypeToConstRector::class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Acme\Bar\DoNotUpdateExistingTargetNamespace;
use Rector\Config\RectorConfig;

use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Fixture\DuplicatedClass;
use Rector\Tests\Renaming\Rector\Name\RenameClassRector\Source\Contract\FirstInterface;
Expand Down
18 changes: 9 additions & 9 deletions rules/Php83/Rector/ClassConst/AddTypeToConstRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@

namespace Rector\Php83\Rector\ClassConst;

use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Identifier;
use PhpParser\Node;
use PhpParser\Node\Const_;
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\DNumber;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MissingConstantFromReflectionException;
use PHPStan\Reflection\ReflectionProvider;
Expand Down Expand Up @@ -73,7 +73,7 @@ public function refactor(Node $node): Class_|null
return null;
}

$consts = array_filter($node->stmts, static fn(Node $node): bool => $node instanceof ClassConst);
$consts = array_filter($node->stmts, static fn (Node $node): bool => $node instanceof ClassConst);

if ($consts === []) {
return null;
Expand Down Expand Up @@ -107,7 +107,7 @@ public function refactor(Node $node): Class_|null
$valueType = $this->findValueType($constNode->value);
}

if (!($valueType ?? null) instanceof Identifier) {
if (! ($valueType ?? null) instanceof Identifier) {
continue;
}

Expand Down
5 changes: 1 addition & 4 deletions rules/Strict/NodeFactory/ExactCompareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ public function createIdenticalFalsyCompare(
return new Identical($expr, new String_(''));
}

return new BooleanOr(
new Identical($expr, new String_('')),
new Identical($expr, new String_('0'))
);
return new BooleanOr(new Identical($expr, new String_('')), new Identical($expr, new String_('0')));
}

if ($exprType->isInteger()->yes()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;

use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\Rector\Class_\ClassPropertyAssignToConstructorPromotionRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;
Expand Down
35 changes: 35 additions & 0 deletions tests/Issues/PlainValueParser/Fixture/fixture.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Rector\Core\Tests\Issues\PlainValueParser\Fixture;

use Rector\Core\Tests\Issues\PlainValueParser\Source\CustomAnnotation;

final class SomeFixture
{
/**
* @CustomAnnotation(description="List of value:
* - <b>TRY</b>: To try
* - <b>TEST</b>: to test (Default if no parameters given)")
*/
public function test()
{}
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\PlainValueParser\Fixture;

use Rector\Core\Tests\Issues\PlainValueParser\Source\CustomAnnotation;

final class SomeFixture
{
#[CustomAnnotation(description: 'List of value :
- < b > TRY < /b>: To try
- < b > TEST < /b>: to test ( Default if no parameters given )')]
public function test()
{}
}

?>
28 changes: 28 additions & 0 deletions tests/Issues/PlainValueParser/PlainValueParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\PlainValueParser;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class PlainValueParserTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
10 changes: 10 additions & 0 deletions tests/Issues/PlainValueParser/Source/CustomAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Core\Tests\Issues\PlainValueParser\Source;

class CustomAnnotation
{
public function __construct(public string $description)
{
}
}
14 changes: 14 additions & 0 deletions tests/Issues/PlainValueParser/config/configured_rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Core\Tests\Issues\PlainValueParser\Source\CustomAnnotation;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector as AnnotationToAttributeRectorAlias;
use Rector\Php80\ValueObject\AnnotationToAttribute;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRectorAlias::class, [
new AnnotationToAttribute(CustomAnnotation::class),
]);
};