Skip to content

Commit c5f709d

Browse files
authored
fix: api:upgrade-resource output formatting (#5624)
1 parent 8257741 commit c5f709d

File tree

5 files changed

+26
-14
lines changed

5 files changed

+26
-14
lines changed

src/Core/Bridge/Symfony/Bundle/Command/UpgradeApiResourceCommand.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@
2626
use ApiPlatform\Exception\ResourceClassNotFoundException;
2727
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
2828
use Doctrine\Common\Annotations\AnnotationReader;
29-
use PhpParser\Lexer\Emulative;
29+
use PhpParser\Lexer;
3030
use PhpParser\NodeTraverser;
31+
use PhpParser\NodeVisitor\CloningVisitor;
3132
use PhpParser\Parser\Php7;
3233
use PhpParser\PrettyPrinter\Standard;
3334
use SebastianBergmann\Diff\Differ;
@@ -106,7 +107,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
106107
continue;
107108
}
108109

109-
$lexer = new Emulative([
110+
$lexer = new Lexer([
110111
'usedAttributes' => [
111112
'comments',
112113
'startLine',
@@ -141,7 +142,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
141142
$oldStmts = $parser->parse($oldCode);
142143
$oldTokens = $lexer->getTokens();
143144

144-
$newStmts = $traverser->traverse($oldStmts);
145+
$cloningTraverser = new NodeTraverser();
146+
$cloningTraverser->addVisitor(new CloningVisitor()); // Required to preserve formatting
147+
148+
$newStmts = $traverser->traverse($cloningTraverser->traverse($oldStmts));
145149
$newCode = $prettyPrinter->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
146150

147151
if (!$input->getOption('force') && $input->getOption('dry-run')) {
@@ -216,7 +220,7 @@ private function getSubresources(): array
216220
private function printDiff(string $oldCode, string $newCode, OutputInterface $output): void
217221
{
218222
$consoleFormatter = new ColorConsoleDiffFormatter();
219-
$differ = class_exists(UnifiedDiffOutputBuilder::class) ? new Differ(new UnifiedDiffOutputBuilder()) : new Differ();
223+
$differ = class_exists(UnifiedDiffOutputBuilder::class) ? new Differ(new UnifiedDiffOutputBuilder()) : new Differ();
220224
$diff = $differ->diff($oldCode, $newCode);
221225
$output->write($consoleFormatter->format($diff));
222226
}

src/Core/Upgrade/RemoveAnnotationTrait.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,15 @@
1313

1414
namespace ApiPlatform\Core\Upgrade;
1515

16-
use phpDocumentor\Reflection\DocBlock\Serializer;
17-
use phpDocumentor\Reflection\DocBlockFactory;
1816
use PhpParser\Comment\Doc;
1917

2018
trait RemoveAnnotationTrait
2119
{
2220
private function removeAnnotationByTag(Doc $comment, string $tagName): Doc
2321
{
24-
$factory = DocBlockFactory::createInstance();
25-
$docBlock = $factory->create($comment->getText());
26-
foreach ($docBlock->getTagsByName($tagName) as $tag) {
27-
$docBlock->removeTag($tag);
28-
}
22+
// https://regex101.com/r/GBIPnj/1
23+
$text = preg_filter("/^[[:blank:]]*[\/*]+[[:blank:]]*@{$tagName}[[:blank:]]*(\(.*?\))?[\s*\/]*?$/ms", '', $comment->getText());
2924

30-
$serializer = new Serializer(0, '', true, null, null, \PHP_EOL);
31-
32-
return new Doc($serializer->getDocComment($docBlock));
25+
return null === $text ? $comment : new Doc($text);
3326
}
3427
}

tests/Core/Bridge/Symfony/Bundle/Command/UpgradeApiResourceCommandTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public function testDebugResource(string $entityClass, array $subresourceOperati
6969
foreach ($expectedStrings as $expectedString) {
7070
$this->assertStringContainsString($expectedString, $display);
7171
}
72+
73+
$this->assertStringNotContainsString('-declare(strict_types=1);', $display);
74+
$this->assertStringNotContainsString('+declare (strict_types=1);', $display);
75+
$this->assertStringNotContainsString('-@ORM\Column(type="integer")', $display);
76+
$this->assertStringNotContainsString('+@ORM\Column (type="integer")', $display);
77+
$this->assertStringNotContainsString('-private function nothingToDo(): void', $display);
78+
$this->assertStringNotContainsString('+private function nothingToDo() : void', $display);
7279
}
7380

7481
public function debugResourceProvider(): array

tests/Fixtures/TestBundle/Entity/DummyToUpgradeWithOnlyAnnotation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,8 @@ class DummyToUpgradeWithOnlyAnnotation
6666
* @ApiFilter(ExistsFilter::class)
6767
*/
6868
private $dummyToUpgradeProduct;
69+
70+
private function nothingToDo(): void // @phpstan-ignore-line
71+
{
72+
}
6973
}

tests/Fixtures/TestBundle/Entity/DummyToUpgradeWithOnlyAttribute.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,8 @@ class DummyToUpgradeWithOnlyAttribute
4949
#[ApiSubresource]
5050
#[ApiProperty(iri: 'DummyToUpgradeWithOnlyAttribute.dummyToUpgradeProduct')]
5151
private $dummyToUpgradeProduct;
52+
53+
private function nothingToDo(): void // @phpstan-ignore-line
54+
{
55+
}
5256
}

0 commit comments

Comments
 (0)