Skip to content

Commit

Permalink
Feature/fix class annotation bad print (#5081)
Browse files Browse the repository at this point in the history
  • Loading branch information
etshy committed Sep 26, 2023
1 parent bd4c8dd commit 50e589c
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\BetterPhpDocParser\PhpDoc\ArrayItemNode;

use Nette\Utils\FileSystem as UtilsFileSystem;
use PhpParser\Node;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\BetterPhpDocParser\Printer\PhpDocInfoPrinter;
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\PhpParser\Parser\RectorParser;
use Rector\Core\ValueObject\Application\File;
use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;

class ArrayItemNodeTest extends AbstractLazyTestCase
{
private DocBlockUpdater $docBlockUpdater;

private PhpDocInfoFactory $phpDocInfoFactory;

private PhpDocInfoPrinter $phpDocInfoPrinter;

private RectorParser $rectorParser;

private NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator;

protected function setUp(): void
{
$this->docBlockUpdater = $this->make(DocBlockUpdater::class);
$this->phpDocInfoFactory = $this->make(PhpDocInfoFactory::class);
$this->phpDocInfoPrinter = $this->make(PhpDocInfoPrinter::class);
$this->rectorParser = $this->make(RectorParser::class);
$this->nodeScopeAndMetadataDecorator = $this->make(NodeScopeAndMetadataDecorator::class);
}

public function testUpdateNestedClassAnnotation(): void
{
$filePath = __DIR__ . '/FixtureNested/DoctrineNestedClassAnnotation.php.inc';
$file = new File($filePath, UtilsFileSystem::read($filePath));

$stmtsAndTokens = $this->rectorParser->parseFileToStmtsAndTokens($file->getFilePath());
$oldStmts = $stmtsAndTokens->getStmts();
$newStmts = $this->nodeScopeAndMetadataDecorator->decorateNodesFromFile($file, $oldStmts);

$classStmt = null;
$classDocComment = null;

foreach ($newStmts as $node) {
if ($node->stmts === null) {
continue;
}

foreach ($node->stmts as $stmt) {
if (! $stmt instanceof Node\Stmt\Class_) {
continue;
}

$phpDocInfo = $this->phpDocInfoFactory->createFromNode($stmt);
$phpDocNode = $phpDocInfo->getPhpDocNode();

foreach ($phpDocNode->children as $key => $phpDocChildNode) {
$phpDocChildNode->setAttribute('start_and_end', null);
$phpDocNode->children[$key] = $phpDocChildNode;
}

$classStmt = $stmt;
break;
}

$this->docBlockUpdater->updateRefactoredNodeWithPhpDocInfo($classStmt);
$classDocComment = $this->printNodePhpDocInfoToString($classStmt);
}

$this->assertEquals(
'/**
* @ORM\Table(name="doctrine_entity", uniqueConstraints={@ORM\UniqueConstraint(name="property")})
*/',
$classDocComment
);
}

private function printNodePhpDocInfoToString(Node $node): string
{
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($node);
return $this->phpDocInfoPrinter->printFormatPreserving($phpDocInfo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\BetterPhpDocParser\PhpDoc\ArrayItemNode\FixtureNested;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Table(
* name="doctrine_entity",
* uniqueConstraints={@ORM\UniqueConstraint(name="property")}
* )
*/
class DoctrineEntity
{
/**
* @ORM\JoinTable(name="property",
* joinColumns={@ORM\JoinColumn(name="property", referencedColumnName="property")},
* inverseJoinColumns={@ORM\JoinColumn(name="property", referencedColumnName="property", unique=true)}
* )
*/
public $property;
}
2 changes: 2 additions & 0 deletions packages/BetterPhpDocParser/PhpDoc/ArrayItemNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function __toString(): string
foreach ($this->value as $singleValue) {
$value .= $singleValue;
}
} elseif ($this->value instanceof DoctrineAnnotationTagValueNode) {
$value .= (string) $this->value->identifierTypeNode . $this->value;
} else {
$value .= $this->value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __toString(): string
}

$itemContents = $this->printValuesContent($this->values);
return sprintf('(%s)', $itemContents);
return \sprintf('(%s)', $itemContents);
}

public function hasClassName(string $className): bool
Expand Down

0 comments on commit 50e589c

Please sign in to comment.