-
-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/fix class annotation bad print (#5081)
- Loading branch information
Showing
4 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
packages-tests/BetterPhpDocParser/PhpDoc/ArrayItemNode/ArrayItemNodeTest.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...tterPhpDocParser/PhpDoc/ArrayItemNode/FixtureNested/DoctrineNestedClassAnnotation.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters