Skip to content

Commit

Permalink
[DX] Avoid supporting edge-case multi-layered comments, must be cover…
Browse files Browse the repository at this point in the history
…ed in php-parser itself (#4743)
  • Loading branch information
TomasVotruba authored Aug 9, 2023
1 parent 484a38b commit defd4e2
Show file tree
Hide file tree
Showing 9 changed files with 13 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,18 @@

use Iterator;
use Nette\Utils\FileSystem;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\PropertyProperty;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\Class_\SomeEntityClass;
use Rector\Tests\BetterPhpDocParser\PhpDocInfo\PhpDocInfoPrinter\Source\TableClass;

final class MultilineTest extends AbstractPhpDocInfoPrinterTestCase
{
#[DataProvider('provideData')]
#[DataProvider('provideDataForProperty')]
#[DataProvider('provideDataClass')]
public function test(string $docFilePath, Node $node): void
{
$docComment = FileSystem::read($docFilePath);
Expand All @@ -32,52 +29,22 @@ public function test(string $docFilePath, Node $node): void

public static function provideData(): Iterator
{
yield [__DIR__ . '/Source/Multiline/multiline1.txt', new Nop()];
yield [__DIR__ . '/Source/Multiline/multiline2.txt', new Nop()];
yield [__DIR__ . '/Source/Multiline/multiline3.txt', new Nop()];
}

/**
* @return Iterator<string[]|Class_[]>
*/
public static function provideDataClass(): Iterator
{
// class
yield [__DIR__ . '/Source/Class_/some_entity_class.txt', new Class_(SomeEntityClass::class)];
yield [__DIR__ . '/Source/Multiline/table.txt', new Class_(TableClass::class)];
}

public static function provideDataForProperty(): Iterator
{
$property = self::createPublicPropertyUnderClass('manyTo');
yield [__DIR__ . '/Source/Multiline/many_to.txt', $property];

$property = self::createPublicPropertyUnderClass('anotherProperty');
$property = self::createPublicProperty('anotherProperty');
yield [__DIR__ . '/Source/Multiline/assert_serialize.txt', $property];

$property = self::createPublicPropertyUnderClass('someProperty');
$property = self::createPublicProperty('someProperty');
yield [__DIR__ . '/Source/Multiline/multiline6.txt', $property];

$property = self::createMethodUnderClass('someMethod');
yield [__DIR__ . '/Source/Multiline/route_property.txt', $property];
}

private static function createPublicPropertyUnderClass(string $name): Property
private static function createPublicProperty(string $name): Property
{
$builderFactory = new BuilderFactory();

$propertyBuilder = $builderFactory->property($name);
$propertyBuilder->makePublic();

return $propertyBuilder->getNode();
}

private static function createMethodUnderClass(string $name): ClassMethod
{
$builderFactory = new BuilderFactory();

$methodBuilder = $builderFactory->method($name);
$methodBuilder->makePublic();

return $methodBuilder->getNode();
return new Property(Class_::MODIFIER_PUBLIC, [new PropertyProperty($name)]);
}
}

This file was deleted.

This file was deleted.

This file was deleted.

53 changes: 1 addition & 52 deletions packages/BetterPhpDocParser/PhpDocInfo/PhpDocInfoFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\BetterPhpDocParser\PhpDocInfo;

use PhpParser\Comment;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
Expand Down Expand Up @@ -77,15 +76,7 @@ public function createFromNode(Node $node): ?PhpDocInfo
$tokenIterator = new BetterTokenIterator([]);
$phpDocNode = new PhpDocNode([]);
} else {
$comments = $node->getComments();
$docs = array_filter($comments, static fn (Comment $comment): bool => $comment instanceof Doc);

if (count($docs) > 1) {
$this->storePreviousDocs($node, $comments, $docComment);
}

$text = $docComment->getText();
$tokens = $this->lexer->tokenize($text);
$tokens = $this->lexer->tokenize($docComment->getText());
$tokenIterator = new BetterTokenIterator($tokens);

$phpDocNode = $this->betterPhpDocParser->parse($tokenIterator);
Expand Down Expand Up @@ -115,48 +106,6 @@ public function createEmpty(Node $node): PhpDocInfo
return $phpDocInfo;
}

/**
* @param Comment[]|Doc[] $comments
*/
private function storePreviousDocs(Node $node, array $comments, Doc $doc): void
{
$previousDocsAsComments = [];
$newMainDoc = null;

foreach ($comments as $comment) {
// On last Doc, stop
if ($comment === $doc) {
break;
}

// pure comment
if (! $comment instanceof Doc) {
$previousDocsAsComments[] = $comment;
continue;
}

// make Doc as comment Doc that not last
$previousDocsAsComments[] = new Comment(
$comment->getText(),
$comment->getStartLine(),
$comment->getStartFilePos(),
$comment->getStartTokenPos(),
$comment->getEndLine(),
$comment->getEndFilePos(),
$comment->getEndTokenPos()
);

/**
* Make last Doc before main Doc to candidate main Doc
* so it can immediatelly be used as replacement of Main doc when main doc removed
*/
$newMainDoc = $comment;
}

$node->setAttribute(AttributeKey::PREVIOUS_DOCS_AS_COMMENTS, $previousDocsAsComments);
$node->setAttribute(AttributeKey::NEW_MAIN_DOC, $newMainDoc);
}

/**
* Needed for printing
*/
Expand Down
12 changes: 0 additions & 12 deletions packages/Comments/NodeDocBlock/DocBlockUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,6 @@ public function updateRefactoredNodeWithPhpDocInfo(Node $node): void

private function setCommentsAttribute(Node $node): void
{
if ($node->hasAttribute(AttributeKey::PREVIOUS_DOCS_AS_COMMENTS)) {
/** @var Comment[] $previousDocsAsComments */
$previousDocsAsComments = $node->getAttribute(AttributeKey::PREVIOUS_DOCS_AS_COMMENTS);
$node->setAttribute(AttributeKey::COMMENTS, $previousDocsAsComments);
}

if ($node->hasAttribute(AttributeKey::NEW_MAIN_DOC)) {
/** @var Doc $newMainDoc */
$newMainDoc = $node->getAttribute(AttributeKey::NEW_MAIN_DOC);
$node->setDocComment($newMainDoc);
}

$comments = array_filter($node->getComments(), static fn (Comment $comment): bool => ! $comment instanceof Doc);
$node->setAttribute(AttributeKey::COMMENTS, $comments);
}
Expand Down
12 changes: 0 additions & 12 deletions packages/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,6 @@ final class AttributeKey
*/
public const COMMENTS = 'comments';

/**
* Cover multi docs
* @var string
*/
public const PREVIOUS_DOCS_AS_COMMENTS = 'previous_docs_as_comments';

/**
* Cover multi docs
* @var string
*/
public const NEW_MAIN_DOC = 'new_main_doc';

/**
* Internal php-parser name.
* Do not change this even if you want!
Expand Down

This file was deleted.

8 changes: 6 additions & 2 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,12 @@ protected function mirrorComments(Node $newNode, Node $oldNode): void
/**
* @param Node|Node[] $refactoredNode
*/
private function postRefactorProcess(Node $originalNode, Node $node, Node|array|int $refactoredNode, string $filePath): Node
{
private function postRefactorProcess(
Node $originalNode,
Node $node,
Node|array|int $refactoredNode,
string $filePath
): Node {
/** @var non-empty-array<Node>|Node $refactoredNode */
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);

Expand Down

0 comments on commit defd4e2

Please sign in to comment.