Skip to content

Commit

Permalink
[Doctrine] Handle Partial removal annotation has annotation below on …
Browse files Browse the repository at this point in the history
…RemoveRedundantDefaultPropertyAnnotationValuesRector (#2914)

* [Doctrine] Handle Partial removal annotation has annotation below on RemoveRedundantDefaultPropertyAnnotationValuesRector

* Fixed 🎉

* clean up
  • Loading branch information
samsonasik committed Sep 6, 2022
1 parent a638a67 commit e5d9275
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 29 deletions.
44 changes: 15 additions & 29 deletions packages/BetterPhpDocParser/Printer/PhpDocInfoPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
use PHPStan\PhpDocParser\Ast\PhpDoc\VarTagValueNode;
use PHPStan\PhpDocParser\Lexer\Lexer;
use Rector\BetterPhpDocParser\PhpDoc\ArrayItemNode;
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\BetterPhpDocParser\PhpDocNodeVisitor\ChangedPhpDocNodeVisitor;
use Rector\BetterPhpDocParser\ValueObject\PhpDocAttributeKey;
Expand Down Expand Up @@ -59,12 +57,6 @@ final class PhpDocInfoPrinter
*/
private const NEWLINE_WITH_ASTERISK = "\n" . ' *';

/**
* @see https://regex101.com/r/WR3goY/1/
* @var string
*/
private const TAG_AND_SPACE_REGEX = '#(@.*?) \(#';

private int $tokenCount = 0;

private int $currentTokenPosition = 0;
Expand Down Expand Up @@ -195,29 +187,23 @@ private function printDocChildNode(

$shouldReprintChildNode = $this->shouldReprint($phpDocChildNode);

if ($phpDocChildNode instanceof PhpDocTagNode) {
if ($shouldReprintChildNode && ($phpDocChildNode->value instanceof ParamTagValueNode || $phpDocChildNode->value instanceof ThrowsTagValueNode || $phpDocChildNode->value instanceof VarTagValueNode || $phpDocChildNode->value instanceof ReturnTagValueNode || $phpDocChildNode->value instanceof PropertyTagValueNode)) {
// the type has changed → reprint
$phpDocChildNodeStartEnd = $phpDocChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);
// bump the last position of token after just printed node
if ($phpDocChildNodeStartEnd instanceof StartAndEnd) {
$this->currentTokenPosition = $phpDocChildNodeStartEnd->getEnd();
}

return $this->standardPrintPhpDocChildNode($phpDocChildNode);
if ($phpDocChildNode instanceof PhpDocTagNode &&
($shouldReprintChildNode && (
$phpDocChildNode->value instanceof ParamTagValueNode ||
$phpDocChildNode->value instanceof ThrowsTagValueNode ||
$phpDocChildNode->value instanceof VarTagValueNode ||
$phpDocChildNode->value instanceof ReturnTagValueNode ||
$phpDocChildNode->value instanceof PropertyTagValueNode
))) {
// the type has changed → reprint
$phpDocChildNodeStartEnd = $phpDocChildNode->getAttribute(PhpDocAttributeKey::START_AND_END);

// bump the last position of token after just printed node
if ($phpDocChildNodeStartEnd instanceof StartAndEnd) {
$this->currentTokenPosition = $phpDocChildNodeStartEnd->getEnd();
}

if ($phpDocChildNode->value instanceof DoctrineAnnotationTagValueNode && $shouldReprintChildNode) {
$silentValueArrayItemNode = $phpDocChildNode->value->getSilentValue();

if (! $silentValueArrayItemNode instanceof ArrayItemNode) {
$printedNode = (string) $phpDocChildNode;

// remove extra space between tags
$printedNode = Strings::replace($printedNode, self::TAG_AND_SPACE_REGEX, '$1(');
return self::NEWLINE_WITH_ASTERISK . ($printedNode === '' ? '' : ' ' . $printedNode);
}
}
return $this->standardPrintPhpDocChildNode($phpDocChildNode);
}

/** @var StartAndEnd|null $startAndEnd */
Expand Down
32 changes: 32 additions & 0 deletions tests/Issues/PartialValueDocblockRemoval/AutoImportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\PartialValueDocblockRemoval;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AutoImportTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/auto_import.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

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

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups as JmsGroups;

/**
* @ORM\Entity()
*/
class PartialRemovalAnnotationHasAnnotationBelow
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255, nullable=false)
*
* @Expose
* @JmsGroups({"list", "details"})
*/
private $name;

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}
}

?>
-----
<?php

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

use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups as JmsGroups;

/**
* @ORM\Entity()
*/
class PartialRemovalAnnotationHasAnnotationBelow
{
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*
* @Expose
* @JmsGroups({"list", "details"})
*/
private $name;

public function getName(): ?string
{
return $this->name;
}

public function setName(string $name): self
{
$this->name = $name;

return $this;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\PartialValueDocblockRemoval;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class PartialValueDocblockRemovalTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return Iterator<array<string>>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();
$rectorConfig->rule(RemoveRedundantDefaultPropertyAnnotationValuesRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Doctrine\Rector\Property\RemoveRedundantDefaultPropertyAnnotationValuesRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(RemoveRedundantDefaultPropertyAnnotationValuesRector::class);
};

0 comments on commit e5d9275

Please sign in to comment.