Is it possible to "prettify" a new PhpDocTagValueNode? #6610
-
|
Hi! We are currently writing a Rector rule (nice tool btw, I was able to migrate a lot of code due to PHP or packages update, and very easily 🙂) to add <?php
namespace App\Rector\Rules;
use App\Command\Data\Website\UpdateRoutesCommand;
use PhpParser\Node;
use Rector\BetterPhpDocParser\PhpDocManipulator\PhpDocTagRemover;
use Rector\BetterPhpDocParser\ValueObject\PhpDoc\DoctrineAnnotation\CurlyListNode;
use Rector\BetterPhpDocParser\ValueObjectFactory\PhpDocNode\Symfony\SymfonyRouteTagValueNodeFactory;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symfony\Component\Routing\Annotation\Route;
class AddRouteAnnotationToPages extends AbstractRector
{
private SymfonyRouteTagValueNodeFactory $symfonyRouteTagValueNodeFactory;
private PhpDocTagRemover $phpDocTagRemover;
public function __construct(SymfonyRouteTagValueNodeFactory $symfonyRouteTagValueNodeFactory, PhpDocTagRemover $phpDocTagRemover)
{
$this->symfonyRouteTagValueNodeFactory = $symfonyRouteTagValueNodeFactory;
$this->phpDocTagRemover = $phpDocTagRemover;
}
public function getNodeTypes(): array
{
return [Node\Stmt\Class_::class];
}
/**
* @param Node\Stmt\Class_ $node
*
* @return void|null|Node\Stmt\Class_
*/
public function refactor(Node $node)
{
// internal logic ...
// Remove and create "Route" annotation
$phpDocInfo = $this->phpDocInfoFactory->createFromNode($node);
if (null !== $routeAnnotation = $phpDocInfo->findOneByAnnotationClass(Route::class)) {
$this->phpDocTagRemover->removeTagValueFromNode($phpDocInfo, $routeAnnotation);
}
$routeItems = [
'name' => '"'.$routeName.'"',
'defaults' => new CurlyListNode([
'"_app_internal_id"' => '"'.$route['internal_id'].'"',
'"_app_display_name"' => new CurlyListNode(
array_reduce(
array_keys($route['displayName']),
function (array $acc, string $websiteId) use ($route) {
$acc[$this->getWebsiteIdConstant($websiteId)] = '"'.$route['displayName'][$websiteId].'"';
return $acc;
},
[]
)
),
]),
];
$phpDocInfo->addTagValueNode($this->symfonyRouteTagValueNodeFactory->createFromItems($routeItems));
return $node;
}
public function getRuleDefinition(): \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
// TODO: Implement getRuleDefinition() method.
}
private function isPage(Node $node): bool
{
$wamizPagesDirectory = $this->parameterProvider->provideParameter('wamiz.pages.directory');
/** @var \Rector\Core\ValueObject\Application\File $file */
$file = $node->getAttribute(AttributeKey::FILE);
return false !== strpos($file->getSmartFileInfo()->getPathname(), $wamizPagesDirectory);
}
// internal logic...
}It works fine, the annotation is nicely added to the class but everything is on a single line, which is not human-readable: /**
* @\Symfony\Component\Routing\Annotation\Route(name="app.privacy_policy", defaults={"_app_internal_id"="804c5f9b-0984-48ff-adb6-fd6b53aa4046", "_app_display_name"={Website::ID_EN="Privacy policy", Website::ID_FR="Politique de confidentialité", Website::ID_DE="Datenschutzerklärung", Website::ID_IT="Norme sulla privacy", Website::ID_ES="Política de Privacidad", Website::ID_MX="Política de Privacidad", Website::ID_LAT="Política de Privacidad", Website::ID_NL="Privacybeleid", Website::ID_PL="Polityka prywatności"}})
*/Is this possible to prettify the generated phpdoc and having something like this? /**
* @\Symfony\Component\Routing\Annotation\Route(
* name="app.privacy_policy",
* defaults={
* "_app_internal_id"="804c5f9b-0984-48ff-adb6-fd6b53aa4046",
* "_app_display_name"={
* Website::ID_EN="Privacy policy",
* Website::ID_FR="Politique de confidentialité",
* Website::ID_DE="Datenschutzerklärung",
* Website::ID_IT="Norme sulla privacy",
* Website::ID_ES="Política de Privacidad",
* Website::ID_MX="Política de Privacidad",
* Website::ID_LAT="Política de Privacidad",
* Website::ID_NL="Privacybeleid",
* Website::ID_PL="Polityka prywatności"
* }
* }
* )
*/Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
|
Hi, I think php-cs-fixer has some rules to prettify docblocks. @symplify has nested rule indent https://github.com/symplify/coding-standard/blob/main/docs/rules_overview.md#doctrineannotationnestedbracketsfixer |
Beta Was this translation helpful? Give feedback.


Hi, I think php-cs-fixer has some rules to prettify docblocks.
Maybe ask there: https://github.com/FriendsOfPHP/PHP-CS-Fixer
@symplify has nested rule indent https://github.com/symplify/coding-standard/blob/main/docs/rules_overview.md#doctrineannotationnestedbracketsfixer