Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to modify or remove comments? #990

Open
jokey35 opened this issue Mar 20, 2024 · 1 comment
Open

Is there a way to modify or remove comments? #990

jokey35 opened this issue Mar 20, 2024 · 1 comment

Comments

@jokey35
Copy link

jokey35 commented Mar 20, 2024

In the AST, there is no comment node, but after generating code again from AST, there are the original comments. Can I at least remove them when generating code from AST?

@nikic
Copy link
Owner

nikic commented Mar 20, 2024

The comments are stored as attributes on the node. There are some APIs for manipulating them:

/**
* Gets all comments directly preceding this node.
*
* The comments are also available through the "comments" attribute.
*
* @return Comment[]
*/
public function getComments(): array;
/**
* Gets the doc comment of the node.
*
* @return null|Comment\Doc Doc comment object or null
*/
public function getDocComment(): ?Comment\Doc;
/**
* Sets the doc comment of the node.
*
* This will either replace an existing doc comment or add it to the comments array.
*
* @param Comment\Doc $docComment Doc comment to set
*/
public function setDocComment(Comment\Doc $docComment): void;

There is no direct API to drop comments, but you can use $node->setAttribute('comments', []).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants