-
Notifications
You must be signed in to change notification settings - Fork 602
Improve namespace generation in PHP #4270
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
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR improves namespace generation in PHP by consolidating namespace declarations to avoid unnecessary reopening. Instead of wrapping each PHP construct in its own namespace declaration, the code now uses a module-based approach with proper namespace stacking to handle nested modules while respecting PHP's lack of support for truly nested namespaces.
- Replaces per-construct namespace wrapping with module-level namespace management
- Implements namespace stack to handle module nesting by closing/reopening namespaces as needed
- Removes redundant
startNamespace
/endNamespace
calls from individual construct visitors
{ | ||
_out << eb; | ||
_parentNamespaces.pop(); | ||
if (!_parentNamespaces.empty()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The stack could become empty during visitModuleEnd, but there's no check to prevent popping from an empty stack. This could cause undefined behavior if modules are not properly balanced.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My assumption is the Parser guarantees proper balancing of start/end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is guaranteed.
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
PHP does not support nesting a namespace with a namespace, but it supports sub-namespaces.
Prior to this PR, we were simply opening the namespace around each mapped PHP construct ... lots of namespaces. This PR reduces the number of PHP namespace reopening.